<?php

use App\Models\Stock;
use App\Models\Product;
use App\Models\Supplier;
use App\Models\Equipment;
use App\Models\MovementStock;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuditController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\InventoryController;
use App\Http\Controllers\AnnualReportController;
use App\Http\Controllers\ProductStockController;
use App\Http\Controllers\UnityController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\RequestController;
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\SupplierController;
use App\Http\Controllers\EquipmentController;
use App\Http\Controllers\DepartmentController;
use App\Http\Controllers\MovementStockController;
use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\DashboardController;

// Page d'accueil - redirige vers le dashboard
Route::get('/', function () {
    return redirect()->route('dashboard');
});

// Routes d'authentification (accessibles sans être connecté)
Route::middleware('guest')->group(function () {
    Route::get('/login', [AuthenticatedSessionController::class, 'create'])->name('login');
    Route::post('/login', [AuthenticatedSessionController::class, 'store']);
});

// Déconnexion
Route::post('/logout', [AuthenticatedSessionController::class, 'destroy'])->name('logout');

// Routes protégées (nécessitent une authentification)
Route::middleware(['auth'])->group(function () {
    // Dashboard
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    // Produits & Stocks (vue unifiée)
    Route::get('/product-stocks', [ProductStockController::class, 'index'])->name('product-stocks.index');
    Route::get('/product-stocks/export', [ProductStockController::class, 'export'])->name('product-stocks.export');
    Route::post('/product-stocks', [ProductStockController::class, 'store'])->name('product-stocks.store');
    Route::put('/product-stocks/{product}', [ProductStockController::class, 'update'])->name('product-stocks.update');
    Route::delete('/product-stocks/{product}', [ProductStockController::class, 'destroy'])->name('product-stocks.destroy');
    Route::put('/product-stocks/{product}/archive', [ProductStockController::class, 'archive'])->name('product-stocks.archive');

    // Mouvements
    Route::get('/movements', [MovementStockController::class, 'index'])->name('movements.index');

    // Inventaire (produits par catégories)
    Route::get('/inventory', [InventoryController::class, 'index'])->name('inventory.index');
    Route::get('/inventory/export', [InventoryController::class, 'export'])->name('inventory.export');
    Route::get('/movements/export', [MovementStockController::class, 'export'])->name('movements.export');
    Route::post('/movements/entree', [MovementStockController::class, 'storeEntree'])->name('movements.store.entree');
    Route::post('/movements/sortie', [MovementStockController::class, 'storeSortie'])->name('movements.store.sortie');

    // Demandes
    Route::get('requests', [RequestController::class, 'index'])->name('requests.index');
    Route::get('requests/export', [RequestController::class, 'export'])->name('requests.export');
    Route::post('requests', [RequestController::class, 'store'])->name('requests.store');
    Route::put('requests/{request}', [RequestController::class, 'update'])->name('requests.update');


    Route::resource('categories', CategoryController::class);
    Route::resource('departments', DepartmentController::class);
    Route::resource('units', UnityController::class);
    Route::resource('suppliers', SupplierController::class);

    // Importation des départements
    Route::post('/departments/import', [DepartmentController::class, 'import'])->name('departments.import');

    // Équipements
    Route::get('/equipment', [EquipmentController::class, 'index'])->name('equipment.index');
    Route::post('/equipment', [EquipmentController::class, 'store'])->name('equipment.store');
    Route::put('/equipment/{id}', [EquipmentController::class, 'update'])->name('equipment.update');
    Route::delete('/equipment/{id}', [EquipmentController::class, 'destroy'])->name('equipment.destroy');

    // Profil utilisateur
    Route::get('/profile', [ProfileController::class, 'show'])->name('profile.show');
    Route::get('/profile/edit', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::put('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::get('/profile/password', [ProfileController::class, 'password'])->name('profile.password');
    Route::put('/profile/password', [ProfileController::class, 'updatePassword'])->name('profile.password.update');

    // Actions sur les demandes
    Route::post('/requests/{request}/approve', [RequestController::class, 'approve'])->name('requests.approve');
    Route::post('/requests/{request}/reject', [RequestController::class, 'reject'])->name('requests.reject');

    // Notifications
    Route::post('/notifications/{notification}/read', [RequestController::class, 'markNotificationAsRead'])->name('notifications.read');
    Route::post('/notifications/read-all', [RequestController::class, 'markAllNotificationsAsRead'])->name('notifications.read-all');

    // Utilisateurs (Super Admin uniquement)
    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::put('/users/{user}/toggle-active', [UserController::class, 'toggleActive'])->name('users.toggle-active');

    // Audits
    Route::get('/audits', [AuditController::class, 'index'])->name('audits.index');

    // Rapport annuel
    Route::get('/annual-report', [AnnualReportController::class, 'index'])->name('annual-report.index');
    Route::get('/annual-report/export', [AnnualReportController::class, 'export'])->name('annual-report.export');
});
