<?php

use App\Http\Controllers\BankWithdrawalController;
use App\Http\Controllers\CurrencyController;
use App\Http\Controllers\ItemController;
use App\Http\Controllers\CustomerController;
use App\Http\Controllers\CustomerLedgerController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\EmployeeController;
use App\Http\Controllers\EmployeePaymentController;
use App\Http\Controllers\ExpenseController;
use App\Http\Controllers\LocaleController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\PurchaseController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\RoleController;
use App\Http\Controllers\RosterController;
use App\Http\Controllers\SaleController;
use App\Http\Controllers\SettingsController;
use App\Http\Controllers\ShareholderController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\VendorController;
use Illuminate\Support\Facades\Route;

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

Route::get('/locale/{locale}', [LocaleController::class, 'switch'])->name('locale.switch');

Route::middleware(['auth'])->group(function () {

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

    // ── SALES ──────────────────────────────────────────────────────────────────
    Route::middleware('permission:sales.view')->get('sales', [SaleController::class, 'index'])->name('sales.index');
    Route::middleware('permission:sales.create')->group(function () {
        Route::get('sales/create', [SaleController::class, 'create'])->name('sales.create');
        Route::post('sales', [SaleController::class, 'store'])->name('sales.store');
    });
    Route::middleware('permission:sales.delete')->delete('sales/{sale}', [SaleController::class, 'destroy'])->name('sales.destroy');

    // ── PURCHASES ──────────────────────────────────────────────────────────────
    Route::middleware('permission:purchases.view')->get('purchases', [PurchaseController::class, 'index'])->name('purchases.index');
    Route::middleware('permission:purchases.create')->group(function () {
        Route::get('purchases/create', [PurchaseController::class, 'create'])->name('purchases.create');
        Route::post('purchases', [PurchaseController::class, 'store'])->name('purchases.store');
    });
    Route::middleware('permission:purchases.delete')->delete('purchases/{purchase}', [PurchaseController::class, 'destroy'])->name('purchases.destroy');

    // ── EXPENSES ───────────────────────────────────────────────────────────────
    Route::middleware('permission:expenses.view')->get('expenses', [ExpenseController::class, 'index'])->name('expenses.index');
    Route::middleware('permission:expenses.create')->group(function () {
        Route::get('expenses/create', [ExpenseController::class, 'create'])->name('expenses.create');
        Route::post('expenses', [ExpenseController::class, 'store'])->name('expenses.store');
    });
    Route::middleware('permission:expenses.delete')->delete('expenses/{expense}', [ExpenseController::class, 'destroy'])->name('expenses.destroy');

    // ── BANK WITHDRAWALS ───────────────────────────────────────────────────────
    Route::middleware('permission:bank-withdrawals.view')->get('bank-withdrawals', [BankWithdrawalController::class, 'index'])->name('bank-withdrawals.index');
    Route::middleware('permission:bank-withdrawals.create')->group(function () {
        Route::get('bank-withdrawals/create', [BankWithdrawalController::class, 'create'])->name('bank-withdrawals.create');
        Route::post('bank-withdrawals', [BankWithdrawalController::class, 'store'])->name('bank-withdrawals.store');
    });
    Route::middleware('permission:bank-withdrawals.delete')->delete('bank-withdrawals/{bankWithdrawal}', [BankWithdrawalController::class, 'destroy'])->name('bank-withdrawals.destroy');

    // ── EMPLOYEES ──────────────────────────────────────────────────────────────
    Route::middleware('permission:employees.view')->get('employees', [EmployeeController::class, 'index'])->name('employees.index');
    Route::middleware('permission:employees.create')->group(function () {
        Route::get('employees/create', [EmployeeController::class, 'create'])->name('employees.create');
        Route::post('employees', [EmployeeController::class, 'store'])->name('employees.store');
    });
    Route::middleware('permission:employees.edit')->group(function () {
        Route::get('employees/{employee}/edit', [EmployeeController::class, 'edit'])->name('employees.edit');
        Route::put('employees/{employee}', [EmployeeController::class, 'update'])->name('employees.update');
        Route::patch('employees/{employee}', [EmployeeController::class, 'update']);
    });
    Route::middleware('permission:employees.delete')->delete('employees/{employee}', [EmployeeController::class, 'destroy'])->name('employees.destroy');

    // ── ROSTERS ────────────────────────────────────────────────────────────────
    Route::middleware('permission:rosters.view')->get('rosters', [RosterController::class, 'index'])->name('rosters.index');
    Route::middleware('permission:rosters.create')->group(function () {
        Route::get('rosters/create', [RosterController::class, 'create'])->name('rosters.create');
        Route::post('rosters', [RosterController::class, 'store'])->name('rosters.store');
    });
    Route::middleware('permission:rosters.delete')->delete('rosters/{roster}', [RosterController::class, 'destroy'])->name('rosters.destroy');

    // ── EMPLOYEE PAYMENTS ──────────────────────────────────────────────────────
    Route::middleware('permission:employee-payments.view')->get('employee-payments', [EmployeePaymentController::class, 'index'])->name('employee-payments.index');
    Route::middleware('permission:employee-payments.create')->group(function () {
        Route::get('employee-payments/create', [EmployeePaymentController::class, 'create'])->name('employee-payments.create');
        Route::post('employee-payments', [EmployeePaymentController::class, 'store'])->name('employee-payments.store');
    });
    Route::middleware('permission:employee-payments.delete')->delete('employee-payments/{employeePayment}', [EmployeePaymentController::class, 'destroy'])->name('employee-payments.destroy');

    // ── CUSTOMERS ──────────────────────────────────────────────────────────────
    Route::middleware('permission:customers.view')->get('customers', [CustomerController::class, 'index'])->name('customers.index');
    Route::middleware('permission:customers.create')->group(function () {
        Route::get('customers/create', [CustomerController::class, 'create'])->name('customers.create');
        Route::post('customers', [CustomerController::class, 'store'])->name('customers.store');
    });
    Route::middleware('permission:customers.delete')->delete('customers/{customer}', [CustomerController::class, 'destroy'])->name('customers.destroy');

    // ── CUSTOMER LEDGERS ───────────────────────────────────────────────────────
    Route::middleware('permission:customer-ledgers.view')->get('customer-ledgers', [CustomerLedgerController::class, 'index'])->name('customer-ledgers.index');
    Route::middleware('permission:customer-ledgers.create')->group(function () {
        Route::get('customer-ledgers/create', [CustomerLedgerController::class, 'create'])->name('customer-ledgers.create');
        Route::post('customer-ledgers', [CustomerLedgerController::class, 'store'])->name('customer-ledgers.store');
    });
    Route::middleware('permission:customer-ledgers.delete')->delete('customer-ledgers/{customerLedger}', [CustomerLedgerController::class, 'destroy'])->name('customer-ledgers.destroy');

    // ── VENDORS ────────────────────────────────────────────────────────────────
    Route::middleware('permission:vendors.view')->group(function () {
        Route::get('vendors', [VendorController::class, 'index'])->name('vendors.index');
        Route::get('vendors/{vendor}/ledger', [VendorController::class, 'ledger'])->name('vendors.ledger');
    });
    Route::middleware('permission:vendors.create')->group(function () {
        Route::get('vendors/create', [VendorController::class, 'create'])->name('vendors.create');
        Route::post('vendors', [VendorController::class, 'store'])->name('vendors.store');
    });
    Route::middleware('permission:vendors.edit')->group(function () {
        Route::get('vendors/{vendor}/edit', [VendorController::class, 'edit'])->name('vendors.edit');
        Route::put('vendors/{vendor}', [VendorController::class, 'update'])->name('vendors.update');
        Route::patch('vendors/{vendor}', [VendorController::class, 'update']);
    });
    Route::middleware('permission:vendors.delete')->delete('vendors/{vendor}', [VendorController::class, 'destroy'])->name('vendors.destroy');
    Route::middleware('permission:vendor-ledgers.create')->post('vendors/{vendor}/ledger', [VendorController::class, 'ledgerStore'])->name('vendors.ledger.store');
    Route::middleware('permission:vendor-ledgers.delete')->delete('vendors/{vendor}/ledger/{vendorLedger}', [VendorController::class, 'ledgerDestroy'])->name('vendors.ledger.destroy');

    // ── SHAREHOLDERS ───────────────────────────────────────────────────────────
    Route::middleware('permission:shareholders.view')->group(function () {
        Route::get('shareholders', [ShareholderController::class, 'index'])->name('shareholders.index');
        Route::get('shareholders/{shareholder}', [ShareholderController::class, 'show'])->name('shareholders.show');
    });
    Route::middleware('permission:shareholders.create')->group(function () {
        Route::get('shareholders/create', [ShareholderController::class, 'create'])->name('shareholders.create');
        Route::post('shareholders', [ShareholderController::class, 'store'])->name('shareholders.store');
    });
    Route::middleware('permission:shareholders.edit')->group(function () {
        Route::get('shareholders/{shareholder}/edit', [ShareholderController::class, 'edit'])->name('shareholders.edit');
        Route::put('shareholders/{shareholder}', [ShareholderController::class, 'update'])->name('shareholders.update');
        Route::patch('shareholders/{shareholder}', [ShareholderController::class, 'update']);
    });
    Route::middleware('permission:shareholders.delete')->delete('shareholders/{shareholder}', [ShareholderController::class, 'destroy'])->name('shareholders.destroy');
    Route::middleware('permission:shareholder-transactions.create')->post('shareholders/{shareholder}/transactions', [ShareholderController::class, 'transactionStore'])->name('shareholders.transactions.store');
    Route::middleware('permission:shareholder-transactions.delete')->delete('shareholders/{shareholder}/transactions/{transaction}', [ShareholderController::class, 'transactionDestroy'])->name('shareholders.transactions.destroy');

    // ── ITEMS ──────────────────────────────────────────────────────────────────
    Route::middleware('permission:items.view')->group(function () {
        Route::get('items', [ItemController::class, 'index'])->name('items.index');
        Route::get('items-categories', [ItemController::class, 'categories'])->name('items.categories');
    });
    Route::middleware('permission:items.create')->group(function () {
        Route::get('items/create', [ItemController::class, 'create'])->name('items.create');
        Route::post('items', [ItemController::class, 'store'])->name('items.store');
        Route::post('items-categories', [ItemController::class, 'storeCategory'])->name('items.categories.store');
    });
    Route::middleware('permission:items.edit')->group(function () {
        Route::get('items/{item}/edit', [ItemController::class, 'edit'])->name('items.edit');
        Route::put('items/{item}', [ItemController::class, 'update'])->name('items.update');
        Route::patch('items/{item}', [ItemController::class, 'update']);
    });
    Route::middleware('permission:items.delete')->group(function () {
        Route::delete('items/{item}', [ItemController::class, 'destroy'])->name('items.destroy');
        Route::delete('items-categories/{itemCategory}', [ItemController::class, 'destroyCategory'])->name('items.categories.destroy');
    });

    // ── CURRENCIES ─────────────────────────────────────────────────────────────
    Route::middleware('permission:currencies.view')->get('currencies', [CurrencyController::class, 'index'])->name('currencies.index');
    Route::middleware('permission:currencies.create')->group(function () {
        Route::get('currencies/create', [CurrencyController::class, 'create'])->name('currencies.create');
        Route::post('currencies', [CurrencyController::class, 'store'])->name('currencies.store');
    });
    Route::middleware('permission:currencies.edit')->group(function () {
        Route::get('currencies/{currency}/edit', [CurrencyController::class, 'edit'])->name('currencies.edit');
        Route::put('currencies/{currency}', [CurrencyController::class, 'update'])->name('currencies.update');
        Route::patch('currencies/{currency}', [CurrencyController::class, 'update']);
    });
    Route::middleware('permission:currencies.delete')->delete('currencies/{currency}', [CurrencyController::class, 'destroy'])->name('currencies.destroy');

    // ── SALE SOURCES ───────────────────────────────────────────────────────────
    Route::middleware('permission:sale-sources.view')->get('settings/sale-sources', [SettingsController::class, 'saleSources'])->name('settings.sale-sources.index');
    Route::middleware('permission:sale-sources.create')->post('settings/sale-sources', [SettingsController::class, 'storeSaleSource'])->name('settings.sale-sources.store');
    Route::middleware('permission:sale-sources.delete')->delete('settings/sale-sources/{saleSource}', [SettingsController::class, 'destroySaleSource'])->name('settings.sale-sources.destroy');
    Route::middleware('permission:sale-sources.edit')->patch('settings/sale-sources/{saleSource}/toggle', [SettingsController::class, 'toggleSaleSource'])->name('settings.sale-sources.toggle');

    // ── EXPENSE CATEGORIES ─────────────────────────────────────────────────────
    Route::middleware('permission:expense-categories.view')->get('settings/expense-categories', [SettingsController::class, 'expenseCategories'])->name('settings.expense-categories.index');
    Route::middleware('permission:expense-categories.create')->post('settings/expense-categories', [SettingsController::class, 'storeExpenseCategory'])->name('settings.expense-categories.store');
    Route::middleware('permission:expense-categories.delete')->delete('settings/expense-categories/{expenseCategory}', [SettingsController::class, 'destroyExpenseCategory'])->name('settings.expense-categories.destroy');
    Route::middleware('permission:expense-categories.edit')->patch('settings/expense-categories/{expenseCategory}/toggle', [SettingsController::class, 'toggleExpenseCategory'])->name('settings.expense-categories.toggle');

    // ── USERS ──────────────────────────────────────────────────────────────────
    Route::middleware('permission:users.view')->get('users', [UserController::class, 'index'])->name('users.index');
    Route::middleware('permission:users.create')->group(function () {
        Route::get('users/create', [UserController::class, 'create'])->name('users.create');
        Route::post('users', [UserController::class, 'store'])->name('users.store');
    });
    Route::middleware('permission:users.edit')->group(function () {
        Route::get('users/{user}/edit', [UserController::class, 'edit'])->name('users.edit');
        Route::put('users/{user}', [UserController::class, 'update'])->name('users.update');
        Route::patch('users/{user}', [UserController::class, 'update']);
    });
    Route::middleware('permission:users.delete')->delete('users/{user}', [UserController::class, 'destroy'])->name('users.destroy');

    // ── ROLES ──────────────────────────────────────────────────────────────────
    Route::middleware('permission:roles.view')->get('roles', [RoleController::class, 'index'])->name('roles.index');
    Route::middleware('permission:roles.create')->group(function () {
        Route::get('roles/create', [RoleController::class, 'create'])->name('roles.create');
        Route::post('roles', [RoleController::class, 'store'])->name('roles.store');
    });
    Route::middleware('permission:roles.edit')->group(function () {
        Route::get('roles/{role}/edit', [RoleController::class, 'edit'])->name('roles.edit');
        Route::put('roles/{role}', [RoleController::class, 'update'])->name('roles.update');
        Route::patch('roles/{role}', [RoleController::class, 'update']);
    });
    Route::middleware('permission:roles.delete')->delete('roles/{role}', [RoleController::class, 'destroy'])->name('roles.destroy');

    // ── REPORTS ────────────────────────────────────────────────────────────────
    Route::middleware('permission:reports.view')->group(function () {
        Route::get('/reports/daily', [ReportController::class, 'dailyIncome'])->name('reports.daily');
        Route::get('/reports/daily/pdf', [ReportController::class, 'dailyPdf'])->name('reports.daily.pdf');
        Route::get('/reports/monthly', [ReportController::class, 'monthlyProfitLoss'])->name('reports.monthly');
        Route::get('/reports/monthly/pdf', [ReportController::class, 'monthlyProfitLossPdf'])->name('reports.monthly.pdf');
        Route::get('/reports/vendor', [ReportController::class, 'vendorReport'])->name('reports.vendor');
    });
    Route::middleware('permission:shareholder-reports.view')->group(function () {
        Route::get('/reports/shareholders', [ReportController::class, 'shareholderReport'])->name('reports.shareholders');
        Route::get('/reports/shareholders/pdf', [ReportController::class, 'shareholderAllPdf'])->name('reports.shareholders.pdf');
        Route::get('/reports/shareholders/{shareholder}/pdf', [ReportController::class, 'shareholderOnePdf'])->name('reports.shareholders.one-pdf');
    });

    // ── PROFILE (no permission needed) ─────────────────────────────────────────
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});

require __DIR__.'/auth.php';
