<?php

use App\Http\Controllers\AudienceFileController;
use App\Models\AudienceFile;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AppointmentController;
use App\Http\Middleware\AdminMiddleware;
use App\Http\Middleware\CanValidateUsers;
use App\Http\Controllers\{AppointmentAdminController, ReportController};
use App\Http\Controllers\LoginController;
use App\Mail\{AppointmentMail, AskAdviceMail, GiveAdviceMail};
use App\Http\Controllers\ServiceController;
use Illuminate\Support\Facades\Mail;
use App\Models\RequererType;

Route::get('/', function () {
    $requererTypes = RequererType::orderBy('label')->get();
    return view('welcome', compact('requererTypes'));
})->name('welcome');
Route::resource('appointments', AppointmentController::class);

Route::get('/preview-mail', function () {
    $fakeData = [
        'name' => 'John Doe',
        'subject' => 'Test Demande d\'audience',
        'object' => 'Consultation',
        'message' => "Votre demande d'audience est confirmée.",
        'info_sup' => "Veuillez venir 10 minutes avant.",
        'image' => 'https://ifri-uac.bj/assets/img/logouac.png'
    ];

    return new AppointmentMail($fakeData);
});
Route::get('/preview-ask-advice', function () {
    $fakeData = [
        'name' => 'John Doe',
        'appointment_name' => "Michael Scofield",
        'subject' => 'Test Demande d\'audience',
        'object' => 'Consultation',
        'message' => "...",
        'service' => "Recteur",
        'image' => 'https://ifri-uac.bj/assets/img/logouac.png'
    ];

    return new AskAdviceMail($fakeData);
});

Route::get('/preview-give-advice', function () {
    $fakeData = [
        'name' => 'John Doe',
        'appointment_name' => "Michael Scofield",
        'subject' => 'Test Demande d\'audience',
        'object' => 'Consultation',
        'service' => "Recteur",
        'image' => 'https://ifri-uac.bj/assets/img/logouac.png'
    ];

    return new GiveAdviceMail($fakeData);
});

Route::get('/services', function () {
    return response()->json(\App\Models\Service::all());
});

Route::get('service-list', [ServiceController::class, 'index_list'])->name('services.list');
Route::get('track-request/{id}', [AppointmentController::class, 'track'])->name('track');

Route::middleware('guest')->group(function () {
    Route::get('user/login', [LoginController::class, 'showLoginForm'])->name('login');
    Route::get('user/register', [LoginController::class, 'showRegisterForm'])->name('register');
    Route::post('user/register', [LoginController::class, 'register'])->name('register.submit');

    // Soumission login
    Route::post('/user/login', [LoginController::class, 'login'])->name('login.submit');
});

// Logout
Route::post('logout', [LoginController::class, 'logout'])->name('logout');

Route::prefix('admin')->name('admin.')->group(function () {
    Route::get('appointments', [AppointmentAdminController::class, 'index'])
        ->middleware(['auth', AdminMiddleware::class])
        ->name('appointments.index');

    Route::get('appointments/{appointment}', [AppointmentAdminController::class, 'show'])
        ->middleware(['auth', AdminMiddleware::class])
        ->name('appointments.show');

    Route::patch('appointments/{appointment}/confirm', [AppointmentAdminController::class, 'confirm'])
        ->middleware(['auth', AdminMiddleware::class])
        ->name('appointments.confirm');

    Route::patch('appointments/{appointment}/reject', [AppointmentAdminController::class, 'reject'])
        ->middleware(['auth', AdminMiddleware::class])
        ->name('appointments.reject');
    Route::patch('appointments/{appointment}/cancel', [AppointmentAdminController::class, 'cancel'])
        ->middleware(['auth', AdminMiddleware::class])
        ->name('appointments.cancel');
    Route::patch('appointments/{appointment}/reschedule', [AppointmentAdminController::class, 'reschedule'])
        ->middleware(['auth', AdminMiddleware::class])
        ->name('appointments.reschedule');
    Route::patch('appointments/{appointment}/repending', [AppointmentAdminController::class, 'repending'])
        ->middleware(['auth', AdminMiddleware::class])
        ->name('appointments.repending');
    Route::patch('appointments/{appointment}/processing', [AppointmentAdminController::class, 'processing'])
        ->middleware(['auth', AdminMiddleware::class])
        ->name('appointments.processing');
    Route::patch('appointments/{appointment}/status', [AppointmentAdminController::class, 'updateStatus'])
        ->middleware(['auth', AdminMiddleware::class])
        ->name('appointments.updateStatus');
    Route::patch(
        '/appointments/{appointment}/ask-opinion',
        [AppointmentAdminController::class, 'askOpinion']
    )->middleware(['auth', AdminMiddleware::class])->name('admin.appointments.ask-opinion');

    Route::patch(
        '/appointments/{appointment}/give-advice',
        [AppointmentAdminController::class, 'giveAdvice']
    )->middleware(['auth', AdminMiddleware::class])->name('admin.appointments.give-opinion');
    Route::get('reports', [ReportController::class, 'index'])
        ->name('reports.index');

    Route::get(
        'appointments/{appointment}/reports/create',
        [ReportController::class, 'create']
    )->name('reports.create');

    Route::get(
        'reports/{report}',
        [ReportController::class, 'show']
    )->name('reports.show');

    Route::get(
        'reports/{report}/download',
        [ReportController::class, 'download']
    )->name('reports.download');

    Route::post(
        'appointments/{appointment}/reports',
        [ReportController::class, 'store']
    )
        ->name('reports.store');
    Route::get('reports/{report}/edit', [ReportController::class, 'edit'])->name('reports.edit');
    Route::put('reports/{report}', [ReportController::class, 'update'])->name('reports.update');
  Route::patch('reports/{report}/approve', [ReportController::class, 'approve'])
        ->name('reports.approve');

    Route::patch('reports/{report}/reject', [ReportController::class, 'reject'])
        ->name('reports.reject');
     Route::get(
        'audience-files/{audiencefile}',
        [AudienceFileController::class, 'show']
    )->name('audience-files.show');
       Route::get(
        'audience-files',
        [AudienceFileController::class, 'index']
    )->name('audience-files.index');

    Route::get('/technical-requests/{appointment}', [AppointmentAdminController::class, 'getTechnicalAdvice'])
        ->middleware(['auth', AdminMiddleware::class])
        ->name('appointments.technical-requests');


});
Route::middleware(['auth', CanValidateUsers::class])
    ->prefix('admin/users')
    ->name('admin.users.')
    ->group(function () {

        Route::get('/', [LoginController::class, 'index'])
            ->name('user.index');

        Route::patch('{user}/approve', [LoginController::class, 'approve'])
            ->name('user.approve');

        Route::patch('{user}/reject', [LoginController::class, 'reject'])
            ->name('user.reject');
    });