<?php
use CodeIgniter\Router\RouteCollection;
/** @var RouteCollection $routes */

// ── Auth ───────────────────────────────────────────────────────
$routes->get( 'login',                 'Auth\AuthController::loginPage');
$routes->post('login',                 'Auth\AuthController::doLogin');
$routes->get( 'logout',                'Auth\AuthController::logout');
$routes->post('forgot-password',       'Auth\AuthController::forgotPassword');
$routes->get( 'reset-password/(:any)', 'Auth\AuthController::resetPassword/$1');
$routes->post('reset-password',        'Auth\AuthController::doResetPassword');

// ── Webhooks (no auth) ─────────────────────────────────────────
$routes->post('webhooks/stripe', 'Api\WebhooksController::stripe');

// ── App (all require auth) ─────────────────────────────────────
$routes->group('', ['filter' => 'auth'], function($routes) {

    // Dashboard
    $routes->get('/', 'DashboardController::index');
    $routes->get('dashboard', 'DashboardController::index');

    // Customers
    $routes->get( 'customers',                    'CustomersController::index');
    $routes->get( 'customers/create',             'CustomersController::create');
    $routes->post('customers/create',             'CustomersController::store');
    $routes->get( 'customers/(:num)',             'CustomersController::show/$1');
    $routes->get( 'customers/(:num)/edit',        'CustomersController::edit/$1');
    $routes->post('customers/(:num)/edit',        'CustomersController::update/$1');
    $routes->post('customers/(:num)/delete',      'CustomersController::delete/$1');
    $routes->post('customers/(:num)/restore',     'CustomersController::restore/$1');
    $routes->post('customers/(:num)/address',     'CustomersController::addAddress/$1');
    $routes->post('customers/(:num)/address/(:num)/delete', 'CustomersController::deleteAddress/$1/$2');
    $routes->post('customers/(:num)/note',        'CustomersController::addNote/$1');
    $routes->post('customers/(:num)/file',        'CustomersController::uploadFile/$1');

    // Jobs
    $routes->get( 'jobs',                         'JobsController::index');
    $routes->get( 'jobs/create',                  'JobsController::create');
    $routes->post('jobs/create',                  'JobsController::store');
    $routes->get( 'jobs/(:num)',                  'JobsController::show/$1');
    $routes->get( 'jobs/(:num)/edit',             'JobsController::edit/$1');
    $routes->post('jobs/(:num)/edit',             'JobsController::update/$1');
    $routes->post('jobs/(:num)/delete',           'JobsController::delete/$1');
    $routes->post('jobs/(:num)/restore',          'JobsController::restore/$1');
    $routes->post('jobs/(:num)/complete',         'JobsController::complete/$1');
    $routes->post('jobs/(:num)/duplicate',        'JobsController::duplicate/$1');
    $routes->post('jobs/(:num)/checklist/(:num)', 'JobsController::toggleChecklist/$1/$2');
    $routes->post('jobs/(:num)/note',             'JobsController::addNote/$1');
    $routes->post('jobs/(:num)/file',             'JobsController::uploadFile/$1');
    $routes->post('jobs/(:num)/time',             'JobsController::logTime/$1');
    $routes->post('jobs/(:num)/checkin',          'JobsController::checkIn/$1');
    $routes->post('jobs/(:num)/checkout',         'JobsController::checkOut/$1');
    $routes->post('jobs/(:num)/credit',           'JobsController::addCredit/$1');

    // Calendar
    $routes->get( 'calendar',                     'CalendarController::index');
    $routes->post('calendar/visit',               'CalendarController::createVisit');
    $routes->post('calendar/visit/(:num)/edit',   'CalendarController::updateVisit/$1');
    $routes->post('calendar/visit/(:num)/delete', 'CalendarController::deleteVisit/$1');

    // Quotes
    $routes->get( 'quotes',                       'QuotesController::index');
    $routes->get( 'quotes/create',                'QuotesController::create');
    $routes->post('quotes/create',                'QuotesController::store');
    $routes->get( 'quotes/(:num)',                'QuotesController::show/$1');
    $routes->get( 'quotes/(:num)/edit',           'QuotesController::edit/$1');
    $routes->post('quotes/(:num)/edit',           'QuotesController::update/$1');
    $routes->post('quotes/(:num)/delete',         'QuotesController::delete/$1');
    $routes->post('quotes/(:num)/restore',        'QuotesController::restore/$1');
    $routes->post('quotes/(:num)/send',           'QuotesController::send/$1');
    $routes->post('quotes/(:num)/accept',         'QuotesController::accept/$1');
    $routes->post('quotes/(:num)/decline',        'QuotesController::decline/$1');
    $routes->post('quotes/(:num)/convert-job',    'QuotesController::convertToJob/$1');
    $routes->post('quotes/(:num)/convert-invoice','QuotesController::convertToInvoice/$1');
    $routes->get( 'quotes/(:num)/pdf',            'QuotesController::pdf/$1');

    // Invoices
    $routes->get( 'invoices',                     'InvoicesController::index');
    $routes->get( 'invoices/create',              'InvoicesController::create');
    $routes->post('invoices/create',              'InvoicesController::store');
    $routes->get( 'invoices/from-job/(:num)',     'InvoicesController::fromJob/$1');
    $routes->get( 'invoices/(:num)',              'InvoicesController::show/$1');
    $routes->get( 'invoices/(:num)/edit',         'InvoicesController::edit/$1');
    $routes->post('invoices/(:num)/edit',         'InvoicesController::update/$1');
    $routes->post('invoices/(:num)/delete',       'InvoicesController::delete/$1');
    $routes->post('invoices/(:num)/restore',      'InvoicesController::restore/$1');
    $routes->post('invoices/(:num)/send',         'InvoicesController::send/$1');
    $routes->post('invoices/(:num)/void',         'InvoicesController::void/$1');
    $routes->post('invoices/(:num)/payment',      'InvoicesController::recordPayment/$1');
    $routes->post('invoices/(:num)/payment/(:num)/delete', 'InvoicesController::deletePayment/$1/$2');
    $routes->get( 'invoices/(:num)/pdf',          'InvoicesController::pdf/$1');

    // Time
    $routes->get( 'time',                         'TimeController::index');
    $routes->post('time/log',                     'TimeController::log');
    $routes->post('time/(:num)/edit',             'TimeController::update/$1');
    $routes->post('time/(:num)/delete',           'TimeController::delete/$1');

    // Reports
    $routes->get('reports',                       'ReportsController::index');
    $routes->get('reports/export/invoices',       'ReportsController::exportInvoices');
    $routes->get('reports/export/time',           'ReportsController::exportTime');

    // Settings
    $routes->get( 'settings',                     'SettingsController::index');
    $routes->get( 'settings/(:segment)',          'SettingsController::tab/$1');
    $routes->post('settings/(:segment)',          'SettingsController::save/$1');
    $routes->post('settings/users/create',        'SettingsController::createUser');
    $routes->post('settings/users/(:num)/edit',   'SettingsController::updateUser/$1');
    $routes->post('settings/users/(:num)/delete', 'SettingsController::deleteUser/$1');
    $routes->post('settings/roles/create',        'SettingsController::createRole');
    $routes->post('settings/roles/(:num)/edit',   'SettingsController::updateRole/$1');
    $routes->post('settings/roles/(:num)/delete', 'SettingsController::deleteRole/$1');
    $routes->post('settings/tax/create',          'SettingsController::createTaxRate');
    $routes->post('settings/tax/(:num)/delete',   'SettingsController::deleteTaxRate/$1');

    // Global Settings
    $routes->get( 'global',                        'GlobalController::index');
    $routes->get( 'global/(:segment)',             'GlobalController::tab/$1');
    $routes->post('global/job-types/save',         'GlobalController::saveJobType');
    $routes->post('global/job-types/(:num)/delete','GlobalController::deleteJobType/$1');
    $routes->post('global/statuses/save',          'GlobalController::saveStatus');
    $routes->post('global/statuses/(:num)/delete', 'GlobalController::deleteStatus/$1');
    $routes->post('global/pricing/save',           'GlobalController::savePricing');
    $routes->post('global/pricing/(:num)/delete',  'GlobalController::deletePricing/$1');
    $routes->post('global/templates/save',         'GlobalController::saveTemplate');
    $routes->post('global/templates/(:num)/delete','GlobalController::deleteTemplate/$1');
    $routes->post('global/custom-fields/save',     'GlobalController::saveCustomField');
    $routes->post('global/custom-fields/(:num)/delete','GlobalController::deleteCustomField/$1');

    // Activity
    $routes->get('activity', 'ActivityController::index');

    // Recycle Bin
    $routes->get( 'bin',                    'BinController::index');
    $routes->post('bin/(:num)/restore',     'BinController::restore/$1');
    $routes->post('bin/(:num)/purge',       'BinController::purge/$1');
    $routes->post('bin/purge-all',          'BinController::purgeAll');

    // AJAX endpoints
    $routes->get( 'ajax/customers/search',  'AjaxController::customerSearch');
    $routes->get( 'ajax/jobs/search',       'AjaxController::jobSearch');
    $routes->post('ajax/checkin',           'AjaxController::checkIn');
    $routes->post('ajax/checkout',          'AjaxController::checkOut');
    $routes->get( 'ajax/notifications',     'AjaxController::notifications');
    $routes->post('ajax/notifications/read','AjaxController::markRead');
});