<?php

use CodeIgniter\Router\RouteCollection;

/**
 * @var RouteCollection $routes
 */
$routes->setAutoRoute(true);

/*
|--------------------------------------------------------------------------
| AUTH
|--------------------------------------------------------------------------
*/

$routes->get('/', 'Auth\Login::index');
$routes->get('login', 'Auth\Login::index');

$routes->post('login/auth', 'Auth\Login::auth');

$routes->get('logout', 'Auth\Logout::index');

$routes->get('forgot-password', 'Auth\ForgotPassword::index');
$routes->post('forgot-password/send', 'Auth\ForgotPassword::sendResetLink');

$routes->get('home/getAdmin', 'Admin\Dashboard::index');
/*
|--------------------------------------------------------------------------
| Public Invoice
|--------------------------------------------------------------------------
*/

$routes->get('invoice/(:segment)', 'InvoicePublic::detail/$1');
$routes->get('invoice/(:segment)/pdf', 'InvoicePublic::pdf/$1');
/*
|--------------------------------------------------------------------------
| Dashboard Umum
|--------------------------------------------------------------------------
*/

$routes->group('', ['filter' => 'login'], function ($routes) {

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

});


/*
|--------------------------------------------------------------------------
| SUPERADMIN
|--------------------------------------------------------------------------
*/

$routes->group('superadmin', ['filter' => 'superadmin'], function ($routes) {

    /*
    |--------------------------------------------------------------------------
    | Dashboard
    |--------------------------------------------------------------------------
    */

    $routes->get('dashboard', 'Superadmin\Dashboard::index');


   
    $routes->get('user', 'Superadmin\User::index');
    $routes->get('user/create', 'Superadmin\User::create');
    $routes->get('user/store', 'Superadmin\User::getStore'); // Safe GET fallback redirect ke form create
    $routes->post('user/store', 'Superadmin\User::store');
    $routes->get('user/detail/(:num)', 'Superadmin\User::detail/$1');
    $routes->get('user/edit/(:num)', 'Superadmin\User::edit/$1');
    $routes->post('user/update/(:num)', 'Superadmin\User::update/$1');
    $routes->get('user/delete/(:num)', 'Superadmin\User::delete/$1');
    $routes->get('user/activate/(:num)', 'Superadmin\User::activate/$1');
    $routes->get('user/deactivate/(:num)', 'Superadmin\User::deactivate/$1');
    $routes->get('user/reset-password/(:num)', 'Superadmin\User::resetPasswordForm/$1');
    $routes->post('user/reset-password/(:num)', 'Superadmin\User::resetPassword/$1');

    /*
    |--------------------------------------------------------------------------
    | ROLE
    |--------------------------------------------------------------------------
    */
    $routes->get('role', 'Superadmin\Role::index');
    $routes->get('role/create', 'Superadmin\Role::create');
    $routes->post('role/store', 'Superadmin\Role::store');
    $routes->get('role/detail/(:num)', 'Superadmin\Role::detail/$1');
    $routes->get('role/edit/(:num)', 'Superadmin\Role::edit/$1');
    $routes->post('role/update/(:num)', 'Superadmin\Role::update/$1');
    $routes->get('role/delete/(:num)', 'Superadmin\Role::delete/$1');

    /*
    |--------------------------------------------------------------------------
    | ACTIVITY LOG
    |--------------------------------------------------------------------------
    */
    $routes->get('activity', 'Superadmin\ActivityLog::index');
    $routes->get('activity/detail/(:num)', 'Superadmin\ActivityLog::detail/$1');
    $routes->get('activity/filter', 'Superadmin\ActivityLog::filter');

    /*
    |--------------------------------------------------------------------------
    | LOGIN LOG
    |--------------------------------------------------------------------------
    */
    $routes->get('loginlog', 'Superadmin\LoginLog::index');
    $routes->get('loginlog/detail/(:num)', 'Superadmin\LoginLog::detail/$1');
    $routes->get('loginlog/filter', 'Superadmin\LoginLog::filter');

    /*
    |--------------------------------------------------------------------------
    | BACKUP
    |--------------------------------------------------------------------------
    */
    $routes->get('backup', 'Superadmin\Backup::index');
    $routes->get('backup/history', 'Superadmin\Backup::history');
    $routes->get('backup/create', 'Superadmin\Backup::createBackup');
    $routes->get('backup/download/(:num)', 'Superadmin\Backup::download/$1');
    $routes->get('backup/delete/(:num)', 'Superadmin\Backup::delete/$1');

    /*
    |--------------------------------------------------------------------------
    | RESTORE
    |--------------------------------------------------------------------------
    */
    $routes->get('restore', 'Superadmin\Restore::index');
    $routes->post('restore/process', 'Superadmin\Restore::process');
    $routes->get('restore/history', 'Superadmin\Restore::history');

    /*
    |--------------------------------------------------------------------------
    | SETTING
    |--------------------------------------------------------------------------
    */
    $routes->get('setting', 'Superadmin\Setting::index');
    $routes->post('setting/save', 'Superadmin\Setting::save');

    /*
    |--------------------------------------------------------------------------
    | PROFILE
    |--------------------------------------------------------------------------
    */
    $routes->get('profile', 'Superadmin\Profile::index');
    $routes->get('profile/edit', 'Superadmin\Profile::edit');
    $routes->post('profile/update', 'Superadmin\Profile::update');
    $routes->get('profile/password', 'Superadmin\Profile::password');
    $routes->post('profile/changePassword', 'Superadmin\Profile::changePassword');

});


