<?php

use CodeIgniter\Router\RouteCollection;

/**
 * @var RouteCollection $routes
 */
$routes->get('/', 'Home::index');
$routes->get('workspace', 'Home::workspace');
$routes->get('dashboard', 'Home::dashboard');
$routes->get('dashboard/simple', 'Home::dashboardSimple');
$routes->get('dashboard/compact', 'Home::dashboardCompact');
$routes->get('dashboard/classic-mock', 'Home::dashboardClassicMock');
$routes->get('dashboard/modern', 'Home::dashboardModern');
$routes->get('dashboard/dense', 'Home::dashboardDense');
$routes->get('dashboard/charts', 'Home::dashboardCharts');
$routes->get('dashboard/blend', 'Home::dashboardBlend');
$routes->get('dashboard/lite', 'Home::dashboardLite');
$routes->get('dashboard/gov-portal', 'Home::dashboardGovPortal');
$routes->get('dashboard/gov-portal/code-kinds', 'Home::dashboardGovPortalCodeKinds');
$routes->get('dashboard/gov-portal-strip', 'Home::dashboardGovPortalStrip');
$routes->get('dashboard/gov-portal-strip/code-kinds', 'Home::dashboardGovPortalStripCodeKinds');
$routes->get('bag/inventory-inquiry', 'Home::inventoryInquiry');
$routes->get('bag/waste-suibal-enterprise', 'Home::wasteSuibalEnterprise');

// 사이트 메뉴 (/bag/*)
$routes->get('bag/basic-info', 'Bag::basicInfo');
$routes->get('bag/prices', 'Bag::prices');
$routes->post('bag/prices', 'Bag::prices');
$routes->get('bag/packaging-units', 'Bag::packagingUnits');
$routes->get('bag/code-kinds', 'Bag::codeKinds');
$routes->get('bag/code-details/(:num)', 'Bag::codeDetails/$1');

// 옛 주소 호환: 세부 목록만 사이트로 이동
$routes->get('admin/code-details/(:num)', 'Admin\CodeDetail::index/$1');
$routes->get('bag/purchase-inbound', 'Bag::purchaseInbound');
$routes->get('bag/issue', 'Bag::issueLegacy');
$routes->get('bag/issue/cancel', 'Bag::issue');
$routes->get('bag/inventory', 'Bag::inventory');
$routes->get('bag/inventory/export', 'Bag::inventoryExport');
$routes->get('bag/inventory/inspection-select', 'Bag::inspectionSelect');
$routes->get('bag/inventory/inspection-work', 'Bag::inspectionWork');
$routes->post('bag/inventory/inspection-run', 'Bag::inspectionRun');
$routes->post('bag/inventory/inspection-select/save', 'Bag::inspectionSelectSave');
$routes->post('bag/inventory/inspection-select/confirm', 'Bag::inspectionSelectConfirm');
$routes->get('bag/inventory/inspection/(:num)', 'Bag::inspectionDetail/$1');
$routes->post('bag/inventory/inspection/(:num)/save', 'Bag::inspectionSave/$1');
$routes->post('bag/inventory/inspection/(:num)/apply', 'Bag::inspectionApply/$1');
$routes->get('bag/sales', 'Bag::sales');
$routes->get('bag/sales-stats', 'Bag::salesStats');
$routes->get('bag/flow', 'Bag::flow');
$routes->get('bag/flow/export', 'Bag::flowExport');
$routes->get('bag/analytics', 'Bag::analytics');
$routes->get('bag/analytics/year-over-year', 'Bag::analyticsYearOverYear');
$routes->get('bag/analytics/monthly-trend', 'Bag::analyticsMonthlyTrend');
$routes->get('bag/analytics/seasonal-trend', 'Bag::analyticsSeasonalTrend');
$routes->get('bag/window', 'Bag::window');
$routes->get('bag/help', 'Bag::help');

// 사용자 매뉴얼(설명서) — 로그인 사용자 전용
$routes->group('bag', ['filter' => 'loginAuth'], static function ($routes): void {
    $routes->get('manual', 'Bag::manual');
    $routes->get('manual/search', 'Bag::manualSearch'); // (:segment) 보다 먼저
    $routes->get('manual/(:segment)', 'Bag::manualPage/$1');
    $routes->post('activity/print-log', 'Bag::printLog'); // 인쇄 활동 기록(beacon)
});

$routes->get('bag/number-lookup', 'Bag::numberLookup');
$routes->post('bag/number-lookup/resolve', 'Bag::numberLookupResolve');

