<?php


use App\Http\Controllers\Admin\AgencyController;
use App\Http\Controllers\Admin\AuthController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\RoleController;
use App\Http\Controllers\Admin\UserController;
use Illuminate\Support\Facades\Route;

Route::prefix('admin')->name('admin.')->group(function () {

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

    Route::middleware('admin.auth')->group(function () {
        Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
        Route::post('/logout', [AuthController::class, 'logout'])->name('logout');


        // User Management Routes
        Route::prefix('users')->name('users.')->group(function () {
            Route::get('/', [UserController::class, 'index'])->middleware('can:users.view')->name('index');
            Route::get('/create', [UserController::class, 'create'])->middleware('can:users.create')->name('create');
            Route::post('/', [UserController::class, 'store'])->middleware('can:users.create')->name('store');
            Route::get('/{user}', [UserController::class, 'show'])->middleware('can:users.view')->name('show');
            Route::get('/{user}/edit', [UserController::class, 'edit'])->middleware('can:users.edit')->name('edit');
            Route::put('/{user}', [UserController::class, 'update'])->middleware('can:users.edit')->name('update');
            Route::delete('/{user}', [UserController::class, 'destroy'])->middleware('can:users.delete')->name('destroy');
            Route::patch('users/{user}/toggle-status', [UserController::class, 'toggleStatus'])->name('toggle-status');
            Route::post('users/{user}/reset-password', [UserController::class, 'resetPassword'])->name('reset-password');
            Route::get('/export/excel', [UserController::class, 'exportExcel'])->middleware('can:users.view')->name('export.excel');
            Route::get('/export/pdf', [UserController::class, 'exportPdf'])->middleware('can:users.view')->name('export.pdf');
        });

        // Role Management Routes
        Route::prefix('roles')->name('roles.')->group(function () {
            Route::get('/', [RoleController::class, 'index'])->name('index');
            Route::get('/create', [RoleController::class, 'create'])->name('create');
            Route::post('/', [RoleController::class, 'store'])->name('store');
            Route::get('/{role}', [RoleController::class, 'show'])->name('show');
            Route::get('/{role}/edit', [RoleController::class, 'edit'])->name('edit');
            Route::put('/{role}', [RoleController::class, 'update'])->name('update');
            Route::delete('/{role}', [RoleController::class, 'destroy'])->name('destroy');
        });

        // Agency routes
        Route::resource('agencies', AgencyController::class);
        Route::patch('agencies/{agency}/approve', [AgencyController::class, 'approve'])->name('agencies.approve');
        Route::patch('agencies/{agency}/reject', [AgencyController::class, 'reject'])->name('agencies.reject');
        Route::patch('agencies/{agency}/activate', [AgencyController::class, 'activate'])->name('agencies.activate');
        Route::patch('agencies/{agency}/deactivate', [AgencyController::class, 'deactivate'])->name('agencies.deactivate');
        Route::get('agencies/{agency}/documents/{type}', [AgencyController::class, 'downloadDocument'])->name('agencies.documents.download');
        Route::delete('agencies/{agency}/documents/{type}', [AgencyController::class, 'deleteDocument'])->name('agencies.documents.delete');
    });
});