$routes->group('owner', ['filter' => 'auth:2'], function ($routes) {

    /* ============================================================
     * Dashboard
     * ============================================================ */
   
    /*
    |--------------------------------------------------------------------------
    | Dashboard
    |--------------------------------------------------------------------------
    */
    $routes->get('dashboard', 'Owner\Dashboard::index');


    /*
    |--------------------------------------------------------------------------
    | Barang Center
    |--------------------------------------------------------------------------
    */
    $routes->get('product', 'Owner\Product::index');
    $routes->get('product/create', 'Owner\Product::create');
    $routes->post('product/store', 'Owner\Product::store');
    $routes->get('product/detail/(:num)', 'Owner\Product::detail/$1');
    $routes->get('product/edit/(:num)', 'Owner\Product::edit/$1');
    $routes->post('product/update/(:num)', 'Owner\Product::update/$1');
    $routes->get('product/delete/(:num)', 'Owner\Product::delete/$1');

    /*
    |--------------------------------------------------------------------------
    | Kategori Barang
    |--------------------------------------------------------------------------
    */
    $routes->get('category', 'Owner\Category::index');
    $routes->get('category/create', 'Owner\Category::create');
    $routes->post('category/store', 'Owner\Category::store');
    $routes->get('category/edit/(:num)', 'Owner\Category::edit/$1');
    $routes->post('category/update/(:num)', 'Owner\Category::update/$1');
    $routes->post('category/delete/(:num)', 'Owner\Category::delete/$1');


    /*
    |--------------------------------------------------------------------------
    | Order Monitor
    |--------------------------------------------------------------------------
    */
    $routes->get('order', 'Owner\Order::index');
    $routes->get('order/detail/(:num)', 'Owner\Order::detail/$1');
    $routes->get('order/waiting/(:num)', 'Owner\Order::waiting/$1');
    $routes->get('order/process/(:num)', 'Owner\Order::process/$1');
    $routes->get('order/finish/(:num)', 'Owner\Order::finish/$1');
    $routes->get('order/cancel/(:num)', 'Owner\Order::cancel/$1');
    $routes->get('order/history', 'Owner\Order::history');
    $routes->get('order/priority', 'Owner\Order::priority');
    $routes->get('order/deadline', 'Owner\Order::deadline');


    /*
    |--------------------------------------------------------------------------
    | Produksi
    |--------------------------------------------------------------------------
    */
    $routes->get('production', 'Owner\Production::index');
    $routes->get('production/detail/(:num)', 'Owner\Production::detail/$1');
    $routes->get('production/history', 'Owner\Production::history');
    $routes->get('production/waiting/(:num)', 'Owner\Production::waiting/$1');
    $routes->get('production/process/(:num)', 'Owner\Production::process/$1');
    $routes->get('production/finish/(:num)', 'Owner\Production::finish/$1');
    $routes->get('production/cancel/(:num)', 'Owner\Production::cancel/$1');
    $routes->get('production/limit', 'Owner\Production::limit');
    $routes->post('production/saveLimit', 'Owner\Production::saveLimit');


    /*
    |--------------------------------------------------------------------------
    | Belanja Material
    |--------------------------------------------------------------------------
    */
    $routes->get('material', 'Owner\Material::index');
    $routes->get('material/create', 'Owner\Material::create');
    $routes->post('material/store', 'Owner\Material::store');
    $routes->get('material/detail/(:num)', 'Owner\Material::detail/$1');
    $routes->get('material/edit/(:num)', 'Owner\Material::edit/$1');
    $routes->post('material/update/(:num)', 'Owner\Material::update/$1');
    $routes->get('material/delete/(:num)', 'Owner\Material::delete/$1');
    $routes->get('material/report', 'Owner\Material::report');


    /*
    |--------------------------------------------------------------------------
    | Penggajian
    |--------------------------------------------------------------------------
    */
    $routes->get('payroll', 'Owner\Payroll::index');
    $routes->get('payroll/create', 'Owner\Payroll::create');
    $routes->post('payroll/store', 'Owner\Payroll::store');
    $routes->get('payroll/detail/(:num)', 'Owner\Payroll::detail/$1');
    $routes->get('payroll/edit/(:num)', 'Owner\Payroll::edit/$1');
    $routes->post('payroll/update/(:num)', 'Owner\Payroll::update/$1');
    $routes->get('payroll/pay/(:num)', 'Owner\Payroll::pay/$1');
    $routes->get('payroll/delete/(:num)', 'Owner\Payroll::delete/$1');
    $routes->get('payroll/history', 'Owner\Payroll::history');
    $routes->get('payroll/recap', 'Owner\Payroll::recap');


    /*
    |--------------------------------------------------------------------------
    | Finance
    |--------------------------------------------------------------------------
    */
   $routes->get('payments', 'Owner\PaymentController::index');

    /*
    |--------------------------------------------------------------------------
    | Report
    |--------------------------------------------------------------------------
    */
    $routes->get('report', 'Owner\Report::index');
    $routes->get('report/sales', 'Owner\Report::sales');
    $routes->get('report/material', 'Owner\Report::material');
    $routes->get('report/payroll', 'Owner\Report::payroll');
    $routes->get('report/cashflow', 'Owner\Report::cashflow');
    $routes->get('report/receivable', 'Owner\Report::receivable');
    $routes->get('report/exportExcel', 'Owner\Report::exportExcel');
    $routes->get('report/exportPDF', 'Owner\Report::exportPDF');


    /*
    |--------------------------------------------------------------------------
    | Pengaturan
    |--------------------------------------------------------------------------
    */
    $routes->get('setting', 'Owner\Setting::index');
    $routes->post('setting/update', 'Owner\Setting::update');
    $routes->get('setting/targetProfit', 'Owner\Setting::targetProfit');
    $routes->get('setting/backup', 'Owner\Setting::backup');
    $routes->get('setting/restore', 'Owner\Setting::restore');
    $routes->get('setting/logs', 'Owner\Setting::logs');

});
// --- 5. Route Grup Admin/Kasir (Role 3) ---

    // Dashboard
  $routes->group('admin', ['filter' => 'auth:3'], function($routes) {
    
    // Dashboard & Dynamic Calendar AJAX
    $routes->get('dashboard', 'Admin\Dashboard::index');
    $routes->get('dashboard/getEvents/(:num)/(:num)', 'Admin\Dashboard::getEvents/$1/$2');

    // Order Management
    $routes->get('orders', 'Admin\OrderController::index');
    $routes->get('orders/create', 'Admin\OrderController::create');
    $routes->post('orders/store', 'Admin\OrderController::store');
    $routes->get('orders/show', 'Admin\OrderController::show');
    $routes->get('orders/show/(:num)', 'Admin\OrderController::show/$1');
    $routes->get('orders/edit/(:num)', 'Admin\OrderController::edit/$1');
    $routes->post('orders/update/(:num)', 'Admin\OrderController::update/$1');
    $routes->post('orders/payment/(:num)', 'Admin\OrderController::updatePayment/$1');
    $routes->get('orders/delete/(:num)', 'Admin\OrderController::delete/$1');
    $routes->get('orders/test-email', 'Admin\OrderController::testEmail');

    // Payment Management
    $routes->get('payments', 'Admin\PaymentController::index');
    $routes->get('payments/create/(:num)', 'Admin\PaymentController::create/$1');
    $routes->post('payments/store', 'Admin\PaymentController::store');
    $routes->post('payments/invoice/send/(:num)', 'Admin\PaymentController::sendInvoice/$1');
    $routes->get('payments/invoice/view/(:num)', 'Admin\PaymentController::viewInvoice/$1');
    $routes->get('payments/delete/(:num)', 'Admin\PaymentController::delete/$1');

    // Production Management
    $routes->get('production', 'Admin\ProductionController::index');
    $routes->get('production/create/(:num)', 'Admin\ProductionController::create/$1');
    $routes->get('production/show/(:num)', 'Admin\ProductionController::show/$1');
    $routes->post('production/start/(:num)', 'Admin\ProductionController::start/$1');
    $routes->post('production/advance/(:num)', 'Admin\ProductionController::advance/$1');
    $routes->post('production/update-progress/(:num)', 'Admin\ProductionController::updateProgress/$1');
    $routes->get('production/delete/(:num)', 'Admin\ProductionController::delete/$1');
});

// --- 2. Route API (Stateless) ---
$routes->group('api', function($routes) {
    $routes->post('auth/login', 'Api\AuthApi::login');
    $routes->get('products', 'Api\ProductApi::index');
    $routes->post('cart/add', 'Api\CartApi::add');
    $routes->post('payment/process', 'Api\PaymentApi::create');
});

// --- 3. Route Report (PDF/Excel) ---
$routes->group('report', ['filter' => 'auth'], function($routes) {
    $routes->get('excel/sales', 'Report\Excel::downloadSales');
    $routes->get('pdf/invoice/(:num)', 'Report\Pdf::printInvoice/$1');
    $routes->get('pdf/workorder/(:num)', 'Report\Pdf::printWorkOrder/$1');
});