// 사이트 메뉴 CRUD (사이트 레이아웃)
$routes->get('bag/issue/create', 'Bag::issueCreate');
$routes->post('bag/issue/store', 'Bag::issueStore');
$routes->post('bag/issue/cancel/(:num)', 'Bag::issueCancel/$1');
$routes->post('bag/issue/cancel-save', 'Bag::issueCancelSave');
$routes->get('bag/order/create', 'Bag::orderCreate');
$routes->get('bag/order/detail-json/(:num)', 'Bag::orderDetailJson/$1');
$routes->get('bag/order/phone', 'Bag::phoneOrderCreate');
$routes->get('bag/order/phone/manage', 'Bag::phoneOrderManage');
$routes->post('bag/order/phone/manage/update', 'Bag::phoneOrderUpdate');
$routes->post('bag/order/phone/manage/scan', 'Bag::phoneOrderManageScan');
$routes->post('bag/order/phone/manage/cancel/(:num)', 'Bag::phoneOrderCancel/$1');
$routes->get('bag/order/phone/receipt/(:num)', 'Bag::phoneOrderReceipt/$1');
$routes->get('bag/order/change', 'Bag::orderChange');
$routes->get('bag/order/revise/(:num)', 'Bag::orderRevise/$1');
$routes->get('bag/order/lot-seed', 'Bag::orderLotSeed');
$routes->get('bag/order/lot-seed/preview', 'Bag::orderLotSeedPreview');
$routes->post('bag/order/lot-seed/generate', 'Bag::orderLotSeedGenerate');
$routes->post('bag/order/store', 'Bag::orderStore');
$routes->post('bag/order/cancel/(:num)', 'Bag::orderCancel/$1');
$routes->post('bag/order/delete', 'Bag::orderDeletePost');
$routes->post('bag/order/delete/(:num)', 'Bag::orderDelete/$1');
$routes->get('bag/receiving/create', 'Bag::receivingCreate');
$routes->post('bag/receiving/store', 'Bag::receivingStore');
$routes->get('bag/receiving/scanner', 'Bag::receivingScanner');
$routes->post('bag/receiving/scanner/store', 'Bag::receivingScannerStore');
$routes->get('bag/receiving/batch', 'Bag::receivingBatch');
$routes->post('bag/receiving/batch/store', 'Bag::receivingBatchStore');
$routes->get('bag/receiving/status', 'Bag::receivingStatus');
$routes->get('bag/receiving/status/export', 'Bag::receivingStatusExport');
$routes->get('bag/sale/create', 'Bag::saleCreate');
$routes->post('bag/sale/store', 'Bag::saleStore');
$routes->get('bag/sale/designated', 'Bag::designatedShopSaleCreate');
$routes->get('bag/sale/designated/dev-saleable-barcodes', 'Bag::designatedShopDevSaleableBarcodes');
$routes->get('bag/sale/dev-all-sales-history', 'Bag::devAllSalesHistory');
$routes->post('bag/sale/designated/scan', 'Bag::designatedShopSaleScan');
$routes->post('bag/sale/designated/save', 'Bag::designatedShopSaleSave');
$routes->get('bag/sale/designated-return', 'Bag::designatedShopSaleReturnCreate');
$routes->post('bag/sale/designated-return/scan', 'Bag::designatedShopSaleReturnScan');
$routes->post('bag/sale/designated-return/save', 'Bag::designatedShopSaleReturnSave');
$routes->get('bag/sale/designated-return-cancel', 'Bag::designatedShopSaleReturnCancelCreate');
$routes->post('bag/sale/designated-return-cancel/save', 'Bag::designatedShopSaleReturnCancelSave');
$routes->get('bag/sale/designated-cancel', 'Bag::designatedShopReturnCreate');
$routes->post('bag/sale/designated-cancel/submit', 'Bag::designatedShopReturnCancel');
$routes->get('bag/shop-order/create', 'Bag::shopOrderCreate');
$routes->post('bag/shop-order/store', 'Bag::shopOrderStore');

