<?php

use CodeIgniter\Router\RouteCollection;

/** @var RouteCollection $routes */

// Root URL Redirect
$routes->get('/', function() {
    if (session()->get('logged_in')) {
        $role = session()->get('role');
        return redirect()->to("/{$role}/dashboard");
    }
    return redirect()->to('/login');
});

// Authentication Routes
$routes->get('/login', 'AuthController::login');
$routes->post('/login', 'AuthController::attemptLogin');
$routes->get('/logout', 'AuthController::logout');

// Wali Santri Routes
$routes->group('wali', ['filter' => 'auth:wali'], function($routes) {
    $routes->get('dashboard', 'WaliController::dashboard');
    $routes->get('riwayat', 'WaliController::riwayat');
    $routes->get('pengajuan', 'WaliController::pengajuan');
    $routes->post('pengajuan/submit', 'WaliController::submitPengajuan');
    $routes->get('izin/(:num)', 'WaliController::viewPermit/$1');
    $routes->get('qr/(:any)', 'WaliController::qrImage/$1');
});

// Kesantrian Routes
$routes->group('kesantrian', ['filter' => 'auth:kesantrian'], function($routes) {
    $routes->get('dashboard', 'KesantrianController::dashboard');
    $routes->get('riwayat', 'KesantrianController::riwayat');
    $routes->get('form-bypass', 'KesantrianController::formBypass');
    $routes->post('approve/(:num)', 'KesantrianController::approve/$1');
    $routes->post('reject/(:num)', 'KesantrianController::reject/$1');
    $routes->post('bypass', 'KesantrianController::bypass');
    $routes->get('monitoring', 'KesantrianController::monitoring');
});

// Satpam Routes
$routes->group('satpam', ['filter' => 'auth:satpam'], function($routes) {
    $routes->get('dashboard', 'SatpamController::dashboard');
    $routes->get('log', 'SatpamController::log');
    $routes->post('scan', 'SatpamController::scan');
});

// Admin Routes
$routes->group('admin', ['filter' => 'auth:admin'], function($routes) {
    $routes->get('dashboard', 'AdminController::dashboard');
    $routes->get('users', 'AdminController::users');
    $routes->get('santri', 'AdminController::santri');
    $routes->post('users/store', 'AdminController::usersStore');
    $routes->post('santri/store', 'AdminController::santriStore');
});
