<?php

use App\Enums\ConsultationStatus;
use App\Models\Consultation;
use App\Services\CalendarService;
use Illuminate\Support\Facades\Route;
use Laravel\Fortify\Features;
use Livewire\Volt\Volt;

Route::get('/', function () {
    return view('pages.home');
})->name('home');

Volt::route('/booking', 'pages.booking')->name('booking');
Volt::route('/booking/success', 'pages.booking-success')->name('booking.success');

Volt::route('/posts', 'pages.posts.index')->name('posts.index');
Volt::route('/posts/{post}', 'pages.posts.show')->name('posts.show');

Route::get('/page/{slug}', function (string $slug) {
    $page = \App\Models\Page::query()->where('slug', $slug)->firstOrFail();

    return view('pages.legal', ['page' => $page]);
})->name('page.show')->where('slug', 'terms|privacy');

// Legacy routes for backward compatibility
Route::redirect('/terms', '/page/terms')->name('terms');
Route::redirect('/privacy', '/page/privacy')->name('privacy');

Route::get('/language/{locale}', function (string $locale) {
    if (! in_array($locale, ['ar', 'en'])) {
        abort(400);
    }

    session(['locale' => $locale]);

    if (auth()->check()) {
        auth()->user()->update(['preferred_language' => $locale]);
    }

    return redirect()->back();
})->name('language.switch');

Route::middleware(['auth', 'active'])->group(function () {
    // Admin routes
    Route::middleware('admin')->prefix('admin')->group(function () {
        Volt::route('/dashboard', 'admin.dashboard')
            ->name('admin.dashboard');

        // Individual Clients Management
        Route::prefix('clients/individual')->name('admin.clients.individual.')->group(function () {
            Volt::route('/', 'admin.clients.individual.index')->name('index');
            Volt::route('/create', 'admin.clients.individual.create')->name('create');
            Volt::route('/{client}', 'admin.clients.individual.show')->name('show');
            Volt::route('/{client}/edit', 'admin.clients.individual.edit')->name('edit');
        });

        // Company Clients Management
        Route::prefix('clients/company')->name('admin.clients.company.')->group(function () {
            Volt::route('/', 'admin.clients.company.index')->name('index');
            Volt::route('/create', 'admin.clients.company.create')->name('create');
            Volt::route('/{client}', 'admin.clients.company.show')->name('show');
            Volt::route('/{client}/edit', 'admin.clients.company.edit')->name('edit');
        });

        // Bookings Management
        Route::prefix('bookings')->name('admin.bookings.')->group(function () {
            Volt::route('/pending', 'admin.bookings.pending')->name('pending');
            Volt::route('/{consultation}', 'admin.bookings.review')->name('review');
        });

        // Consultations Management
        Route::prefix('consultations')->name('admin.consultations.')->group(function () {
            Volt::route('/', 'admin.consultations.index')->name('index');
            Volt::route('/export', 'admin.consultations.export-consultations')->name('export');
            Volt::route('/{consultation}', 'admin.consultations.show')->name('show');
        });

        // Client Consultation History
        Volt::route('/clients/{user}/consultations', 'admin.clients.consultation-history')
            ->name('admin.clients.consultation-history');

        // Timelines Management
        Route::prefix('timelines')->name('admin.timelines.')->group(function () {
            Volt::route('/', 'admin.timelines.index')->name('index');
            Volt::route('/create', 'admin.timelines.create')->name('create');
            Volt::route('/export', 'admin.timelines.export-timelines')->name('export');
            Volt::route('/{timeline}', 'admin.timelines.show')->name('show');
        });

        // Admin Settings
        Route::prefix('settings')->name('admin.settings.')->group(function () {
            Volt::route('/working-hours', 'admin.settings.working-hours')->name('working-hours');
            Volt::route('/blocked-times', 'admin.settings.blocked-times')->name('blocked-times');
        });

        // Legal Pages Management
        Route::prefix('pages')->name('admin.pages.')->group(function () {
            Volt::route('/', 'admin.pages.index')->name('index');
            Volt::route('/{slug}/edit', 'admin.pages.edit')
                ->name('edit')
                ->where('slug', 'terms|privacy');
        });

        // Posts Management
        Route::prefix('posts')->name('admin.posts.')->group(function () {
            Volt::route('/', 'admin.posts.index')->name('index');
            Volt::route('/create', 'admin.posts.create')->name('create');
            Volt::route('/{post}/edit', 'admin.posts.edit')->name('edit');
        });

        // User Export
        Volt::route('/users/export', 'admin.users.export-users')
            ->name('admin.users.export');

        // Reports
        Route::prefix('reports')->name('admin.reports.')->group(function () {
            Volt::route('/monthly', 'admin.reports.monthly-report')->name('monthly');
        });

        // Audit Logs
        Volt::route('/audit-logs', 'admin.audit-logs')->name('admin.audit-logs');
    });

    // Client routes
    Route::middleware('client')->prefix('client')->name('client.')->group(function () {
        Volt::route('/dashboard', 'client.dashboard')
            ->name('dashboard');

        // Consultations
        Route::prefix('consultations')->name('consultations.')->group(function () {
            Volt::route('/', 'client.consultations.index')->name('index');
            Volt::route('/book', 'client.consultations.book')->name('book');
            Route::get('/{consultation}/calendar', function (Consultation $consultation) {
                abort_unless($consultation->user_id === auth()->id(), 403);
                abort_unless($consultation->status === ConsultationStatus::Approved, 404);

                return app(CalendarService::class)->generateDownloadResponse($consultation);
            })->name('calendar');
        });

        // Timelines
        Route::prefix('timelines')->name('timelines.')->group(function () {
            Volt::route('/', 'client.timelines.index')->name('index');
            Volt::route('/{timeline}', 'client.timelines.show')->name('show');
        });

        // Profile
        Volt::route('/profile', 'client.profile')->name('profile');
    });

    // Settings routes
    Route::redirect('settings', 'settings/profile');

    Volt::route('settings/profile', 'settings.profile')->name('profile.edit');
    Volt::route('settings/password', 'settings.password')->name('user-password.edit');
    Volt::route('settings/appearance', 'settings.appearance')->name('appearance.edit');

    Volt::route('settings/two-factor', 'settings.two-factor')
        ->middleware(
            when(
                Features::canManageTwoFactorAuthentication()
                    && Features::optionEnabled(Features::twoFactorAuthentication(), 'confirmPassword'),
                ['password.confirm'],
                [],
            ),
        )
        ->name('two-factor.show');
});
