<?php

use App\Http\Controllers\ProfileController;

use App\Http\Controllers\Admin\MemberController;
use App\Http\Controllers\Admin\ProjectController;
use App\Http\Controllers\Admin\ContributionController;
use App\Http\Controllers\Member\DashboardController as MemberDashboardController;
use App\Http\Controllers\Public\HomeController;
use App\Http\Controllers\Public\ContactController;
use App\Http\Controllers\Public\ProjectController as PublicProjectController;
use Illuminate\Support\Facades\Route;

// Routes publiques (site vitrine)
Route::get('/', [HomeController::class, 'index'])->name('home');
Route::get('/about', [HomeController::class, 'about'])->name('about');
Route::get('/projects', [PublicProjectController::class, 'index'])->name('public.projects');
Route::get('/projects/{project}', [PublicProjectController::class, 'show'])->name('public.project.show');
Route::post('/contact', [ContactController::class, 'store'])->name('contact.store');

// Routes d'authentification
require __DIR__ . '/auth.php';


// Routes membres
Route::middleware(['auth', 'verified'])->group(function () {
    Route::get('/member/dashboard', [MemberDashboardController::class, 'index'])->name('member.dashboard');

    // Rapports et Statistiques (Accessible à tous les membres)
    Route::get('/admin/reports', [\App\Http\Controllers\Admin\ReportController::class, 'index'])->name('admin.reports.index');
    Route::get('/admin/reports/export/pdf', [\App\Http\Controllers\Admin\ReportController::class, 'exportPdf'])->name('admin.reports.export.pdf');

    // Redirection selon le rôle après connexion
    Route::get('/dashboard', function () {
        $user = auth()->user();
        if ($user->canManage()) {
            return redirect()->route('admin.dashboard');
        }
        return redirect()->route('member.dashboard');
    })->name('dashboard');
});

// Routes administration (Admin et Ayant droit)
Route::middleware(['auth', 'verified', \App\Http\Middleware\EnsureUserCanManage::class])
    ->prefix('admin')
    ->name('admin.')
    ->group(function () {

        // Tableau de bord
        Route::get('/dashboard', [\App\Http\Controllers\Admin\DashboardController::class, 'index'])->name('dashboard');

        // Gestion des membres
        Route::get('/members/export/excel', [MemberController::class, 'exportExcel'])->name('members.export.excel');
        Route::get('/members/export/pdf', [MemberController::class, 'exportPdf'])->name('members.export.pdf');
        Route::resource('members', MemberController::class)->except(['show']);
        Route::post('/members/{member}/toggle-status', [MemberController::class, 'toggleStatus'])->name('members.toggle-status');

        // Gestion des projets
        Route::get('/projects/export/excel', [ProjectController::class, 'exportExcel'])->name('projects.export.excel');
        Route::get('/projects/export/pdf', [ProjectController::class, 'exportPdf'])->name('projects.export.pdf');
        Route::resource('projects', ProjectController::class)->except(['show']);

        // Gestion des cotisations
        Route::get('/contributions/export/excel', [ContributionController::class, 'exportExcel'])->name('contributions.export.excel');
        Route::get('/contributions/export/pdf', [ContributionController::class, 'exportPdf'])->name('contributions.export.pdf');
        Route::resource('contributions', ContributionController::class);

        // Gestion Finance (Transactions)
        Route::get('/transactions/export/pdf', [\App\Http\Controllers\Admin\TransactionController::class, 'exportPdf'])->name('transactions.export.pdf');
        Route::resource('transactions', \App\Http\Controllers\Admin\TransactionController::class);

        // Gestion des comptes
        Route::resource('accounts', \App\Http\Controllers\Admin\AccountController::class);

        // Paramètres
        Route::get('/settings', [\App\Http\Controllers\Admin\SettingsController::class, 'index'])->name('settings.index');
        Route::post('/settings/currency', [\App\Http\Controllers\Admin\SettingsController::class, 'updateCurrency'])->name('settings.update-currency');
        Route::post('/settings/registration', [\App\Http\Controllers\Admin\SettingsController::class, 'updateRegistrationSettings'])->name('settings.update-registration');
        Route::post('/settings/management-year', [\App\Http\Controllers\Admin\SettingsController::class, 'updateManagementYear'])->name('settings.update-management-year');
        Route::get('/settings/admins/create', [\App\Http\Controllers\Admin\SettingsController::class, 'createAdmin'])->name('settings.create-admin');
        Route::post('/settings/admins', [\App\Http\Controllers\Admin\SettingsController::class, 'addAdmin'])->name('settings.add-admin');
        Route::delete('/settings/admins/{user}', [\App\Http\Controllers\Admin\SettingsController::class, 'removeAdmin'])->name('settings.remove-admin');

        // Années de gestion (CRUD)
        Route::resource('management-years', \App\Http\Controllers\Admin\ManagementYearController::class)->except(['index', 'show']);
    });

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