<?php

use App\Http\Controllers\Admin\ActivityLogController;
use App\Http\Controllers\Admin\AdminUserController;
use App\Http\Controllers\Admin\AssignmentController;
use App\Http\Controllers\Admin\DashboardController as AdminDashboardController;
use App\Http\Controllers\Admin\FilialeController;
use App\Http\Controllers\Admin\FilialeUserController;
use App\Http\Controllers\Admin\FormController;
use App\Http\Controllers\Admin\NotificationController as AdminNotificationController;
use App\Http\Controllers\Admin\SubmissionsController;
use App\Http\Controllers\Filiale\DashboardController as FilialeDashboardController;
use App\Http\Controllers\Filiale\FormFillController;
use App\Http\Controllers\Filiale\NotificationController;
use App\Http\Controllers\FileDownloadController;
use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    if (auth()->check()) {
        return auth()->user()->isAdmin()
            ? redirect()->route('admin.dashboard')
            : redirect()->route('filiale.dashboard');
    }

    return redirect()->route('login');
});

Route::middleware(['auth', 'admin'])->prefix('admin')->name('admin.')->group(function () {
    Route::get('/', [AdminDashboardController::class, 'index'])->name('dashboard');
    Route::resource('filiales', FilialeController::class)->except(['show']);
    Route::resource('forms', FormController::class);
    Route::patch('forms/{form}/lock', [FormController::class, 'toggleLock'])->name('forms.lock');
    Route::get('forms/{form}/assign', [AssignmentController::class, 'create'])->name('forms.assign');
    Route::post('forms/{form}/assign', [AssignmentController::class, 'store'])->name('forms.assign.store');
    Route::put('forms/{form}/assignments/due-dates', [AssignmentController::class, 'updateDueDatesBulk'])->name('forms.assignments.due-dates');
    Route::put('forms/{form}/assignments/{filiale}/due-date', [AssignmentController::class, 'updateDueDate'])->name('forms.assignments.due-date');
    Route::get('submissions', [SubmissionsController::class, 'overview'])->name('submissions.overview');
    Route::get('filiales/{filiale}/submissions', [SubmissionsController::class, 'byFiliale'])->name('filiales.submissions');
    Route::get('forms/{form}/submissions', [SubmissionsController::class, 'index'])->name('forms.submissions');
    Route::get('submissions/{submission}', [SubmissionsController::class, 'show'])->name('submissions.show');
    Route::get('notifications', [AdminNotificationController::class, 'index'])->name('notifications');
    Route::post('notifications/{id}/read', [AdminNotificationController::class, 'markAsRead'])->name('notifications.read');
    Route::get('journal', [ActivityLogController::class, 'index'])->name('activity.index');
    Route::resource('filiales.users', FilialeUserController::class)->except(['show']);

    // Gestion des comptes admin / super admin — réservée aux super administrateurs.
    Route::middleware('super_admin')->group(function () {
        Route::resource('admins', AdminUserController::class)
            ->except(['show'])
            ->parameters(['admins' => 'admin']);
    });
});

Route::middleware(['auth', 'filiale'])->prefix('filiale')->name('filiale.')->group(function () {
    Route::get('/', [FilialeDashboardController::class, 'index'])->name('dashboard');
    Route::get('forms/{form}/fill', [FormFillController::class, 'show'])->name('forms.fill');
    Route::post('forms/{form}/fill', [FormFillController::class, 'store'])->name('forms.fill.store');
    Route::get('notifications', [NotificationController::class, 'index'])->name('notifications');
    Route::post('notifications/{id}/read', [NotificationController::class, 'markAsRead'])->name('notifications.read');
});

Route::middleware('auth')->group(function () {
    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');

    Route::get('fichiers', [FileDownloadController::class, 'show'])->name('files.show');
});

require __DIR__.'/auth.php';
