<?php

use CodeIgniter\Router\RouteCollection;

/**
 * @var RouteCollection $routes
 */

// ===============================
// API ROUTES
// ===============================
$routes->group('api', function ($routes) {

    // HANDLE PREFLIGHT (CORS)
    $routes->options('(:any)', 'Preflight::options');

    // ===========================
    // AUTH (TANPA JWT)
    // ===========================
    $routes->post('login', 'Api\AuthController::login');

    // ===========================
    // PROTECTED API (JWT)
    // ===========================
    $routes->group('', ['filter' => 'jwt'], function ($routes) {

        // =======================
        // ADMIN ONLY
        // =======================
        $routes->group('', ['filter' => 'role:admin,petugas'], function ($routes) {
            $routes->post('barang', 'Api\BarangController::store');
            $routes->delete('barang/(:num)', 'Api\BarangController::delete/$1');
            $routes->get('log', 'Api\LogAktivitasController::index');
            // =======================
            // MASTER DATA
            // =======================
            $routes->get('kategori', 'Api\KategoriController::index');
            $routes->get('ruangan', 'Api\RuanganController::index');
             // kategori
            $routes->get('kategori', 'Api\KategoriController::index');
            $routes->post('kategori', 'Api\KategoriController::store');
            $routes->put('kategori/(:num)', 'Api\KategoriController::update/$1');

            // ruangan
            $routes->get('ruangan', 'Api\RuanganController::index');
            $routes->post('ruangan', 'Api\RuanganController::store');
            $routes->put('ruangan/(:num)', 'Api\RuanganController::update/$1');
            // penghapusan
            $routes->get('penghapusan', 'Api\PenghapusanController::index');
            $routes->post('penghapusan', 'Api\PenghapusanController::store');
            // Log Aktifitas
            $routes->get('log', 'Api\LogAktivitasController::index');
            // Migrasi Barang Unit
             $routes->get('barang-unit', 'Api\BarangUnitController::index');
            $routes->post('barang-unit', 'Api\BarangUnitController::store');
            $routes->put('barang-unit/(:num)', 'Api\BarangUnitController::update/$1');
    
        });

        // =======================
        // ADMIN & PETUGAS
        // =======================
        $routes->group('', ['filter' => 'role:admin,petugas'], function ($routes) {

            $routes->get('barang', 'Api\BarangController::index');
            $routes->put('barang/(:num)', 'Api\BarangController::update/$1');

            $routes->post('mutasi', 'Api\MutasiController::store');
            $routes->get('mutasi', 'Api\MutasiController::index');

            $routes->post('pemeliharaan', 'Api\PemeliharaanController::store');
            $routes->get('pemeliharaan', 'Api\PemeliharaanController::index');

            $routes->post('barcode/lookup', 'Api\BarcodeController::lookup');
            $routes->post('barcode/update', 'Api\BarcodeController::quickUpdate');
            $routes->get('mutasi', 'Api\MutasiController::index');
            $routes->post('mutasi', 'Api\MutasiController::store');
            $routes->get('pemeliharaan', 'Api\PemeliharaanController::index');
            $routes->post('pemeliharaan', 'Api\PemeliharaanController::store');
            $routes->post('barcode/lookup', 'Api\BarcodeController::lookup');
             $routes->get('kategori', 'Api\KategoriController::index');
            $routes->get('ruangan', 'Api\RuanganController::index');
             // kategori
            $routes->get('kategori', 'Api\KategoriController::index');
            $routes->post('kategori', 'Api\KategoriController::store');
            $routes->put('kategori/(:num)', 'Api\KategoriController::update/$1');

            // ruangan
            $routes->get('ruangan', 'Api\RuanganController::index');
            $routes->post('ruangan', 'Api\RuanganController::store');
            $routes->put('ruangan/(:num)', 'Api\RuanganController::update/$1');
        });

        // =======================
        // ADMIN & KEPALA
        // =======================
        $routes->group('', ['filter' => 'role:admin,kepala'], function ($routes) {
            $routes->get('dashboard/summary', 'Api\DashboardController::summary');
            $routes->get('dashboard/unit', 'Api\DashboardController::unit');
            $routes->get('user', 'Api\UserController::index');
    $routes->post('user', 'Api\UserController::store');
    $routes->put('user/(:num)', 'Api\UserController::update/$1');
    //$routes->delete('user/(:num)', 'Api\UserController::delete/$1');
        });
// =======================
// ADMIN & KEPALA (EXPORT)
// =======================
$routes->group('export', ['filter' => 'role:admin,kepala'], function ($routes) {
    $routes->get('mutasi', 'Api\ExportController::mutasi');
    $routes->get('pemeliharaan', 'Api\ExportController::pemeliharaan');
    $routes->get('pemusnahan', 'Api\ExportController::pemusnahan');
    $routes->get('barang-unit', 'Api\ExportController::barangUnit');
    

});

        // =======================
        // READ ONLY (SEMUA ROLE)
        // =======================
        $routes->group('', ['filter' => 'role:admin,petugas,kepala'], function ($routes) {
            $routes->get('barang/(:num)', 'Api\BarangController::show/$1');
            $routes->get('unit/(:num)/riwayat', 'Api\UnitRiwayatController::show/$1');

        });
        
    });
});