<?php

use App\Http\Controllers\AuthController;
use App\Http\Controllers\CustomerController;
use App\Http\Controllers\DailyClosingController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\ExpenseController;
use App\Http\Controllers\FuelSaleController;
use App\Http\Controllers\GuideController;
use App\Http\Controllers\LocalizationController;
use App\Http\Controllers\PurchaseController;
use App\Http\Controllers\ReceiptController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\RoleController;
use App\Http\Controllers\SettingController;
use App\Http\Controllers\StockController;
use App\Http\Controllers\SupplierController;
use App\Http\Controllers\SupplierPaymentController;
use App\Http\Controllers\TankController;
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;

Route::get('/', fn () => redirect()->route('dashboard'));

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

Route::get('/lang/{locale}', function ($locale) {
    if (in_array($locale, ['en', 'ps', 'fa'], true)) {
        session(['locale' => $locale]);
    }
    return back();
})->name('lang.switch');

Route::middleware(['auth'])->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->middleware('permission:dashboard')->name('dashboard');
    Route::get('/guide', [GuideController::class, 'index'])->name('guide.index');

    Route::middleware('permission:pos_sale')->group(function () {
        Route::get('/pos/fuel-sale', [FuelSaleController::class, 'create'])->name('fuel-sales.create');
        Route::post('/pos/fuel-sale', [FuelSaleController::class, 'store'])->name('fuel-sales.store');
        Route::get('/sales/{sale}/print', [FuelSaleController::class, 'print'])->name('fuel-sales.print');
    });

    Route::get('/sales', [FuelSaleController::class, 'index'])->middleware('permission:sales_list')->name('fuel-sales.index');
    Route::resource('purchases', PurchaseController::class)->only(['index','create','store','show'])->middleware('permission:purchase');
    Route::resource('tanks', TankController::class)->except(['show','destroy'])->middleware('permission:stock');
    Route::get('/stock/manage', [StockController::class, 'manage'])->middleware('permission:stock')->name('stock.manage');
    Route::post('/stock/add', [StockController::class, 'add'])->middleware('permission:stock')->name('stock.add');
    Route::post('/stock/correct', [StockController::class, 'correct'])->middleware('permission:stock')->name('stock.correct');
    Route::resource('customers', CustomerController::class)->except(['show','destroy'])->middleware('permission:customers');
    Route::resource('suppliers', SupplierController::class)->except(['show','destroy'])->middleware('permission:suppliers');
    Route::resource('expenses', ExpenseController::class)->except(['show','destroy'])->middleware('permission:expenses');

    Route::middleware('permission:cash')->group(function () {
        Route::get('/customer-receipts/create', [ReceiptController::class, 'create'])->name('customer-receipts.create');
        Route::post('/customer-receipts', [ReceiptController::class, 'store'])->name('customer-receipts.store');
        Route::get('/supplier-payments/create', [SupplierPaymentController::class, 'create'])->name('supplier-payments.create');
        Route::post('/supplier-payments', [SupplierPaymentController::class, 'store'])->name('supplier-payments.store');
        Route::get('/daily-closing', [DailyClosingController::class, 'index'])->name('daily-closing.index');
        Route::get('/daily-closing/create', [DailyClosingController::class, 'create'])->name('daily-closing.create');
        Route::post('/daily-closing', [DailyClosingController::class, 'store'])->name('daily-closing.store');
    });

    Route::middleware('permission:reports')->group(function () {
        Route::get('/reports/daily-sales', [ReportController::class, 'dailySales'])->name('reports.daily-sales');
        Route::get('/reports/stock', [ReportController::class, 'stock'])->name('reports.stock');
        Route::get('/reports/profit', [ReportController::class, 'profit'])->name('reports.profit');
    });

    Route::get('/settings', [SettingController::class, 'edit'])->middleware('permission:settings')->name('settings.edit');
    Route::post('/settings', [SettingController::class, 'update'])->middleware('permission:settings')->name('settings.update');

    Route::resource('users', UserController::class)->except(['show','destroy'])->middleware('permission:users');
    Route::resource('roles', RoleController::class)->except(['show','destroy'])->middleware('permission:users');

    Route::get('/localization', [LocalizationController::class, 'index'])->middleware('permission:localization')->name('localization.index');
    Route::post('/localization', [LocalizationController::class, 'update'])->middleware('permission:localization')->name('localization.update');
});
