<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\AuthController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\ProductController;
use App\Http\Controllers\Admin\CategoryController;
use App\Http\Controllers\Admin\BrandController;
use App\Http\Controllers\Admin\ColorController;
use App\Http\Controllers\Admin\SizeController;
use App\Http\Controllers\Admin\UnitController;
use App\Http\Controllers\Admin\SupplierController;
use App\Http\Controllers\Admin\PurchaseController;
use App\Http\Controllers\Admin\SaleController;
use App\Http\Controllers\Admin\SaleReturnController;
use App\Http\Controllers\Admin\SupplierReturnController;
use App\Http\Controllers\Admin\ConveyanceBillController;
use App\Http\Controllers\Admin\SettingsController;
use App\Http\Controllers\Admin\BarcodeController;
use App\Http\Controllers\Admin\CustomerController;
use App\Http\Controllers\Admin\AccountController;
use App\Http\Controllers\Admin\ReportController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\Admin\InventoryController;
use App\Http\Controllers\Admin\BranchController;
use App\Http\Controllers\Admin\ServiceController;
use App\Http\Controllers\Admin\InstallmentController;
use App\Http\Controllers\Admin\DealerController;
use App\Http\Controllers\Admin\StaffController;
use App\Http\Controllers\Admin\SecurityController;
use App\Http\Controllers\Admin\AutomationController;

Route::get('/', fn () => redirect()->route('admin.login'));

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