// 메인 사이트 메뉴용 업무 URL (관리자 권한). 동일 컨트롤러가 URI 가 bag 이면 메인 사이트 레이아웃으로 렌더.
$routes->group('bag', ['filter' => 'adminAuth'], static function ($routes): void {
    $routes->get('managers', 'Admin\Manager::index');
    $routes->get('managers/create', 'Admin\Manager::create');
    $routes->post('managers/store', 'Admin\Manager::store');
    $routes->get('managers/edit/(:num)', 'Admin\Manager::edit/$1');
    $routes->post('managers/update/(:num)', 'Admin\Manager::update/$1');
    $routes->post('managers/delete/(:num)', 'Admin\Manager::delete/$1');

    $routes->get('sales-agencies', 'Admin\SalesAgency::index');
    $routes->get('sales-agencies/create', 'Admin\SalesAgency::create');
    $routes->post('sales-agencies/store', 'Admin\SalesAgency::store');
    $routes->get('sales-agencies/edit/(:num)', 'Admin\SalesAgency::edit/$1');
    $routes->post('sales-agencies/update/(:num)', 'Admin\SalesAgency::update/$1');
    $routes->post('sales-agencies/delete/(:num)', 'Admin\SalesAgency::delete/$1');

    $routes->get('companies', 'Admin\Company::index');
    $routes->get('companies/create', 'Admin\Company::create');
    $routes->post('companies/store', 'Admin\Company::store');
    $routes->get('companies/edit/(:num)', 'Admin\Company::edit/$1');
    $routes->post('companies/update/(:num)', 'Admin\Company::update/$1');
    $routes->post('companies/delete/(:num)', 'Admin\Company::delete/$1');

    $routes->get('free-recipients', 'Admin\FreeRecipient::index');
    $routes->get('free-recipients/create', 'Admin\FreeRecipient::create');
    $routes->post('free-recipients/store', 'Admin\FreeRecipient::store');
    $routes->get('free-recipients/edit/(:num)', 'Admin\FreeRecipient::edit/$1');
    $routes->post('free-recipients/update/(:num)', 'Admin\FreeRecipient::update/$1');
    $routes->post('free-recipients/delete/(:num)', 'Admin\FreeRecipient::delete/$1');

    $routes->get('designated-shops/export', 'Admin\DesignatedShop::export');
    $routes->get('designated-shops/map', 'Admin\DesignatedShop::map');
    $routes->get('designated-shops/status/export', 'Admin\DesignatedShop::statusExport');
    $routes->get('designated-shops/status', 'Admin\DesignatedShop::status');
    $routes->get('designated-shops/barcode', 'Admin\DesignatedShop::barcode');
    $routes->post('designated-shops/barcode/print', 'Admin\DesignatedShop::barcodePrint');
    $routes->get('designated-shops/district-new-cancel/export', 'Admin\DesignatedShop::districtNewCancelExport');
    $routes->get('designated-shops/district-new-cancel', 'Admin\DesignatedShop::districtNewCancel');
    $routes->get('designated-shops/browse', 'Admin\DesignatedShop::browse');
    $routes->get('designated-shops', 'Admin\DesignatedShop::index');
    $routes->get('designated-shops/create', 'Admin\DesignatedShop::create');
    $routes->post('designated-shops/resolve-address-codes', 'Admin\DesignatedShop::resolveAddressCodes');
    $routes->post('designated-shops/store', 'Admin\DesignatedShop::store');
    $routes->get('designated-shops/edit/(:num)', 'Admin\DesignatedShop::edit/$1');
    $routes->post('designated-shops/update/(:num)', 'Admin\DesignatedShop::update/$1');
    $routes->post('designated-shops/delete/(:num)', 'Admin\DesignatedShop::delete/$1');

    $routes->get('bag-prices', 'Admin\BagPrice::index');
    $routes->get('bag-prices/create', 'Admin\BagPrice::create');
    $routes->post('bag-prices/store', 'Admin\BagPrice::store');
    $routes->get('bag-prices/edit/(:num)', 'Admin\BagPrice::edit/$1');
    $routes->post('bag-prices/update/(:num)', 'Admin\BagPrice::update/$1');
    $routes->post('bag-prices/delete/(:num)', 'Admin\BagPrice::delete/$1');
    $routes->get('bag-prices/history/(:num)', 'Admin\BagPrice::history/$1');

    $routes->get('bag-orders/export', 'Admin\BagOrder::export');
    $routes->get('bag-orders', 'Admin\BagOrder::index');
    $routes->get('bag-orders/create', 'Admin\BagOrder::create');
    $routes->get('bag-orders/revise/(:num)', 'Admin\BagOrder::revise/$1');
    $routes->post('bag-orders/store', 'Admin\BagOrder::store');
    $routes->get('bag-orders/detail/(:num)', 'Admin\BagOrder::detail/$1');
    $routes->post('bag-orders/cancel/(:num)', 'Admin\BagOrder::cancel/$1');
    $routes->post('bag-orders/delete/(:num)', 'Admin\BagOrder::delete/$1');

    $routes->get('bag-receivings', 'Admin\BagReceiving::index');
    $routes->get('bag-receivings/create', 'Admin\BagReceiving::create');
    $routes->post('bag-receivings/store', 'Admin\BagReceiving::store');

    $routes->get('bag-inventory/export', 'Admin\BagInventory::export');
    $routes->get('bag-inventory', 'Admin\BagInventory::index');

    $routes->get('shop-orders', 'Admin\ShopOrder::index');
    $routes->get('shop-orders/create', 'Admin\ShopOrder::create');
    $routes->post('shop-orders/store', 'Admin\ShopOrder::store');
    $routes->post('shop-orders/cancel/(:num)', 'Admin\ShopOrder::cancel/$1');

    $routes->get('bag-sales/export', 'Admin\BagSale::export');
    $routes->get('bag-sales', 'Admin\BagSale::index');
    $routes->get('bag-sales/create', 'Admin\BagSale::create');
    $routes->post('bag-sales/store', 'Admin\BagSale::store');

    $routes->get('bag-issues', 'Admin\BagIssue::index');
    $routes->get('bag-issues/create', 'Admin\BagIssue::create');
    $routes->post('bag-issues/store', 'Admin\BagIssue::store');
    $routes->post('bag-issues/cancel/(:num)', 'Admin\BagIssue::cancel/$1');

    $routes->get('packaging-units/manage', 'Admin\PackagingUnit::index');
    $routes->get('packaging-units/manage/create', 'Admin\PackagingUnit::create');
    $routes->post('packaging-units/manage/store', 'Admin\PackagingUnit::store');
    $routes->get('packaging-units/manage/edit/(:num)', 'Admin\PackagingUnit::edit/$1');
    $routes->post('packaging-units/manage/update/(:num)', 'Admin\PackagingUnit::update/$1');
    $routes->post('packaging-units/manage/delete/(:num)', 'Admin\PackagingUnit::delete/$1');
    $routes->get('packaging-units/manage/history/(:num)', 'Admin\PackagingUnit::history/$1');

    $routes->get('reports/sales-ledger', 'Admin\SalesReport::salesLedger');
    $routes->get('reports/daily-summary', 'Admin\SalesReport::dailySummary');
    $routes->get('reports/period-sales', 'Admin\SalesReport::periodSales');
    $routes->get('reports/supply-demand', 'Admin\SalesReport::supplyDemand');
    $routes->get('reports/yearly-sales', 'Admin\SalesReport::yearlySales');
    $routes->get('reports/shop-sales', 'Admin\SalesReport::shopSales');
    $routes->get('reports/hometax-export', 'Admin\SalesReport::hometaxExport');
    $routes->get('reports/returns', 'Admin\SalesReport::returns');
    $routes->get('reports/returns/export', 'Admin\SalesReport::returnsExport');
    $routes->get('reports/lot-flow', 'Admin\SalesReport::lotFlow');
    $routes->get('reports/misc-flow', 'Admin\SalesReport::miscFlow');
    $routes->post('reports/misc-flow', 'Admin\SalesReport::miscFlowStore');
    $routes->post('reports/misc-flow/delete', 'Admin\SalesReport::miscFlowDelete');

    $routes->get('password-change', 'Admin\PasswordChange::index');
    $routes->post('password-change', 'Admin\PasswordChange::update');
});

