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

// ============================================================
// PUBLIC ROUTES — No auth required
// ============================================================
$routes->get('health',   'Health::index');
$routes->get('/',        'Auth::login');
$routes->get('login',    'Auth::login');
$routes->post('login',   'Auth::doLogin', ['filter' => 'throttle']);
$routes->get('logout',   'Auth::logout');
$routes->get('register', 'Auth::register');
$routes->post('register','Auth::doRegister');

// Public maintenance portal
$routes->get('request',               'PublicRequest::index');
$routes->post('request',              'PublicRequest::submit');
$routes->get('track/(:segment)',      'PublicRequest::track/$1');

// File serving — authenticated via controller
$routes->get('file/(:segment)/(:any)', 'FileServe::serve/$1/$2');

// ============================================================
// PROTECTED ROUTES — Requires auth filter
// ============================================================
$routes->group('', ['filter' => ['auth', 'rbac']], static function (RouteCollection $routes) {

    // ── Dashboard ────────────────────────────────────────────
    $routes->get('dashboard', 'Dashboard::index');
    $routes->get('dashboard/kpi', 'Reports::kpi');

    // ── Work Orders ──────────────────────────────────────────
    $routes->get('workorders',                           'WorkOrders::index');
    $routes->get('workorders/create',                    'WorkOrders::create');
    $routes->post('workorders/store',                    'WorkOrders::store');
    $routes->get('workorders/view/(:num)',               'WorkOrders::view/$1');
    $routes->get('workorders/edit/(:num)',               'WorkOrders::edit/$1');
    $routes->post('workorders/update/(:num)',            'WorkOrders::update/$1');
    $routes->post('workorders/delete/(:num)',            'WorkOrders::delete/$1');
    $routes->get('workorders/print/(:num)',              'WorkOrders::printView/$1');
    // AJAX / action endpoints
    $routes->post('workorders/status',                  'WorkOrders::updateStatus');
    $routes->post('workorders/comment/(:num)',          'WorkOrders::addComment/$1');
    $routes->post('workorders/upload/(:num)',           'WorkOrders::uploadAttachment/$1');
    $routes->post('workorders/escalate/(:num)',         'WorkOrders::escalate/$1');
    // Labor
    $routes->post('workorders/labor/add/(:num)',        'WorkOrders::addLabor/$1');
    $routes->post('workorders/labor/delete/(:num)',     'WorkOrders::deleteLabor/$1');
    // Materials
    $routes->post('workorders/material/add/(:num)',     'WorkOrders::addMaterial/$1');
    $routes->post('workorders/material/delete/(:num)', 'WorkOrders::deleteMaterial/$1');
    // Approval
    $routes->post('workorders/approve/(:num)',          'WorkOrders::approve/$1');
    $routes->post('workorders/reject/(:num)',           'WorkOrders::reject/$1');
    $routes->post('workorders/qa-approve/(:num)',       'WorkOrders::qaApprove/$1');
    $routes->post('workorders/qa-reject/(:num)',        'WorkOrders::qaReject/$1');
    $routes->post('workorders/client-approve/(:num)',  'WorkOrders::clientApprove/$1');
    $routes->post('workorders/client-reject/(:num)',   'WorkOrders::clientReject/$1');
    // Meter readings
    $routes->post('workorders/meter/(:num)',            'WorkOrders::addMeterReading/$1');
    // Schedule
    $routes->get('workorders/schedule',                'WorkOrders::schedule');

    // ── Assets ───────────────────────────────────────────────
    $routes->get('asset-register',                      'Assets::index');
    $routes->get('asset-register/create',               'Assets::create');
    $routes->post('asset-register/store',               'Assets::store');
    $routes->get('asset-register/view/(:num)',          'Assets::view/$1');
    $routes->get('asset-register/edit/(:num)',          'Assets::edit/$1');
    $routes->post('asset-register/update/(:num)',       'Assets::update/$1');
    $routes->post('asset-register/delete/(:num)',       'Assets::delete/$1');
    $routes->get('asset-register/qrcode/(:num)',        'Assets::qrcode/$1');
    $routes->get('asset-register/history/(:num)',       'Assets::history/$1');

    // ── Facilities ───────────────────────────────────────────
    $routes->get('facilities',                          'Facilities::index');
    $routes->get('facilities/create',                   'Facilities::create');
    $routes->post('facilities/store',                   'Facilities::store');
    $routes->get('facilities/view/(:num)',              'Facilities::view/$1');
    $routes->get('facilities/edit/(:num)',              'Facilities::edit/$1');
    $routes->post('facilities/update/(:num)',           'Facilities::update/$1');
    $routes->post('facilities/delete/(:num)',           'Facilities::delete/$1');
    // Units under facilities
    $routes->get('facilities/(:num)/units',             'Units::index/$1');
    $routes->get('facilities/(:num)/units/create',      'Units::create/$1');
    $routes->post('facilities/(:num)/units/store',      'Units::store/$1');
    $routes->get('units/view/(:num)',                   'Units::view/$1');
    $routes->get('units/edit/(:num)',                   'Units::edit/$1');
    $routes->post('units/update/(:num)',                'Units::update/$1');
    $routes->post('units/delete/(:num)',                'Units::delete/$1');
    $routes->get('units/checklist/(:num)/(:segment)',   'Units::checklist/$1/$2');
    $routes->post('units/checklist/store',              'Units::storeChecklist');
    $routes->get('units/checklist/print/(:num)',        'Units::printChecklist/$1');

    // ── Employees / Workforce ────────────────────────────────
    $routes->get('employees',                           'Employees::index');
    $routes->get('employees/create',                    'Employees::create');
    $routes->post('employees/store',                    'Employees::store');
    $routes->get('employees/view/(:num)',               'Employees::view/$1');
    $routes->get('employees/edit/(:num)',               'Employees::edit/$1');
    $routes->post('employees/update/(:num)',            'Employees::update/$1');
    $routes->post('employees/delete/(:num)',            'Employees::delete/$1');
    $routes->post('employees/checkin',                 'Employees::checkin');
    $routes->post('employees/checkout',                'Employees::checkout');
    $routes->post('employees/break/start',             'Employees::startBreak');
    $routes->post('employees/break/end',               'Employees::endBreak');
    $routes->get('employees/attendance',               'Employees::attendance');

    // ── Finance ──────────────────────────────────────────────
    $routes->get('finance',                             'Finance::index');
    // Invoices
    $routes->get('finance/invoices',                    'Finance::invoices');
    $routes->get('finance/invoices/create',             'Finance::createInvoice');
    $routes->post('finance/invoices/store',             'Finance::storeInvoice');
    $routes->get('finance/invoices/view/(:num)',        'Finance::viewInvoice/$1');
    $routes->get('finance/invoices/edit/(:num)',        'Finance::editInvoice/$1');
    $routes->post('finance/invoices/update/(:num)',     'Finance::updateInvoice/$1');
    $routes->post('finance/invoices/status/(:num)',     'Finance::updateInvoiceStatus/$1');
    $routes->get('finance/invoices/print/(:num)',       'Finance::printInvoice/$1');
    // Contracts
    $routes->get('finance/contracts',                   'Finance::contracts');
    $routes->get('finance/contracts/create',            'Finance::createContract');
    $routes->post('finance/contracts/store',            'Finance::storeContract');
    $routes->get('finance/contracts/view/(:num)',       'Finance::viewContract/$1');
    $routes->get('finance/contracts/edit/(:num)',       'Finance::editContract/$1');
    $routes->post('finance/contracts/update/(:num)',    'Finance::updateContract/$1');
    // Expenses
    $routes->get('finance/expenses',                    'Finance::expenses');
    $routes->get('finance/expenses/create',             'Finance::createExpense');
    $routes->post('finance/expenses/store',             'Finance::storeExpense');
    $routes->post('finance/expenses/approve/(:num)',    'Finance::approveExpense/$1');
    $routes->post('finance/expenses/reject/(:num)',     'Finance::rejectExpense/$1');
    // Petty Cash
    $routes->get('finance/petty-cash',                  'Finance::pettyCash');
    $routes->get('finance/petty-cash/create',           'Finance::createPettyCash');
    $routes->post('finance/petty-cash/store',           'Finance::storePettyCash');
    $routes->get('finance/petty-cash/view/(:num)',        'Finance::viewPettyCash/$1');
    $routes->post('finance/petty-cash/approve/(:num)',   'Finance::approvePettyCash/$1');
    $routes->post('finance/petty-cash/reject/(:num)',    'Finance::rejectPettyCash/$1');
    $routes->post('finance/petty-cash/issue/(:num)',     'Finance::issuePettyCash/$1');
    $routes->post('finance/petty-cash/reconcile/(:num)', 'Finance::reconcilePettyCash/$1');
    $routes->post('finance/petty-cash/close/(:num)',     'Finance::closePettyCash/$1');
    // Reimbursements
    $routes->get('finance/reimbursements',              'Finance::reimbursements');
    $routes->get('finance/reimbursements/create',       'Finance::createReimbursement');
    $routes->post('finance/reimbursements/store',       'Finance::storeReimbursement');
    $routes->post('finance/reimbursements/approve/(:num)', 'Finance::approveReimbursement/$1');
    $routes->post('finance/reimbursements/pay/(:num)',     'Finance::payReimbursement/$1');
    $routes->get('finance/payments',                      'Finance::payments');
    $routes->post('finance/payments/record/(:num)',      'Finance::recordPayment/$1');
    $routes->get('finance/ledger',                        'Finance::ledger');

    // ── Job Cards ────────────────────────────────────────────
    $routes->get('job-cards',                           'JobCards::index');
    $routes->get('job-cards/create',                    'JobCards::create');
    $routes->post('job-cards/store',                    'JobCards::store');
    $routes->get('job-cards/view/(:num)',               'JobCards::view/$1');
    $routes->get('job-cards/edit/(:num)',               'JobCards::edit/$1');
    $routes->post('job-cards/update/(:num)',            'JobCards::update/$1');
    $routes->get('job-cards/print/(:num)',              'JobCards::printView/$1');
    $routes->post('job-cards/approve/(:num)',           'JobCards::approve/$1');
    $routes->post('job-cards/material/add/(:num)',      'JobCards::addMaterial/$1');

    // ── Estimations ──────────────────────────────────────────
    $routes->get('estimations',                         'Estimations::index');
    $routes->get('estimations/create',                  'Estimations::create');
    $routes->post('estimations/store',                  'Estimations::store');
    $routes->get('estimations/view/(:num)',             'Estimations::view/$1');
    $routes->get('estimations/edit/(:num)',             'Estimations::edit/$1');
    $routes->post('estimations/update/(:num)',          'Estimations::update/$1');
    $routes->post('estimations/approve/(:num)',         'Estimations::approve/$1');
    $routes->post('estimations/convert/(:num)',         'Estimations::convertToWorkOrder/$1');
    $routes->get('estimations/print/(:num)',            'Estimations::printView/$1');

    // ── Inventory ────────────────────────────────────────────
    $routes->get('inventory',                           'Inventory::index');
    $routes->get('inventory/create',                    'Inventory::create');
    $routes->post('inventory/store',                    'Inventory::store');
    $routes->get('inventory/view/(:num)',               'Inventory::view/$1');
    $routes->get('inventory/edit/(:num)',               'Inventory::edit/$1');
    $routes->post('inventory/update/(:num)',            'Inventory::update/$1');
    $routes->post('inventory/delete/(:num)',            'Inventory::delete/$1');
    $routes->get('inventory/movement',                 'Inventory::movement');
    $routes->post('inventory/addMovement',             'Inventory::addMovement');

    // ── Helpdesk ─────────────────────────────────────────────
    $routes->get('helpdesk',                            'Helpdesk::index');
    $routes->get('helpdesk/view/(:num)',                'Helpdesk::view/$1');
    $routes->post('helpdesk/verify/(:num)',             'Helpdesk::verify/$1');
    $routes->post('helpdesk/reject/(:num)',             'Helpdesk::reject/$1');
    $routes->post('helpdesk/convert/(:num)',            'Helpdesk::convert/$1');
    $routes->post('helpdesk/updateStatus/(:num)',       'Helpdesk::updateStatus/$1');
    $routes->post('helpdesk/feedback/(:num)',           'Helpdesk::submitFeedback/$1');

    // ── Utility & Energy ─────────────────────────────────────
    $routes->get('utility',                             'Utility::index');
    $routes->get('utility/create',                      'Utility::create');
    $routes->post('utility/store',                      'Utility::store');

    // ── Compliance & Safety ──────────────────────────────────
    $routes->get('compliance',                          'Compliance::index');
    $routes->get('compliance/audit/create',             'Compliance::createAudit');
    $routes->post('compliance/audit/store',             'Compliance::storeAudit');
    $routes->get('compliance/incident/create',          'Compliance::createIncident');
    $routes->post('compliance/incident/store',          'Compliance::storeIncident');
    $routes->get('compliance/inspections',              'Compliance::inspections');
    $routes->get('compliance/inspections/create',       'Compliance::createInspection');
    $routes->post('compliance/inspections/store',       'Compliance::storeInspection');
    $routes->get('compliance/inspections/view/(:num)', 'Compliance::viewInspection/$1');
    $routes->post('compliance/inspections/submit/(:num)', 'Compliance::submitInspection/$1');
    $routes->get('compliance/inspections/print/(:num)', 'Compliance::printInspection/$1');

    // ── Vendors ──────────────────────────────────────────────
    $routes->get('vendors',                             'Vendors::index');
    $routes->get('vendors/create',                      'Vendors::create');
    $routes->post('vendors/store',                      'Vendors::store');
    $routes->get('vendors/view/(:num)',                 'Vendors::view/$1');
    $routes->get('vendors/edit/(:num)',                 'Vendors::edit/$1');         // FIXED: was missing
    $routes->post('vendors/update/(:num)',              'Vendors::update/$1');       // FIXED: was missing
    $routes->post('vendors/delete/(:num)',              'Vendors::delete/$1');       // FIXED: was missing
    $routes->get('vendors/performance/(:num)',          'Vendors::performance/$1');

    // ── Procurement ──────────────────────────────────────────
    $routes->get('procurement',                         'Procurement::index');
    // Purchase Requests
    $routes->get('procurement/request/create',          'Procurement::createRequest');
    $routes->post('procurement/request/store',          'Procurement::storeRequest');
    $routes->get('procurement/request/view/(:num)',     'Procurement::viewRequest/$1');
    $routes->post('procurement/request/approve/(:num)','Procurement::approveRequest/$1');
    $routes->post('procurement/request/reject/(:num)', 'Procurement::rejectRequest/$1');
    // RFQ
    $routes->get('procurement/rfq/create',              'Procurement::createRfq');
    $routes->post('procurement/rfq/store',              'Procurement::storeRfq');
    $routes->get('procurement/rfq/view/(:num)',         'Procurement::viewRfq/$1');
    $routes->post('procurement/rfq/quotation/(:num)',   'Procurement::addQuotation/$1');
    $routes->get('procurement/rfq/compare/(:num)',      'Procurement::compareQuotations/$1');
    // Purchase Orders
    $routes->get('procurement/order/create',            'Procurement::createOrder');
    $routes->post('procurement/order/store',            'Procurement::storeOrder');
    $routes->get('procurement/order/view/(:num)',       'Procurement::viewOrder/$1');
    $routes->post('procurement/order/approve/(:num)',   'Procurement::approveOrder/$1');
    $routes->get('procurement/order/print/(:num)',      'Procurement::printOrder/$1');
    // GRN
    $routes->get('procurement/grn/create/(:num)',       'Procurement::createGrn/$1');
    $routes->post('procurement/grn/store',              'Procurement::storeGrn');
    $routes->get('procurement/grn/view/(:num)',         'Procurement::viewGrn/$1');

    // ── Maintenance Costing ──────────────────────────────────
    $routes->get('costing',                             'Costing::index');
    $routes->get('costing/create',                      'Costing::create');
    $routes->post('costing/store',                      'Costing::store');
    $routes->get('costing/view/(:num)',                 'Costing::view/$1');

    // ── AI Insights ──────────────────────────────────────────
    $routes->get('ai',                                  'AiInsights::index');
    $routes->get('ai/predictive',                       'AiInsights::predictive');
    $routes->get('ai/risk',                             'AiInsights::risk');
    $routes->get('ai/cost',                             'AiInsights::cost');
    $routes->post('ai/assign',                          'AiInsights::smartAssign');

    // ── Reports ──────────────────────────────────────────────
    $routes->get('reports',                             'Reports::index');
    $routes->get('reports/kpi',                         'Reports::kpi');
    $routes->get('reports/workorders',                  'Reports::workorders');
    $routes->get('reports/finance',                     'Reports::finance');
    $routes->get('reports/sla',                         'Reports::sla');
    $routes->get('reports/assets',                      'Reports::assets');
    $routes->get('reports/occupancy',                   'Reports::occupancy');
    $routes->get('reports/contracts',                   'Reports::contracts');
    $routes->get('reports/procurement',                 'Reports::procurement');
    $routes->get('reports/inventory',                   'Reports::inventory');
    $routes->get('reports/technician',                  'Reports::technicianPerformance');
    // Export endpoints
    $routes->get('reports/export/(:segment)/(:segment)', 'Reports::export/$1/$2');

    // ── Settings ─────────────────────────────────────────────
    $routes->get('settings',                            'Settings::index');
    $routes->post('settings/update',                   'Settings::update');
    $routes->post('settings/switch-company',           'Settings::switchCompany');
    $routes->get('settings/users',                      'Settings::users');
    $routes->get('settings/users/create',               'Settings::createUser');
    $routes->post('settings/users/store',               'Settings::storeUser');
    $routes->get('settings/users/edit/(:num)',          'Settings::editUser/$1');
    $routes->post('settings/users/update/(:num)',       'Settings::updateUser/$1');
    $routes->post('settings/users/delete/(:num)',       'Settings::deleteUser/$1');
    $routes->get('settings/roles',                      'Settings::roles');
    $routes->post('settings/roles/save',               'Settings::saveRoles');
    $routes->get('settings/workflow',                   'Settings::workflow');
    $routes->post('settings/workflow/save',            'Settings::saveWorkflow');
    $routes->get('settings/activity-log',               'Settings::activityLog');
    $routes->get('settings/login-history',              'Settings::loginHistory');
    $routes->get('settings/companies',                  'Settings::companies');
    $routes->get('settings/companies/create',           'Settings::createCompany');
    $routes->post('settings/companies/store',           'Settings::storeCompany');
    $routes->get('settings/companies/edit/(:num)',      'Settings::editCompany/$1');
    $routes->post('settings/companies/update/(:num)',   'Settings::updateCompany/$1');

    // ── Profile ──────────────────────────────────────────────
    $routes->get('profile',                             'Profile::index');
    $routes->post('profile/update',                    'Profile::update');
    $routes->post('profile/changePassword',            'Profile::changePassword');

    // ── Notifications ────────────────────────────────────────
    $routes->get('notifications',                       'Notifications::index');
    $routes->post('notifications/read/(:num)',          'Notifications::markRead/$1');
    $routes->post('notifications/read-all',            'Notifications::markAllRead');

    // ── AJAX ─────────────────────────────────────────────────
    $routes->get('ajax/work-orders',                   'Ajax::liveWorkOrders');
    $routes->get('ajax/notifications',                 'Ajax::notifications');
    $routes->get('ajax/dashboard-stats',               'Ajax::dashboardStats');
    $routes->get('ajax/inventory-price/(:num)',        'Ajax::inventoryPrice/$1');
    $routes->get('ajax/wo-chat/(:num)',                'Ajax::woChat/$1');
    $routes->post('ajax/wo-chat/(:num)',               'Ajax::sendWoChat/$1');
});

// ============================================================
// REST API — JWT protected
// ============================================================
$routes->group('api/v1', ['filter' => 'jwt'], static function (RouteCollection $routes) {
    $routes->post('auth/login',            'Api\Auth::login');
    $routes->get('workorders',             'Api\WorkOrders::index');
    $routes->get('workorders/(:num)',      'Api\WorkOrders::show/$1');
    $routes->post('workorders',            'Api\WorkOrders::create');
    $routes->put('workorders/(:num)',      'Api\WorkOrders::update/$1');
    $routes->get('finance/invoices',       'Api\Finance::invoices');
    $routes->post('public/request',        'Api\PublicApi::submitRequest');
});
// Public API endpoint (no auth):
$routes->post('api/v1/public/request', 'Api\PublicApi::submitRequest');