Route::middleware(['web', 'admin.auth'])->prefix('admin')->name('admin.')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard')->middleware('admin.can:dashboard');

    Route::resource('products', ProductController::class)->except(['show'])->middleware('admin.can:products');
    Route::get('/barcodes', [BarcodeController::class, 'index'])->name('barcodes.index')->middleware('admin.can:products');
    Route::get('/barcodes/{id}/print', [BarcodeController::class, 'print'])->name('barcodes.print')->middleware('admin.can:products');

    Route::resource('categories', CategoryController::class)->except(['show'])->middleware('admin.can:products');
    Route::resource('brands', BrandController::class)->except(['show'])->middleware('admin.can:products');
    Route::resource('colors', ColorController::class)->except(['show'])->middleware('admin.can:products');
    Route::resource('sizes', SizeController::class)->except(['show'])->middleware('admin.can:products');
    Route::resource('units', UnitController::class)->except(['show'])->middleware('admin.can:products');
    Route::resource('suppliers', SupplierController::class)->except(['show'])->middleware('admin.can:purchases');

    Route::get('suppliers/{supplier}/ledger', [SupplierController::class, 'ledger'])->name('suppliers.ledger')->middleware('admin.can:purchases');
    Route::get('suppliers/{supplier}/statement', [SupplierController::class, 'statement'])->name('suppliers.statement')->middleware('admin.can:purchases');

    Route::resource('customers', CustomerController::class)->middleware('admin.can:customers');

    Route::get('purchases', [PurchaseController::class, 'index'])->name('purchases.index')->middleware('admin.can:purchases');
    Route::get('purchases/create', [PurchaseController::class, 'create'])->name('purchases.create')->middleware('admin.can:purchases');
    Route::post('purchases', [PurchaseController::class, 'store'])->name('purchases.store')->middleware('admin.can:purchases');
    Route::get('purchases/{purchase}', [PurchaseController::class, 'show'])->name('purchases.show')->middleware('admin.can:purchases');
    Route::get('purchases/{purchase}/edit', [PurchaseController::class, 'edit'])->name('purchases.edit')->middleware('admin.can:purchases');
    Route::put('purchases/{purchase}', [PurchaseController::class, 'update'])->name('purchases.update')->middleware('admin.can:purchases');
    Route::delete('purchases/{purchase}', [PurchaseController::class, 'destroy'])->name('purchases.destroy')->middleware('admin.can:purchases');

    Route::get('sales', [SaleController::class, 'index'])->name('sales.index')->middleware('admin.can:sales');
    Route::get('sales/create', [SaleController::class, 'create'])->name('sales.create')->middleware('admin.can:sales');
    Route::post('sales', [SaleController::class, 'store'])->name('sales.store')->middleware('admin.can:sales');
    Route::get('sales/{sale}/invoice', [SaleController::class, 'invoice'])->name('sales.invoice')->middleware('admin.can:sales');
    Route::get('sales/invoice/{invoiceNo}/edit', [SaleController::class, 'edit'])->name('sales.edit')->middleware('admin.can:sales');
    Route::put('sales/invoice/{invoiceNo}', [SaleController::class, 'update'])->name('sales.update')->middleware('admin.can:sales');
    Route::delete('sales/invoice/{invoiceNo}', [SaleController::class, 'destroy'])->name('sales.destroy')->middleware('admin.can:sales');
    Route::get('pos', [SaleController::class, 'create'])->name('pos.create')->middleware('admin.can:sales');
    Route::get('all-sales', [SaleController::class, 'index'])->name('all-sales.index')->middleware('admin.can:sales');

    Route::get('sales-holds', [SaleController::class, 'holds'])->name('sales.holds')->middleware('admin.can:sales');
    Route::get('sales-holds/{hold}/resume', [SaleController::class, 'releaseHold'])->name('sales.holds.resume')->middleware('admin.can:sales');
    Route::delete('sales-holds/{hold}', [SaleController::class, 'removeHold'])->name('sales.holds.delete')->middleware('admin.can:sales');
    Route::match(['get','post'], 'cashier-shifts', [SaleController::class, 'shifts'])->name('sales.shifts')->middleware('admin.can:sales');

    Route::get('sale-returns', [SaleReturnController::class, 'index'])->name('sale-returns.index')->middleware('admin.can:returns');
    Route::get('sale-returns/create', [SaleReturnController::class, 'create'])->name('sale-returns.create')->middleware('admin.can:returns');
    Route::post('sale-returns', [SaleReturnController::class, 'store'])->name('sale-returns.store')->middleware('admin.can:returns');
    Route::get('sale-returns/{saleReturn}', [SaleReturnController::class, 'show'])->name('sale-returns.show')->middleware('admin.can:returns');
    Route::delete('sale-returns/{saleReturn}', [SaleReturnController::class, 'destroy'])->name('sale-returns.destroy')->middleware('admin.can:returns');
    Route::get('customer-return', [SaleReturnController::class, 'create'])->name('customer-return.create')->middleware('admin.can:returns');
    Route::get('customer-returns', [SaleReturnController::class, 'index'])->name('customer-returns.index')->middleware('admin.can:returns');

    Route::get('supplier-returns', [SupplierReturnController::class, 'index'])->name('supplier-returns.index')->middleware('admin.can:returns');
    Route::get('supplier-returns/create', [SupplierReturnController::class, 'create'])->name('supplier-returns.create')->middleware('admin.can:returns');
    Route::post('supplier-returns', [SupplierReturnController::class, 'store'])->name('supplier-returns.store')->middleware('admin.can:returns');
    Route::get('supplier-returns/{supplierReturn}', [SupplierReturnController::class, 'show'])->name('supplier-returns.show')->middleware('admin.can:returns');
    Route::delete('supplier-returns/{supplierReturn}', [SupplierReturnController::class, 'destroy'])->name('supplier-returns.destroy')->middleware('admin.can:returns');
    Route::get('supplier-return', [SupplierReturnController::class, 'create'])->name('supplier-return.create')->middleware('admin.can:returns');

    Route::resource('expenses', ConveyanceBillController::class)->except(['show'])->middleware('admin.can:expenses');
    Route::get('expense-list', [ConveyanceBillController::class, 'index'])->name('expense-list.index')->middleware('admin.can:expenses');
    Route::get('add-expense', [ConveyanceBillController::class, 'create'])->name('add-expense.create')->middleware('admin.can:expenses');

    Route::get('accounts', [AccountController::class, 'index'])->name('accounts.index')->middleware('admin.can:accounts');
    Route::post('accounts', [AccountController::class, 'store'])->name('accounts.store')->middleware('admin.can:accounts');

    Route::get('reports', [ReportController::class, 'index'])->name('reports.index')->middleware('admin.can:reports');

    Route::get('inventory', [InventoryController::class, 'index'])->name('inventory.index')->middleware('admin.can:inventory');
    Route::get('inventory/adjustments', [InventoryController::class, 'adjustmentForm'])->name('inventory.adjustments')->middleware('admin.can:inventory');
    Route::post('inventory/adjustments', [InventoryController::class, 'storeAdjustment'])->name('inventory.adjustments.store')->middleware('admin.can:inventory');
    Route::get('inventory/transfers', [InventoryController::class, 'transferForm'])->name('inventory.transfers')->middleware('admin.can:inventory');
    Route::post('inventory/transfers', [InventoryController::class, 'storeTransfer'])->name('inventory.transfers.store')->middleware('admin.can:inventory');

    Route::get('users', [UserController::class, 'index'])->name('users.index')->middleware('admin.can:users');
    Route::post('users', [UserController::class, 'store'])->name('users.store')->middleware('admin.can:users');
    Route::put('users/{user}', [UserController::class, 'update'])->name('users.update')->middleware('admin.can:users');
    Route::delete('users/{user}', [UserController::class, 'destroy'])->name('users.destroy')->middleware('admin.can:users');



    Route::get('branches', [BranchController::class, 'index'])->name('branches.index')->middleware('admin.can:branches');
    Route::post('branches', [BranchController::class, 'store'])->name('branches.store')->middleware('admin.can:branches');
    Route::put('branches/{branch}', [BranchController::class, 'update'])->name('branches.update')->middleware('admin.can:branches');
    Route::delete('branches/{branch}', [BranchController::class, 'destroy'])->name('branches.destroy')->middleware('admin.can:branches');
    Route::get('branches/stocks', [BranchController::class, 'stocks'])->name('branches.stocks')->middleware('admin.can:branches');
    Route::post('branches/stocks', [BranchController::class, 'saveStock'])->name('branches.stocks.save')->middleware('admin.can:branches');
    Route::get('branches/performance', [BranchController::class, 'performance'])->name('branches.performance')->middleware('admin.can:branches');

    Route::get('service', [ServiceController::class, 'index'])->name('service.index')->middleware('admin.can:service');
    Route::post('service/warranty', [ServiceController::class, 'storeWarranty'])->name('service.warranty.store')->middleware('admin.can:service');
    Route::post('service/claim', [ServiceController::class, 'storeClaim'])->name('service.claim.store')->middleware('admin.can:service');
    Route::get('service/warranty-check', [ServiceController::class, 'warrantyCheck'])->name('service.warranty.check')->middleware('admin.can:service');

    Route::get('installments', [InstallmentController::class, 'index'])->name('installments.index')->middleware('admin.can:installments');
    Route::post('installments', [InstallmentController::class, 'store'])->name('installments.store')->middleware('admin.can:installments');
    Route::post('installments/{plan}/collect', [InstallmentController::class, 'collect'])->name('installments.collect')->middleware('admin.can:installments');
    Route::get('installments/defaulters', [InstallmentController::class, 'defaulters'])->name('installments.defaulters')->middleware('admin.can:installments');

    Route::get('dealers', [DealerController::class, 'index'])->name('dealers.index')->middleware('admin.can:dealers');
    Route::post('dealers', [DealerController::class, 'store'])->name('dealers.store')->middleware('admin.can:dealers');
    Route::put('dealers/{dealer}', [DealerController::class, 'update'])->name('dealers.update')->middleware('admin.can:dealers');
    Route::match(['get','post'],'dealers/{dealer}/ledger', [DealerController::class, 'ledger'])->name('dealers.ledger')->middleware('admin.can:dealers');

    Route::get('staff', [StaffController::class, 'index'])->name('staff.index')->middleware('admin.can:hr');
    Route::post('staff', [StaffController::class, 'store'])->name('staff.store')->middleware('admin.can:hr');
    Route::match(['get','post'], 'staff/attendance', [StaffController::class, 'attendance'])->name('staff.attendance')->middleware('admin.can:hr');
    Route::match(['get','post'], 'staff/salary', [StaffController::class, 'salary'])->name('staff.salary')->middleware('admin.can:hr');

    Route::get('security', [SecurityController::class, 'index'])->name('security.index')->middleware('admin.can:security');
    Route::post('security/2fa/{admin}', [SecurityController::class, 'save2fa'])->name('security.2fa')->middleware('admin.can:security');
    Route::post('security/snapshot', [SecurityController::class, 'snapshot'])->name('security.snapshot')->middleware('admin.can:security');
    Route::post('security/restore/{snapshot}', [SecurityController::class, 'restore'])->name('security.restore')->middleware('admin.can:security');

    Route::get('automation', [AutomationController::class, 'index'])->name('automation.index')->middleware('admin.can:automation');
    Route::post('automation/run', [AutomationController::class, 'run'])->name('automation.run')->middleware('admin.can:automation');

    Route::get('settings/company', [SettingsController::class, 'company'])->name('settings.company')->middleware('admin.can:settings');
    Route::post('settings/company', [SettingsController::class, 'saveCompany'])->name('settings.company.save')->middleware('admin.can:settings');
    Route::get('settings/printer', [SettingsController::class, 'printer'])->name('settings.printer')->middleware('admin.can:settings');
    Route::post('settings/printer', [SettingsController::class, 'savePrinter'])->name('settings.printer.save')->middleware('admin.can:settings');
    Route::get('settings/integrations', [SettingsController::class, 'integrations'])->name('settings.integrations')->middleware('admin.can:settings');
    Route::post('settings/integrations', [SettingsController::class, 'saveIntegrations'])->name('settings.integrations.save')->middleware('admin.can:settings');
    Route::get('company-profile', [SettingsController::class, 'company'])->name('company-profile')->middleware('admin.can:settings');
    Route::get('print-settings', [SettingsController::class, 'printer'])->name('print-settings')->middleware('admin.can:settings');
});