// 사용자 의견 접수 (로그인 사용자)
$routes->post('feedback', 'Feedback::store', ['filter' => 'loginAuth']);

// Auth
$routes->get('login', 'Auth::showLoginForm');
$routes->post('login', 'Auth::login');
$routes->get('login/two-factor', 'Auth::showTwoFactor');
$routes->post('login/two-factor', 'Auth::verifyTwoFactor');
$routes->get('login/totp-setup', 'Auth::showTotpSetup');
$routes->post('login/totp-setup', 'Auth::completeTotpSetup');
$routes->get('logout', 'Auth::logout');
$routes->get('register', 'Auth::showRegisterForm');
$routes->post('register', 'Auth::register');

// Admin (adminAuth 필터 적용)
$routes->group('admin', ['filter' => 'adminAuth'], static function ($routes): void {
    $routes->get('select-local-government', 'Admin\SelectLocalGovernment::index');
    $routes->post('select-local-government', 'Admin\SelectLocalGovernment::store');

    // 사용자 의견 검토
    $routes->get('feedback', 'Admin\Feedback::index');
    $routes->get('feedback/all', 'Admin\Feedback::all');
    $routes->get('feedback/image/(:num)', 'Admin\Feedback::image/$1');
    $routes->get('feedback/file/(:num)', 'Admin\Feedback::file/$1');
    $routes->get('feedback/(:num)', 'Admin\Feedback::show/$1');
    $routes->post('feedback/(:num)/status', 'Admin\Feedback::updateStatus/$1');
    $routes->get('/', 'Admin\Dashboard::index');
    $routes->get('users', 'Admin\User::index');
    $routes->get('users/create', 'Admin\User::create');
    $routes->post('users/store', 'Admin\User::store');
    $routes->get('users/edit/(:num)', 'Admin\User::edit/$1');
    $routes->post('users/update/(:num)', 'Admin\User::update/$1');
    $routes->post('users/unlock-login/(:num)', 'Admin\User::unlockLogin/$1');
    $routes->post('users/delete/(:num)', 'Admin\User::delete/$1');
    $routes->get('access/login-history', 'Admin\Access::loginHistory');
    $routes->get('access/activity-logs', 'Admin\ActivityLog::index');
    $routes->get('access/activity-logs/(:num)', 'Admin\ActivityLog::show/$1');
    $routes->get('access/approvals', 'Admin\Access::approvals');
    $routes->post('access/approve/(:num)', 'Admin\Access::approve/$1');
    $routes->post('access/reject/(:num)', 'Admin\Access::reject/$1');
    $routes->get('roles', 'Admin\Role::index');
    $routes->get('menus', 'Admin\Menu::index');
    $routes->get('menus/list', 'Admin\Menu::list');
    $routes->post('menus/store', 'Admin\Menu::store');
    $routes->post('menus/update/(:num)', 'Admin\Menu::update/$1');
    $routes->post('menus/delete/(:num)', 'Admin\Menu::delete/$1');
    $routes->post('menus/move', 'Admin\Menu::move');

    // Local government & designated shop 관리
    $routes->get('local-governments', 'Admin\LocalGovernment::index');
    $routes->get('local-governments/create', 'Admin\LocalGovernment::create');
    $routes->post('local-governments/store', 'Admin\LocalGovernment::store');
    $routes->get('local-governments/edit/(:num)', 'Admin\LocalGovernment::edit/$1');
    $routes->post('local-governments/update/(:num)', 'Admin\LocalGovernment::update/$1');
    $routes->post('local-governments/delete/(:num)', 'Admin\LocalGovernment::delete/$1');

    // 기본코드 종류 관리 (P2-01) — 등록·수정·삭제는 관리자 전용
    $routes->get('code-kinds/create', 'Admin\CodeKind::create');
    $routes->post('code-kinds/store', 'Admin\CodeKind::store');
    $routes->get('code-kinds/edit/(:num)', 'Admin\CodeKind::edit/$1');
    $routes->post('code-kinds/update/(:num)', 'Admin\CodeKind::update/$1');
    $routes->post('code-kinds/delete/(:num)', 'Admin\CodeKind::delete/$1');

    // 세부코드 관리 (P2-02)
    $routes->get('code-details/(:num)/create', 'Admin\CodeDetail::create/$1');
    $routes->post('code-details/store', 'Admin\CodeDetail::store');
    $routes->get('code-details/edit/(:num)', 'Admin\CodeDetail::edit/$1');
    $routes->post('code-details/update/(:num)', 'Admin\CodeDetail::update/$1');
    $routes->post('code-details/delete/(:num)', 'Admin\CodeDetail::delete/$1');

    // 구 업무 URL → /bag/* (실제 처리는 bag 그룹). GET 301, POST 307.
    $adminToBagPrefixes = [
        'managers',
        'sales-agencies',
        'companies',
        'free-recipients',
        'designated-shops',
        'bag-prices',
        'bag-orders',
        'bag-receivings',
        'bag-inventory',
        'shop-orders',
        'bag-sales',
        'bag-issues',
        'packaging-units',
        'reports',
        'password-change',
    ];
    foreach ($adminToBagPrefixes as $p) {
        $routes->match(['get', 'post'], $p, 'Admin\WorkMovedToBag::toBag/' . $p);
        $routes->match(['get', 'post'], $p . '/(:any)', 'Admin\WorkMovedToBag::toBag/' . $p . '/$1');
    }
});
