<?php

use CodeIgniter\Router\RouteCollection;

/** @var RouteCollection $routes */

// ─── Custom auth routes (override Shield defaults) ──────────────────────────
$routes->get('login',   'Auth\LoginController::loginView');
$routes->post('login',  'Auth\LoginController::loginAction');
$routes->get('logout',  'Auth\LoginController::logoutAction');
$routes->post('logout', 'Auth\LoginController::logoutAction');

// Shield routes for magic-link etc.
service('auth')->routes($routes);

// ─── Public routes (no auth required) ───────────────────────────────────────
$routes->get('/',                       'PublicController::index');
$routes->get('documents',               'PublicController::index');
$routes->get('documents/(:num)',        'PublicController::show/$1');
$routes->get('covers/(:segment)',       'PublicController::cover/$1');

// ─── API routes (AJAX — for citation search) ────────────────────────────────
$routes->get('api/documents/search',    'ApiController::searchDocuments');

// ─── Authenticated routes ────────────────────────────────────────────────────
$routes->group('', ['filter' => 'auth'], static function ($routes) {
    $routes->get('documents/(:num)/read',     'PublicController::read/$1');
    $routes->get('documents/(:num)/stream',   'PublicController::stream/$1');
    $routes->get('documents/(:num)/download', 'PublicController::download/$1');
});

// ─── Student / Lecturer submission routes ───────────────────────────────────
$routes->group('', ['filter' => 'role:student,lecturer,super_admin'], static function ($routes) {
    $routes->get('submit',                  'SubmissionController::create');
    $routes->post('submit',                 'SubmissionController::store');
    $routes->get('submit/(:num)/edit',      'SubmissionController::edit/$1');
    $routes->post('submit/(:num)/update',   'SubmissionController::update/$1');
    $routes->post('submit/(:num)/send',     'SubmissionController::send/$1');
    $routes->post('submit/(:num)/delete',   'SubmissionController::delete/$1');
    $routes->get('my-works',                'SubmissionController::index');
    $routes->get('my-works/(:num)',         'SubmissionController::show/$1');
});

// ─── Admin routes ────────────────────────────────────────────────────────────
$routes->group('admin', ['filter' => 'role:admin,super_admin'], static function ($routes) {
    $routes->get('/',           'Admin\DashboardController::index');

    // Documents
    $routes->get('documents',                       'Admin\DocumentController::index');
    $routes->get('documents/new',                   'Admin\DocumentController::create');
    $routes->post('documents',                      'Admin\DocumentController::store');
    $routes->get('documents/(:num)',                'Admin\DocumentController::show/$1');
    $routes->get('documents/(:num)/edit',           'Admin\DocumentController::edit/$1');
    $routes->post('documents/(:num)/update',        'Admin\DocumentController::update/$1');
    $routes->post('documents/(:num)/delete',        'Admin\DocumentController::delete/$1');
    $routes->post('documents/(:num)/approve',       'Admin\DocumentController::approve/$1');
    $routes->post('documents/(:num)/reject',        'Admin\DocumentController::reject/$1');
    $routes->post('documents/(:num)/revision',      'Admin\DocumentController::requestRevision/$1');
    $routes->post('documents/(:num)/unpublish',     'Admin\DocumentController::unpublish/$1');

    // Users
    $routes->get('users',                   'Admin\UserController::index');
    $routes->get('users/new',               'Admin\UserController::create');
    $routes->post('users',                  'Admin\UserController::store');
    $routes->get('users/(:num)/edit',       'Admin\UserController::edit/$1');
    $routes->post('users/(:num)/update',    'Admin\UserController::update/$1');
    $routes->post('users/(:num)/toggle',    'Admin\UserController::toggleStatus/$1');
    $routes->get('users/import',            'Admin\UserController::importForm');
    $routes->post('users/import',           'Admin\UserController::import');

    // Study Programs
    $routes->get('programs',                        'Admin\StudyProgramController::index');
    $routes->post('programs',                       'Admin\StudyProgramController::store');
    $routes->post('programs/(:num)/update',         'Admin\StudyProgramController::update/$1');
    $routes->post('programs/(:num)/delete',         'Admin\StudyProgramController::delete/$1');

    // Reports
    $routes->get('reports',                 'Admin\ReportController::index');
    $routes->get('reports/export',          'Admin\ReportController::export');

    // Logs
    $routes->get('logs',                    'Admin\LogController::index');
});
