<?php

use App\Http\Controllers\Auth\ForgotPasswordController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\ResetPasswordController;
use App\Http\Controllers\AchatController;
use App\Http\Controllers\AuditController;
use App\Http\Controllers\CautionController;
use App\Http\Controllers\ClientController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\DepenseController;
use App\Http\Controllers\FournisseurControlleur;
use App\Http\Controllers\InventaireController;
use App\Http\Controllers\InvoiceController;
use App\Http\Controllers\MagasinController;
use App\Http\Controllers\MouvementController;
use App\Http\Controllers\PersonnelController;
use App\Http\Controllers\ProduitController;
use App\Http\Controllers\StockController;
use App\Http\Controllers\RapportController;
use App\Http\Controllers\TransfertStockController;
use App\Http\Controllers\VenteController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

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

Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login')->middleware('guest');
Route::post('/login', [LoginController::class, 'login'])->middleware('guest');
Route::post('/logout', [LoginController::class, 'logout'])->name('logout')->middleware('auth');

Route::get('/mot-de-passe-oublie', [ForgotPasswordController::class, 'showLinkRequestForm'])->name('password.request')->middleware('guest');
Route::post('/mot-de-passe-oublie', [ForgotPasswordController::class, 'sendResetLinkEmail'])->name('password.email')->middleware('guest');
Route::get('/reinitialiser-mot-de-passe/{token}', [ResetPasswordController::class, 'showResetForm'])->name('password.reset')->middleware('guest');
Route::post('/reinitialiser-mot-de-passe', [ResetPasswordController::class, 'reset'])->name('password.update')->middleware('guest');

Route::middleware(['auth', 'ensure.user.active', 'deny.administrateur.create.update'])->group(function () {
    Route::get('/dashboard', DashboardController::class)->name('dashboard');

    Route::middleware('deny.vendeur.magasin.personnel')->group(function () {
        Route::put('magasins/{id}/disabled', [MagasinController::class, 'disabledStore'])->name('magasins.disabled');
        Route::resource('magasins', MagasinController::class);
    });
    Route::get('clients/{id}/releve-pdf', [ClientController::class, 'relevePdf'])->name('clients.releve-pdf');
    Route::resource('clients', ClientController::class)->parameters(['clients' => 'id']);
    Route::resource('fournisseurs', FournisseurControlleur::class)->parameters(['fournisseurs' => 'id']);
    Route::get('depenses/export', [DepenseController::class, 'export'])->name('depenses.export');
    Route::resource('depenses', DepenseController::class)->parameters(['depenses' => 'id']);
    Route::get('produits/export', [ProduitController::class, 'export'])->name('produits.export');
    Route::resource('produits', ProduitController::class)->parameters(['produits' => 'id']);
    Route::get('transferts/export', [TransfertStockController::class, 'export'])->name('transferts.export');
    Route::resource('transferts', TransfertStockController::class)->parameters(['transferts' => 'id']);
    Route::get('ventes/export', [VenteController::class, 'export'])->name('ventes.export');
    Route::post('ventes/store-client', [VenteController::class, 'storeClient'])->name('ventes.store-client');
    Route::post('ventes/{vente}/paiement', [VenteController::class, 'storePaiement'])->name('ventes.paiement.store');
    Route::resource('ventes', VenteController::class)->parameters(['ventes' => 'id']);
    Route::resource('achats', AchatController::class)->parameters(['achats' => 'id']);
    Route::middleware('deny.vendeur.magasin.personnel')->group(function () {
        Route::put('personnel/{id}/toggle-active', [PersonnelController::class, 'toggleActive'])->name('personnel.toggle-active');
        Route::resource('personnel', PersonnelController::class)
            ->parameters(['personnel' => 'id'])
            ->only(['index', 'store', 'update', 'destroy']);
    });

    /*** Stock Module ***/
    Route::get('stock/export', [StockController::class, 'export'])->name('stock.export');
    Route::get('/stock', StockController::class)->name('stock.index');

    /*** Mouvements Module ***/
    Route::get('mouvements/export', [MouvementController::class, 'export'])->name('mouvements.export');
    Route::get('mouvements', [MouvementController::class, 'index'])->name('mouvements.index');
    Route::get('factures', [InvoiceController::class, 'index'])->name('factures.index');
    Route::get('factures/export', [InvoiceController::class, 'export'])->name('factures.export');
    Route::get('audits', [AuditController::class, 'index'])->name('audits.index');

    /*** Inventaire ***/
    Route::get('/inventaire', [InventaireController::class, 'index'])->name('inventaire.index');
    Route::post('/inventaire', [InventaireController::class, 'store'])->name('inventaire.store');
    Route::get('/inventaire/{inventaire}', [InventaireController::class, 'show'])->name('inventaire.show');
    Route::put('/inventaire/{inventaire}', [InventaireController::class, 'update'])->name('inventaire.update');
    Route::post('/inventaire/{inventaire}/terminer', [InventaireController::class, 'terminer'])->name('inventaire.terminer');

    /*** Rapports ***/
    Route::get('/rapports', [RapportController::class, 'index'])->name('rapports.index');

    /** Gestion des cautions clients */
    Route::get('/cautions/export', [CautionController::class, 'export'])->name('cautions.export');
    Route::get('/cautions', [CautionController::class, 'index'])->name('cautions.index');
});