<?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\ProfileController;
use App\Http\Controllers\RequestController;
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\DepartmentController;
use App\Http\Controllers\Auth\AuthenticatedSessionController;

// 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', function () {
        $requestModel = \App\Models\Request::class;
        return view('dashboard', [
            'totalProducts' => Product::count(),
            'totalStock' => Stock::sum('actual_quantity'),
            'pendingRequests' => $requestModel::where('status', 'en_attente')->count(),
            'movementsToday' => MovementStock::whereDate('created_at', today())->count(),
            'validatedRequests' => $requestModel::where('status', 'validée')->count(),
            'rejectedRequests' => $requestModel::where('status', 'rejetée')->count(),
            'suppliersCount' => Supplier::count(),
            'equipmentCount' => Equipment::count(),
        ]);
    })->name('dashboard');

    // Produits
    Route::get('/products', function () {
        return view('products.index');
    })->name('products.index');

    // Stocks
    Route::get('/stocks', function () {
        return view('stocks.index');
    })->name('stocks.index');

    // Mouvements
    Route::get('/movements', function () {
        return view('movements.index');
    })->name('movements.index');

    // Demandes
    Route::get('/requests', function () {
        return view('requests.index');
    })->name('requests.index');



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

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


    // Fournisseurs
    Route::get('/suppliers', function () {
        return view('suppliers.index');
    })->name('suppliers.index');



    // Équipements
    Route::get('/equipment', function () {
        return view('equipment.index');
    })->name('equipment.index');

    // 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');
});