<?php

use App\Http\Controllers\Api\Auth\AuthSessionController;
use App\Http\Controllers\Api\Auth\SessionTimeoutController;
use App\Http\Controllers\Api\System\DashboardRedirectController;
use App\Http\Controllers\Web\SchoolYearAdminPageController;
use App\Http\Controllers\Web\TeacherCalendarPageController;
use Illuminate\Support\Facades\Route;

/*
| Cookie session + browser-facing paths (SPA still calls same-origin /login, /logout, …).
| All REST business logic lives under routes/api.php → /api, /api/v1.
*/

Route::get('/', function () {
    return response()->json([
        'status' => 'ok',
        'service' => config('app.name', 'api'),
        'docs' => url('/docs'),
    ]);
})->name('api.home');

Route::get('docs', function () {
    $specUrl = url('/api/documentation/swagger.json');

    $html = <<<HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Alrahma API Docs</title>
    <link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5/swagger-ui.css">
    <style>
        body { margin: 0; background: #ffffff; }
        #swagger-ui { min-height: 100vh; }
    </style>
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js"></script>
<script>
window.addEventListener('load', function () {
    SwaggerUIBundle({
        url: "{$specUrl}",
        dom_id: '#swagger-ui',
        operationsSorter: 'alpha',
        tagsSorter: 'alpha',
        docExpansion: 'none',
        defaultModelsExpandDepth: -1,
        persistAuthorization: true
    });
});
</script>
</body>
</html>
HTML;

    return response($html, 200)->header('Content-Type', 'text/html; charset=UTF-8');
})->name('docs.ui');

Route::middleware('web')->group(function () {
    Route::get('login', [AuthSessionController::class, 'loginMask'])->name('login');
    Route::post('user/login', [AuthSessionController::class, 'login'])->name('auth.session.login');
    Route::post('logout', [AuthSessionController::class, 'logout'])->name('auth.session.logout');
    Route::get('select-role', [AuthSessionController::class, 'selectRole'])->name('auth.session.select-role');
    Route::post('set-role', [AuthSessionController::class, 'setRole'])->name('auth.session.set-role');

    Route::get('session/check-timeout', [SessionTimeoutController::class, 'checkTimeout'])
        ->name('auth.session.check-timeout');
    Route::get('session/check', [SessionTimeoutController::class, 'checkTimeout'])
        ->name('auth.session.check');
    Route::post('session/ping', [SessionTimeoutController::class, 'pingActivity'])
        ->name('auth.session.ping');
    Route::get('session/timeout-config', [SessionTimeoutController::class, 'getTimeoutConfig'])
        ->name('auth.session.timeout-config');

    Route::middleware('auth')->get('dashboard', [DashboardRedirectController::class, 'dashboard'])
        ->name('dashboard.redirect');
    Route::middleware('auth')->get('teacher/calendar', [TeacherCalendarPageController::class, 'show'])
        ->name('teacher.calendar');
    Route::middleware('auth')->get('app/teacher/calendar', [TeacherCalendarPageController::class, 'show'])
        ->name('app.teacher.calendar');
    Route::middleware(['auth', 'admin.access'])->get('administrator/school-years', [SchoolYearAdminPageController::class, 'show'])
        ->name('admin.school-years');
    Route::middleware(['auth', 'admin.access'])->get('app/administrator/school-years', [SchoolYearAdminPageController::class, 'show'])
        ->name('app.admin.school-years');
    Route::middleware(['auth', 'admin.access'])->post('administrator/school-years', [SchoolYearAdminPageController::class, 'store'])
        ->name('admin.school-years.store');
    Route::middleware(['auth', 'admin.access'])->patch('administrator/school-years/{schoolYear}', [SchoolYearAdminPageController::class, 'update'])
        ->whereNumber('schoolYear')
        ->name('admin.school-years.update');
    Route::middleware(['auth', 'admin.access'])->post('administrator/school-years/{schoolYear}/validate-close', [SchoolYearAdminPageController::class, 'validateClose'])
        ->whereNumber('schoolYear')
        ->name('admin.school-years.validate-close');
    Route::middleware(['auth', 'admin.access'])->post('administrator/school-years/{schoolYear}/preview-close', [SchoolYearAdminPageController::class, 'previewClose'])
        ->whereNumber('schoolYear')
        ->name('admin.school-years.preview-close');
    Route::middleware(['auth', 'admin.access'])->post('administrator/school-years/{schoolYear}/close', [SchoolYearAdminPageController::class, 'close'])
        ->whereNumber('schoolYear')
        ->name('admin.school-years.close');
    Route::middleware(['auth', 'admin.access'])->post('administrator/school-years/{schoolYear}/reopen', [SchoolYearAdminPageController::class, 'reopen'])
        ->whereNumber('schoolYear')
        ->name('admin.school-years.reopen');
    Route::middleware('auth')->get('teacher/absence-vacation', function () {
        return redirect('/app/teacher/absence-vacation');
    })->name('teacher.absence-vacation');
    Route::middleware('auth')->get('teacher/absence_vacation', function () {
        return redirect('/app/teacher/absence-vacation');
    })->name('teacher.absence_vacation');
});
