<?php
// Nowills SQC ERP — route map (web only, session auth, no API needed on site)
use App\Http\Controllers as C;
use Illuminate\Support\Facades\Route;

Route::get('login', [C\AuthController::class, 'showLogin'])->name('login');
Route::post('login', [C\AuthController::class, 'login'])->name('login.post');
Route::post('logout', [C\AuthController::class, 'logout'])->name('logout');

Route::middleware('auth')->group(function () {
    Route::get('/', fn() => redirect()->route('command'));
    Route::get('command', [C\CommandController::class, 'shell'])->name('command');
    Route::prefix('api/v1')->name('api.v1.')->group(function () {
        Route::get('dashboard/exec', [C\Api\V1\DashboardController::class, 'exec'])->name('dashboard.exec');
        Route::get('modules/quarry', [C\Api\V1\QuarryController::class, 'show'])->name('modules.quarry');
        Route::get('modules/drill', [C\Api\V1\DrillController::class, 'show'])->name('modules.drill');
        Route::get('modules/crush', [C\Api\V1\CrushController::class, 'show'])->name('modules.crush');
        Route::get('modules/weigh', [C\Api\V1\WeighController::class, 'show'])->name('modules.weigh');
        Route::get('modules/stockpile', [C\Api\V1\StockpileController::class, 'show'])->name('modules.stockpile');
        Route::get('modules/sales', [C\Api\V1\SalesController::class, 'show'])->name('modules.sales');
        Route::get('modules/equip', [C\Api\V1\EquipController::class, 'show'])->name('modules.equip');
        Route::get('modules/projects', [C\Api\V1\ProjectController::class, 'show'])->name('modules.projects');
        Route::get('modules/proc', [C\Api\V1\ProcController::class, 'show'])->name('modules.proc');
        Route::get('modules/inventory', [C\Api\V1\InventoryController::class, 'show'])->name('modules.inventory');
        Route::get('modules/finance', [C\Api\V1\FinanceController::class, 'show'])->name('modules.finance');
        Route::get('modules/hr', [C\Api\V1\HrController::class, 'show'])->name('modules.hr');
        Route::get('modules/hse', [C\Api\V1\HseController::class, 'show'])->name('modules.hse');
        Route::get('modules/env', [C\Api\V1\EnvController::class, 'show'])->name('modules.env');
        Route::get('modules/treasury', [C\Api\V1\TreasuryController::class, 'show'])->name('modules.treasury');
        Route::get('modules/tax', [C\Api\V1\TaxController::class, 'show'])->name('modules.tax');
        Route::get('modules/budget', [C\Api\V1\BudgetController::class, 'show'])->name('modules.budget');
        Route::get('modules/contracts', [C\Api\V1\ContractController::class, 'show'])->name('modules.contracts');
        Route::get('modules/maint', [C\Api\V1\MaintController::class, 'show'])->name('modules.maint');
        Route::get('modules/fleet', [C\Api\V1\FleetController::class, 'show'])->name('modules.fleet');
        Route::get('modules/fuel', [C\Api\V1\FuelController::class, 'show'])->name('modules.fuel');
        Route::get('modules/ipc', [C\Api\V1\IpcController::class, 'show'])->name('modules.ipc');
        Route::get('modules/site', [C\Api\V1\SiteController::class, 'show'])->name('modules.site');
        Route::get('modules/crm', [C\Api\V1\CrmController::class, 'show'])->name('modules.crm');
        Route::get('modules/suppliers', [C\Api\V1\SuppController::class, 'show'])->name('modules.suppliers');
        Route::get('modules/docs', [C\Api\V1\DocsController::class, 'show'])->name('modules.docs');
        Route::get('modules/reports', [C\Api\V1\ReportsController::class, 'show'])->name('modules.reports');
        Route::get('modules/ai', [C\Api\V1\AiController::class, 'show'])->name('modules.ai');
        Route::get('modules/wflow', [C\Api\V1\WflowController::class, 'show'])->name('modules.wflow');
        Route::get('modules/setup', [C\Api\V1\AdminController::class, 'show'])->name('modules.setup');
        Route::get('modules/admin', [C\Api\V1\AdminController::class, 'show'])->name('modules.admin');
    });
    Route::get('dashboard', [C\DashboardController::class, 'index'])->name('dashboard');
    Route::get('profile', [C\AuthController::class, 'profile'])->name('profile');
    Route::put('profile', [C\AuthController::class, 'updateProfile'])->name('profile.update');

    // Generic CRUD engine (masters + simple logs)
    Route::get('app/{key}', [C\CrudController::class, 'index'])->name('crud.index');
    Route::get('app/{key}/create', [C\CrudController::class, 'create'])->name('crud.create');
    Route::get('app/{key}/schema', [C\CrudController::class, 'schema'])->name('crud.schema');
    Route::post('app/{key}', [C\CrudController::class, 'store'])->name('crud.store');
    Route::get('app/{key}/{id}', [C\CrudController::class, 'show'])->name('crud.show');
    Route::get('app/{key}/{id}/edit', [C\CrudController::class, 'edit'])->name('crud.edit');
    Route::put('app/{key}/{id}', [C\CrudController::class, 'update'])->name('crud.update');
    Route::delete('app/{key}/{id}', [C\CrudController::class, 'destroy'])->name('crud.destroy');
    Route::post('app/{key}/{id}/transition/{action}', [C\CrudController::class, 'transition'])->name('crud.transition');

    // Document controllers (index/show reuse the CRUD engine views)
    Route::post('production-entries/{id}/approve', [C\ProductionEntryController::class, 'approve'])->name('production-entries.approve');
    Route::resource('production-entries', C\ProductionEntryController::class)->except(['edit', 'update'])->parameters(['production-entries' => 'id']);
    Route::get('production-entries/{id}/edit', [C\ProductionEntryController::class, 'edit'])->name('production-entries.edit');
    Route::put('production-entries/{id}', [C\ProductionEntryController::class, 'update'])->name('production-entries.update');

    Route::post('stock-adjustments/{id}/post', [C\StockAdjustmentController::class, 'post'])->name('stock-adjustments.post');
    Route::resource('stock-adjustments', C\StockAdjustmentController::class)->except(['edit', 'update'])->parameters(['stock-adjustments' => 'id']);
    Route::get('stock-adjustments/{id}/edit', [C\StockAdjustmentController::class, 'edit'])->name('stock-adjustments.edit');
    Route::put('stock-adjustments/{id}', [C\StockAdjustmentController::class, 'update'])->name('stock-adjustments.update');
    Route::get('stock-transactions', [C\StockTransactionController::class, 'index'])->name('stock-transactions.index');
    Route::get('stock-transactions/create', [C\StockTransactionController::class, 'create'])->name('stock-transactions.create');
    Route::post('stock-transactions', [C\StockTransactionController::class, 'store'])->name('stock-transactions.store');

    Route::post('maintenance-work-orders/{id}/start', [C\MaintenanceWorkOrderController::class, 'start'])->name('maintenance-work-orders.start');
    Route::post('maintenance-work-orders/{id}/complete', [C\MaintenanceWorkOrderController::class, 'complete'])->name('maintenance-work-orders.complete');
    Route::post('maintenance-work-orders/{id}/close', [C\MaintenanceWorkOrderController::class, 'close'])->name('maintenance-work-orders.close');
    Route::get('maintenance-work-orders/{id}/print', [C\MaintenanceWorkOrderController::class, 'print'])->name('maintenance-work-orders.print');
    Route::resource('maintenance-work-orders', C\MaintenanceWorkOrderController::class)->except(['edit', 'update'])->parameters(['maintenance-work-orders' => 'id']);
    Route::get('maintenance-work-orders/{id}/edit', [C\MaintenanceWorkOrderController::class, 'edit'])->name('maintenance-work-orders.edit');
    Route::put('maintenance-work-orders/{id}', [C\MaintenanceWorkOrderController::class, 'update'])->name('maintenance-work-orders.update');

    Route::get('weighbridge-tickets/{id}/close', [C\WeighbridgeTicketController::class, 'closeForm'])->name('weighbridge-tickets.close-form');
    Route::post('weighbridge-tickets/{id}/close', [C\WeighbridgeTicketController::class, 'close'])->name('weighbridge-tickets.close');
    Route::get('weighbridge-tickets/{id}/print', [C\WeighbridgeTicketController::class, 'print'])->name('weighbridge-tickets.print');
    Route::resource('weighbridge-tickets', C\WeighbridgeTicketController::class)->only(['index', 'show', 'create', 'store', 'destroy'])->parameters(['weighbridge-tickets' => 'id']);

    Route::post('quotations/{id}/send', [C\QuotationController::class, 'send'])->name('quotations.send');
    Route::post('quotations/{id}/convert', [C\QuotationController::class, 'convert'])->name('quotations.convert');
    Route::get('quotations/{id}/print', [C\QuotationController::class, 'print'])->name('quotations.print');
    Route::resource('quotations', C\QuotationController::class)->parameters(['quotations' => 'id']);

    Route::post('sales-orders/{id}/cancel', [C\SalesOrderController::class, 'cancel'])->name('sales-orders.cancel');
    Route::get('sales-orders/{id}/print', [C\SalesOrderController::class, 'print'])->name('sales-orders.print');
    Route::resource('sales-orders', C\SalesOrderController::class)->parameters(['sales-orders' => 'id']);

    Route::get('delivery-notes/{id}/invoice', [C\DeliveryNoteController::class, 'invoice'])->name('delivery-notes.invoice');
    Route::get('delivery-notes/{id}/print', [C\DeliveryNoteController::class, 'print'])->name('delivery-notes.print');
    Route::resource('delivery-notes', C\DeliveryNoteController::class)->only(['index', 'show', 'create', 'store', 'destroy'])->parameters(['delivery-notes' => 'id']);

    Route::post('sales-invoices/{id}/post', [C\SalesInvoiceController::class, 'post'])->name('sales-invoices.post');
    Route::get('sales-invoices/{id}/print', [C\SalesInvoiceController::class, 'print'])->name('sales-invoices.print');
    Route::resource('sales-invoices', C\SalesInvoiceController::class)->parameters(['sales-invoices' => 'id']);

    Route::get('customer-payments/{id}/print', [C\CustomerPaymentController::class, 'print'])->name('customer-payments.print');
    Route::resource('customer-payments', C\CustomerPaymentController::class)->only(['index', 'show', 'create', 'store'])->parameters(['customer-payments' => 'id']);

    Route::post('sales-credit-notes/{id}/approve', [C\SalesCreditNoteController::class, 'approve'])->name('sales-credit-notes.approve');
    Route::resource('sales-credit-notes', C\SalesCreditNoteController::class)->only(['index', 'show', 'create', 'store', 'destroy'])->parameters(['sales-credit-notes' => 'id']);

    Route::post('purchase-requisitions/{id}/convert', [C\PurchaseRequisitionController::class, 'convert'])->name('purchase-requisitions.convert');
    Route::resource('purchase-requisitions', C\PurchaseRequisitionController::class)->parameters(['purchase-requisitions' => 'id']);

    Route::post('purchase-orders/{id}/submit', [C\PurchaseOrderController::class, 'submit'])->name('purchase-orders.submit');
    Route::get('purchase-orders/{id}/receive', [C\PurchaseOrderController::class, 'receive'])->name('purchase-orders.receive');
    Route::get('purchase-orders/{id}/print', [C\PurchaseOrderController::class, 'print'])->name('purchase-orders.print');
    Route::resource('purchase-orders', C\PurchaseOrderController::class)->parameters(['purchase-orders' => 'id']);

    Route::post('goods-receipts/{id}/post', [C\GoodsReceiptController::class, 'post'])->name('goods-receipts.post');
    Route::get('goods-receipts/{id}/bill', [C\GoodsReceiptController::class, 'bill'])->name('goods-receipts.bill');
    Route::get('goods-receipts/{id}/print', [C\GoodsReceiptController::class, 'print'])->name('goods-receipts.print');
    Route::resource('goods-receipts', C\GoodsReceiptController::class)->only(['index', 'show', 'create', 'store', 'destroy'])->parameters(['goods-receipts' => 'id']);

    Route::post('purchase-invoices/{id}/post', [C\PurchaseInvoiceController::class, 'post'])->name('purchase-invoices.post');
    Route::resource('purchase-invoices', C\PurchaseInvoiceController::class)->parameters(['purchase-invoices' => 'id']);
    Route::resource('supplier-payments', C\SupplierPaymentController::class)->only(['index', 'show', 'create', 'store'])->parameters(['supplier-payments' => 'id']);

    Route::resource('subcontractor-claims', C\SubcontractorClaimController::class)->parameters(['subcontractor-claims' => 'id']);
    Route::resource('client-ipcs', C\ClientIpcController::class)->parameters(['client-ipcs' => 'id']);

    Route::post('payroll-runs/{id}/approve', [C\PayrollRunController::class, 'approve'])->name('payroll-runs.approve');
    Route::post('payroll-runs/{id}/pay', [C\PayrollRunController::class, 'pay'])->name('payroll-runs.pay');
    Route::get('payroll-runs/{id}/payslip/{lineId}', [C\PayrollRunController::class, 'payslip'])->name('payroll-runs.payslip');
    Route::resource('payroll-runs', C\PayrollRunController::class)->only(['index', 'show', 'create', 'store', 'destroy'])->parameters(['payroll-runs' => 'id']);

    Route::post('journals/{id}/post', [C\JournalController::class, 'post'])->name('journals.post');
    Route::post('journals/{id}/reverse', [C\JournalController::class, 'reverse'])->name('journals.reverse');
    Route::get('journals/{id}/print', [C\JournalController::class, 'print'])->name('journals.print');
    Route::resource('journals', C\JournalController::class)->parameters(['journals' => 'id']);
    Route::resource('depreciation-runs', C\DepreciationController::class)->only(['index','store','show'])->parameters(['depreciation-runs' => 'id']);
    Route::resource('budget-virements', C\BudgetVirementController::class)->only(['index','create','store','show'])->parameters(['budget-virements' => 'id']);

    Route::get('bank-transactions/import', [C\BankImportController::class, 'create'])->name('bank-import.create');
    Route::post('bank-transactions/import', [C\BankImportController::class, 'parse'])->name('bank-import.parse');
    Route::post('bank-transactions/import/confirm', [C\BankImportController::class, 'confirm'])->name('bank-import.confirm');
    Route::post('bank-transactions/{id}/reconcile', [C\BankTransactionController::class, 'reconcile'])->name('bank-transactions.reconcile');
    Route::resource('bank-transactions', C\BankTransactionController::class)->only(['index', 'show', 'create', 'store'])->parameters(['bank-transactions' => 'id']);

    Route::post('expense-claims/{id}/pay', [C\ExpenseClaimController::class, 'pay'])->name('expense-claims.pay');
    Route::resource('expense-claims', C\ExpenseClaimController::class)->parameters(['expense-claims' => 'id']);

    Route::resource('roles', C\RoleController::class)->except(['show']);
    Route::get('users/{id}/logins', [C\UserController::class, 'logins'])->name('users.logins');
    Route::resource('users', C\UserController::class)->except(['show']);

    Route::get('approvals', [C\ApprovalController::class, 'index'])->name('approvals.index');
    Route::post('approvals/{id}', [C\ApprovalController::class, 'decide'])->name('approvals.decide');

    Route::get('notifications', [C\NotificationController::class, 'index'])->name('notifications.index');
    Route::post('notifications/read-all', [C\NotificationController::class, 'readAll'])->name('notifications.read-all');
    Route::get('notifications/{id}/open', [C\NotificationController::class, 'open'])->name('notifications.open');
    Route::get('search', [C\SearchController::class, 'query'])->name('search');
    Route::get('fiscal-years', [C\FiscalYearController::class, 'index'])->name('fiscal-years.index');
    Route::post('fiscal-years/{id}/close', [C\FiscalYearController::class, 'close'])->name('fiscal-years.close');
    Route::post('fiscal-years/{id}/reopen', [C\FiscalYearController::class, 'reopen'])->name('fiscal-years.reopen');
    Route::get('reports', [C\ReportController::class, 'index'])->name('reports.index');
    Route::get('reports/{slug}', [C\ReportController::class, 'show'])->name('reports.show');
    Route::get('docs-library', [C\DocumentLibraryController::class, 'index'])->name('docs-library.index');
    Route::get('docs-library/create', [C\DocumentLibraryController::class, 'create'])->name('docs-library.create');
    Route::post('docs-library', [C\DocumentLibraryController::class, 'store'])->name('docs-library.store');
    Route::get('docs-library/{id}/download', [C\DocumentLibraryController::class, 'download'])->name('docs-library.download');
    Route::delete('docs-library/{id}', [C\DocumentLibraryController::class, 'destroy'])->name('docs-library.destroy');

    Route::get('settings/company', [C\SettingsController::class, 'company'])->name('settings.company');
    Route::put('settings/company', [C\SettingsController::class, 'companyUpdate'])->name('settings.company.update');
    Route::get('settings/preferences', [C\SettingsController::class, 'prefs'])->name('settings.prefs');
    Route::put('settings/preferences', [C\SettingsController::class, 'prefsUpdate'])->name('settings.prefs.update');
    Route::get('settings/backup', [C\SettingsController::class, 'backup'])->name('settings.backup');
});
