<?php

use App\Http\Controllers\AuthController;
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\CreditController;
use App\Http\Controllers\CustomerController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\ExpenseController;
use App\Http\Controllers\ExpiryController;
use App\Http\Controllers\InventoryController;
use App\Http\Controllers\MpesaController;
use App\Http\Controllers\OpeningBalanceController;
use App\Http\Controllers\OrderController;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\SettingsController;
use App\Http\Controllers\SupplierController;
use App\Http\Controllers\TaxController;
use App\Http\Controllers\UnitOfMeasureController;
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return redirect()->route('login');
});

Route::get('/login', [AuthController::class, 'showLoginForm'])->name('login');
Route::post('/login', [AuthController::class, 'login']);
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

Route::middleware(['auth'])->group(function () {
    // Auth
    Route::post('/auto-logout', [AuthController::class, 'autoLogout'])->name('auto.logout');

    // Settings
    Route::post('/settings/theme', [SettingsController::class, 'theme'])->name('settings.theme');
    Route::get('/settings', [SettingsController::class, 'index'])->name('settings.index');
    Route::post('/settings', [SettingsController::class, 'update'])->name('settings.update');
    Route::get('/settings/sms', [SettingsController::class, 'smsIndex'])->name('settings.sms');
    Route::post('/settings/sms', [SettingsController::class, 'smsSave'])->name('settings.sms.save');
    Route::post('/settings/sms/test', [SettingsController::class, 'smsTest'])->name('settings.sms.test');
    Route::post('/settings/sms/clear-cache', [SettingsController::class, 'smsClearCache'])->name('settings.sms.clear-cache');
    Route::get('/settings/system', [SettingsController::class, 'systemIndex'])->name('settings.system');
    Route::post('/settings/system', [SettingsController::class, 'systemUpdate'])->name('settings.system.update');

    // Dashboard
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    // Categories
    Route::get('/categories', [CategoryController::class, 'index'])->name('categories.index');
    Route::get('/categories/create', [CategoryController::class, 'create'])->name('categories.create');
    Route::post('/categories', [CategoryController::class, 'store'])->name('categories.store');
    Route::get('/categories/{category}', [CategoryController::class, 'show'])->name('categories.show');
    Route::get('/categories/{category}/edit', [CategoryController::class, 'edit'])->name('categories.edit');
    Route::put('/categories/{category}', [CategoryController::class, 'update'])->name('categories.update');
    Route::delete('/categories/{category}', [CategoryController::class, 'destroy'])->name('categories.destroy');
    Route::patch('/categories/{category}/toggle', [CategoryController::class, 'toggleStatus'])->name('categories.toggle');

    // Unit of Measures
    Route::get('/unit-of-measures', [UnitOfMeasureController::class, 'index'])->name('unit-of-measures.index');
    Route::get('/unit-of-measures/create', [UnitOfMeasureController::class, 'create'])->name('unit-of-measures.create');
    Route::post('/unit-of-measures', [UnitOfMeasureController::class, 'store'])->name('unit-of-measures.store');
    Route::get('/unit-of-measures/{unitOfMeasure}', [UnitOfMeasureController::class, 'show'])->name('unit-of-measures.show');
    Route::get('/unit-of-measures/{unitOfMeasure}/edit', [UnitOfMeasureController::class, 'edit'])->name('unit-of-measures.edit');
    Route::put('/unit-of-measures/{unitOfMeasure}', [UnitOfMeasureController::class, 'update'])->name('unit-of-measures.update');
    Route::delete('/unit-of-measures/{unitOfMeasure}', [UnitOfMeasureController::class, 'destroy'])->name('unit-of-measures.destroy');
    Route::patch('/unit-of-measures/{unitOfMeasure}/toggle', [UnitOfMeasureController::class, 'toggleStatus'])->name('unit-of-measures.toggle');

    // Products
    Route::get('/products', [ProductController::class, 'index'])->name('products.index');
    Route::get('/products/create', [ProductController::class, 'create'])->name('products.create');
    Route::post('/products', [ProductController::class, 'store'])->name('products.store');
    Route::get('/products/search', [ProductController::class, 'search'])->name('products.search');
    Route::get('/products/find-barcode', [ProductController::class, 'findByBarcode'])->name('products.findBarcode');
    Route::get('/products/generate-barcode', [ProductController::class, 'generateBarcode'])->name('products.generateBarcode');
    Route::post('/products/bulk-delete', [ProductController::class, 'bulkDestroy'])->name('products.bulkDestroy');
    Route::get('/products/{product}', [ProductController::class, 'show'])->name('products.show');
    Route::get('/products/{product}/edit', [ProductController::class, 'edit'])->name('products.edit');
    Route::put('/products/{product}', [ProductController::class, 'update'])->name('products.update');
    Route::delete('/products/{product}', [ProductController::class, 'destroy'])->name('products.destroy');
    Route::patch('/products/{product}/toggle', [ProductController::class, 'toggleStatus'])->name('products.toggle');

    // Customers
    Route::get('/customers', [CustomerController::class, 'index'])->name('customers.index');
    Route::get('/customers/create', [CustomerController::class, 'create'])->name('customers.create');
    Route::post('/customers', [CustomerController::class, 'store'])->name('customers.store');
    Route::get('/customers/search', [CustomerController::class, 'search'])->name('customers.search');
    Route::get('/customers/{customer}', [CustomerController::class, 'show'])->name('customers.show');
    Route::get('/customers/{customer}/edit', [CustomerController::class, 'edit'])->name('customers.edit');
    Route::put('/customers/{customer}', [CustomerController::class, 'update'])->name('customers.update');
    Route::delete('/customers/{customer}', [CustomerController::class, 'destroy'])->name('customers.destroy');
    Route::patch('/customers/{customer}/toggle', [CustomerController::class, 'toggleStatus'])->name('customers.toggle');

    // Suppliers
    Route::get('/suppliers', [SupplierController::class, 'index'])->name('suppliers.index');
    Route::get('/suppliers/create', [SupplierController::class, 'create'])->name('suppliers.create');
    Route::post('/suppliers', [SupplierController::class, 'store'])->name('suppliers.store');
    Route::get('/suppliers/finance', [SupplierController::class, 'finance'])->name('suppliers.finance');
    Route::get('/suppliers/{supplier}', [SupplierController::class, 'show'])->name('suppliers.show');
    Route::get('/suppliers/{supplier}/edit', [SupplierController::class, 'edit'])->name('suppliers.edit');
    Route::put('/suppliers/{supplier}', [SupplierController::class, 'update'])->name('suppliers.update');
    Route::delete('/suppliers/{supplier}', [SupplierController::class, 'destroy'])->name('suppliers.destroy');
    Route::patch('/suppliers/{supplier}/toggle', [SupplierController::class, 'toggleStatus'])->name('suppliers.toggle');
    Route::post('/suppliers/{supplier}/payment', [SupplierController::class, 'recordPayment'])->name('suppliers.payment');

    // POS & Orders
    Route::get('/pos', [OrderController::class, 'home'])->name('pos.index');
    Route::get('/pos/sales', [OrderController::class, 'sales'])->name('pos.sales');
    Route::post('/pos', [OrderController::class, 'store'])->name('pos.store');
    Route::get('/pos/products', [OrderController::class, 'products'])->name('pos.products');
    Route::get('/opening-balance', [OpeningBalanceController::class, 'index'])->name('opening-balance.index');
    Route::post('/opening-balance', [OpeningBalanceController::class, 'store'])->name('opening-balance.store');
    Route::get('/opening-balance/report', [OpeningBalanceController::class, 'report'])->name('opening-balance.report')->middleware('auth');
    Route::put('/opening-balance/{openingBalance}', [OpeningBalanceController::class, 'update'])->name('opening-balance.update');
    Route::delete('/opening-balance/{openingBalance}', [OpeningBalanceController::class, 'destroy'])->name('opening-balance.destroy');
    Route::get('/orders', [OrderController::class, 'index'])->name('orders.index');
    Route::get('/orders/{order}', [OrderController::class, 'show'])->name('orders.show');
    Route::delete('/orders/{order}', [OrderController::class, 'destroy'])->name('orders.destroy');
    Route::get('/orders/{order}/receipt', [OrderController::class, 'receipt'])->name('orders.receipt');
    Route::get('/orders/{order}/receipt-pdf', [OrderController::class, 'receiptPdf'])->name('orders.receipt.pdf');

    // Inventory
    Route::get('/inventory', [InventoryController::class, 'index'])->name('inventory.index');
    Route::get('/inventory/stock-in', [InventoryController::class, 'stockInForm'])->name('inventory.stock-in');
    Route::post('/inventory/stock-in', [InventoryController::class, 'stockIn'])->name('inventory.stock-in');
    Route::get('/inventory/last-cost', [InventoryController::class, 'getLastCost'])->name('inventory.last-cost');
    Route::get('/inventory/supplier-balance', [InventoryController::class, 'getSupplierBalance'])->name('inventory.supplier-balance');
    Route::get('/inventory/adjustment', [InventoryController::class, 'adjustmentForm'])->name('inventory.adjustment');
    Route::post('/inventory/adjustment', [InventoryController::class, 'adjustment'])->name('inventory.adjustment');
    Route::get('/inventory/report', [InventoryController::class, 'report'])->name('inventory.report');
    Route::get('/inventory/price-adjustment', [InventoryController::class, 'priceAdjustmentForm'])->name('inventory.price-adjustment');
    Route::post('/inventory/price-adjustment', [InventoryController::class, 'priceAdjustment'])->name('inventory.price-adjustment');
    Route::get('/inventory/product-price-history', [InventoryController::class, 'productPriceHistory'])->name('inventory.product-price-history');
    Route::get('/inventory/product-price-history/export', [InventoryController::class, 'exportProductPriceHistory'])->name('inventory.product-price-history.export');
    Route::get('/inventory/reconciliation', [InventoryController::class, 'reconciliation'])->name('inventory.reconciliation');
    Route::post('/inventory/reconciliation', [InventoryController::class, 'saveReconciliation'])->name('inventory.reconciliation.save');

    // Expenses
    Route::get('/expenses', [ExpenseController::class, 'index'])->name('expenses.index');
    Route::get('/expenses/create', [ExpenseController::class, 'create'])->name('expenses.create');
    Route::post('/expenses', [ExpenseController::class, 'store'])->name('expenses.store');
    Route::delete('/expenses/{expense}', [ExpenseController::class, 'destroy'])->name('expenses.destroy');
    Route::get('/expenses/cashflow', [ExpenseController::class, 'cashflow'])->name('expenses.cashflow');
    Route::get('/expenses/cashflow/export', [ExpenseController::class, 'cashflowExport'])->name('expenses.cashflow.export');

    // Reports
    Route::get('/reports/sales', [ReportController::class, 'sales'])->name('reports.sales');
    Route::get('/reports/sales/export', [ReportController::class, 'salesExport'])->name('reports.sales.export');
    Route::get('/reports/profit', [ReportController::class, 'profit'])->name('reports.profit');
    Route::get('/reports/profit/export', [ReportController::class, 'profitExport'])->name('reports.profit.export');
    Route::get('/reports/profit/pdf', [ReportController::class, 'profitPdf'])->name('reports.profit.pdf');
    Route::get('/reports/inventory', [ReportController::class, 'inventory'])->name('reports.inventory');
    Route::get('/reports/inventory/export', [ReportController::class, 'inventoryExport'])->name('reports.inventory.export');
    Route::get('/reports/stock-value', [ReportController::class, 'stockValue'])->name('reports.stock-value');
    Route::get('/reports/stock-value/export', [ReportController::class, 'stockValueExport'])->name('reports.stock-value.export');
    Route::get('/reports/supplier-payments', [ReportController::class, 'supplierPayments'])->name('reports.supplier-payments');
    Route::get('/reports/supplier-payments/export', [ReportController::class, 'supplierPaymentsExport'])->name('reports.supplier-payments.export');
    Route::get('/reports/opening-balance', [ReportController::class, 'openingBalance'])->name('reports.opening-balance');
    Route::get('/reports/opening-balance/export', [ReportController::class, 'openingBalanceExport'])->name('reports.opening-balance.export');
    Route::get('/reports/stock-movement', [ReportController::class, 'stockMovement'])->name('reports.stock-movement');
    Route::get('/reports/stock-movement/export', [ReportController::class, 'stockMovementExport'])->name('reports.stock-movement.export');
    Route::get('/reports/cashier', [ReportController::class, 'cashierReport'])->name('reports.cashier');
    Route::get('/reports/cashier/export', [ReportController::class, 'cashierReportExport'])->name('reports.cashier.export');
    Route::get('/reports/cashier-sales', [ReportController::class, 'cashierSales'])->name('reports.cashier-sales');
    Route::get('/reports/cashier-sales/export', [ReportController::class, 'cashierSalesExport'])->name('reports.cashier-sales.export');
    Route::get('/reports/disposed', [ReportController::class, 'disposedProducts'])->name('reports.disposed');
    Route::get('/reports/disposed/export', [ReportController::class, 'disposedProductsExport'])->name('reports.disposed.export');
    Route::get('/reports/reconciliation', [ReportController::class, 'reconciliationReport'])->name('reports.reconciliation');
    Route::get('/reports/reconciliation/export', [ReportController::class, 'reconciliationReportExport'])->name('reports.reconciliation.export');
    Route::get('/reports/mpesa-transactions', [ReportController::class, 'mpesaTransactions'])->name('reports.mpesa-transactions');
    Route::get('/reports/mpesa-transactions/export', [ReportController::class, 'mpesaTransactionsExport'])->name('reports.mpesa-transactions.export');

    // Users
    Route::get('/users', [UserController::class, 'index'])->name('users.index');
    Route::get('/users/create', [UserController::class, 'create'])->name('users.create');
    Route::post('/users', [UserController::class, 'store'])->name('users.store');
    Route::get('/users/{user}/edit', [UserController::class, 'edit'])->name('users.edit');
    Route::put('/users/{user}', [UserController::class, 'update'])->name('users.update');
    Route::delete('/users/{user}', [UserController::class, 'destroy'])->name('users.destroy');
    Route::patch('/users/{user}/toggle', [UserController::class, 'toggleStatus'])->name('users.toggle');
    Route::get('/users/{user}/permissions', [UserController::class, 'permissions'])->name('users.permissions');
    Route::put('/users/{user}/permissions', [UserController::class, 'updatePermissions'])->name('users.permissions.update');

    // Tax Management
    Route::get('/tax', [TaxController::class, 'index'])->name('tax.index');
    Route::post('/tax', [TaxController::class, 'store'])->name('tax.store');
    Route::put('/tax/{taxConfig}', [TaxController::class, 'update'])->name('tax.update');
    Route::delete('/tax/{taxConfig}', [TaxController::class, 'destroy'])->name('tax.destroy');
    Route::patch('/tax/{taxConfig}/toggle', [TaxController::class, 'toggle'])->name('tax.toggle');
    Route::get('/tax/rates', [TaxController::class, 'getTaxRates'])->name('tax.rates');
    Route::post('/tax/calculate', [TaxController::class, 'calculateTax'])->name('tax.calculate');

    // e-TIMS Configuration
    Route::get('/tax/etims', [TaxController::class, 'etimsIndex'])->name('tax.etims');
    Route::post('/tax/etims', [TaxController::class, 'etimsStore'])->name('tax.etims.store');
    Route::get('/tax/etims/invoices', [TaxController::class, 'etimsInvoices'])->name('tax.etims.invoices');

    // Expiry Management
    Route::get('/expiry', [ExpiryController::class, 'index'])->name('expiry.index');
    Route::put('/expiry/{product}', [ExpiryController::class, 'update'])->name('expiry.update');
    Route::post('/expiry/{product}/mark-expired', [ExpiryController::class, 'markAsExpired'])->name('expiry.mark-expired');
    Route::post('/expiry/{product}/extend', [ExpiryController::class, 'extend'])->name('expiry.extend');
    Route::post('/expiry/{product}/dispose', [ExpiryController::class, 'dispose'])->name('expiry.dispose');

    // M-Pesa Configuration
    Route::get('/mpesa', [MpesaController::class, 'index'])->name('mpesa.index');
    Route::post('/mpesa', [MpesaController::class, 'store'])->name('mpesa.store');
    Route::get('/mpesa/transactions', [MpesaController::class, 'transactions'])->name('mpesa.transactions');
    Route::post('/mpesa/stk-push', [MpesaController::class, 'stkPush'])->name('mpesa.stk-push');
    Route::post('/mpesa/check-status', [MpesaController::class, 'checkStatus'])->name('mpesa.check-status');
    Route::post('/mpesa/callback', [MpesaController::class, 'callback'])->name('mpesa.callback');
    Route::post('/mpesa/manual-record', [MpesaController::class, 'manualRecord'])->name('mpesa.manual-record');

    // Credit Management
    Route::get('/credit', [CreditController::class, 'index'])->name('credit.index');
    Route::get('/credit/customer/{customer}', [CreditController::class, 'customerCredits'])->name('credit.customer');
    Route::post('/credit/payment', [CreditController::class, 'recordPayment'])->name('credit.payment');
    Route::post('/credit/reminder', [CreditController::class, 'sendReminder'])->name('credit.reminder');
    Route::post('/credit/reminder/bulk', [CreditController::class, 'sendBulkReminders'])->name('credit.reminder.bulk');
    Route::post('/credit/update-overdue', [CreditController::class, 'updateOverdueStatus'])->name('credit.update-overdue');
});
