<?php
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;

return function (RouteBuilder $routes): void {
    $routes->setRouteClass(DashedRoute::class);

    $routes->scope('/', function (RouteBuilder $builder): void {
        $builder->connect('/', ['controller' => 'Pages', 'action' => 'home'], ['_name' => 'home']);
        $builder->connect('/route-help', ['controller' => 'Pages', 'action' => 'routeHelp'], ['_name' => 'route_help']);

        $builder->connect('/contact', ['controller' => 'ContactMessages', 'action' => 'add'], ['_name' => 'contact_add']);
        $builder->connect('/contact/success', ['controller' => 'ContactMessages', 'action' => 'success'], ['_name' => 'contact_success']);

        // Public teachers
        $builder->connect('/teachers', ['controller' => 'Teachers', 'action' => 'index'], ['_name' => 'teachers_index']);
        $builder->connect('/teachers/{slug}', ['controller' => 'Teachers', 'action' => 'profile'], [
            'pass' => ['slug'],
            '_name' => 'teacher_profile',
        ]);

        // Become-a-teacher application (public)
        $builder->connect('/teach', ['controller' => 'TeacherApplications', 'action' => 'add'], ['_name' => 'teacher_apply']);
        $builder->connect('/teach/success', ['controller' => 'TeacherApplications', 'action' => 'success'], ['_name' => 'teacher_apply_success']);

        // Booking flow (public)
        $builder->connect('/book', ['controller' => 'Bookings', 'action' => 'index'], ['_name' => 'booking_index']);
        $builder->connect('/book/teacher/{slug}', ['controller' => 'Bookings', 'action' => 'teacher'], [
            'pass' => ['slug'],
            '_name' => 'booking_teacher',
        ]);
        $builder->connect('/book/checkout', ['controller' => 'Bookings', 'action' => 'checkout'], ['_name' => 'booking_checkout']);
        $builder->connect('/book/success', ['controller' => 'Bookings', 'action' => 'success'], ['_name' => 'booking_success']);
        $builder->connect('/book/cancel', ['controller' => 'Bookings', 'action' => 'cancel'], ['_name' => 'booking_cancel']);

        // AI chat API
        $builder->connect('/api/chat', ['controller' => 'Chat', 'action' => 'send'], ['_name' => 'api_chat']);

        $builder->connect('/pages/*', 'Pages::display');
        $builder->fallbacks();
    });

    $routes->prefix('Admin', function (RouteBuilder $builder): void {
        $builder->connect('/login', ['controller' => 'Session', 'action' => 'login'], ['_name' => 'admin_login']);
        $builder->connect('/logout', ['controller' => 'Session', 'action' => 'logout'], ['_name' => 'admin_logout']);

        $builder->connect('/messages', ['controller' => 'ContactMessages', 'action' => 'index'], ['_name' => 'admin_messages']);
        $builder->connect('/messages/{id}', ['controller' => 'ContactMessages', 'action' => 'view'], [
            'id' => '\d+', 'pass' => ['id'], '_name' => 'admin_message_view',
        ]);
        $builder->connect('/messages/{id}/reply', ['controller' => 'ContactMessages', 'action' => 'reply'], [
            'id' => '\d+', 'pass' => ['id'], '_name' => 'admin_message_reply',
        ]);

        $builder->connect('/bookings', ['controller' => 'Bookings', 'action' => 'index'], ['_name' => 'admin_bookings']);

        // Teacher CRUD + availability
        $builder->connect('/teachers', ['controller' => 'Teachers', 'action' => 'index'], ['_name' => 'admin_teachers']);
        $builder->connect('/teachers/add', ['controller' => 'Teachers', 'action' => 'add'], ['_name' => 'admin_teacher_add']);
        $builder->connect('/teachers/{id}/edit', ['controller' => 'Teachers', 'action' => 'edit'], [
            'id' => '\d+', 'pass' => ['id'], '_name' => 'admin_teacher_edit',
        ]);
        $builder->connect('/teachers/{id}/deactivate', ['controller' => 'Teachers', 'action' => 'deactivate'], [
            'id' => '\d+', 'pass' => ['id'], '_name' => 'admin_teacher_deactivate',
        ]);
        $builder->connect('/teachers/{id}/slots/add', ['controller' => 'Teachers', 'action' => 'addSlot'], [
            'id' => '\d+', 'pass' => ['id'], '_name' => 'admin_teacher_add_slot',
        ]);
        $builder->connect('/teachers/{id}/slots/{slotId}/delete', ['controller' => 'Teachers', 'action' => 'deleteSlot'], [
            'id' => '\d+', 'slotId' => '\d+', 'pass' => ['id', 'slotId'], '_name' => 'admin_teacher_delete_slot',
        ]);
        $builder->connect('/teachers/{id}/slots/generate', ['controller' => 'Teachers', 'action' => 'generateSlots'], [
            'id' => '\d+', 'pass' => ['id'], '_name' => 'admin_teacher_generate',
        ]);

        // Teacher applications review
        $builder->connect('/applications', ['controller' => 'TeacherApplications', 'action' => 'index'], ['_name' => 'admin_applications']);
        $builder->connect('/applications/{id}', ['controller' => 'TeacherApplications', 'action' => 'view'], [
            'id' => '\d+', 'pass' => ['id'], '_name' => 'admin_application_view',
        ]);

        $builder->fallbacks();
    });
};
