<?php

use CodeIgniter\Router\RouteCollection;

/**
 * @var RouteCollection $routes
 */
$routes->get('/', function () {
    return redirect()->to('/v2');
});

$routes->options('(:any)', function () {
    return '';
});

$routes->group('api', ['filter' => 'auth'], function ($routes) {
    $routes->get('dashboard', 'DashboardController::index');
    $routes->get('result', 'ResultController::index');
    $routes->get('sample', 'SampleController::index');
});

// Public Routes (no auth required)
$routes->get('/v2/login', 'PagesController::login');

// V2 Auth API Routes (public - no auth required)
$routes->group('v2/auth', function ($routes) {
    $routes->post('login', 'AuthV2Controller::login');
    $routes->post('register', 'AuthV2Controller::register');
    $routes->get('check', 'AuthV2Controller::checkAuth');
    $routes->post('logout', 'AuthV2Controller::logout');
});

// Protected Page Routes - V2 (requires auth)
$routes->group('v2', ['filter' => 'auth'], function ($routes) {
    $routes->get('/', 'PagesController::dashboard');
    $routes->get('dashboard', 'PagesController::dashboard');
    $routes->get('patients', 'PagesController::patients');
    $routes->get('requests', 'PagesController::requests');
    $routes->get('settings', 'PagesController::settings');

    // Master Data - Organization
    $routes->get('master/organization/accounts', 'PagesController::masterOrgAccounts');
    $routes->get('master/organization/sites', 'PagesController::masterOrgSites');
    $routes->get('master/organization/disciplines', 'PagesController::masterOrgDisciplines');
    $routes->get('master/organization/departments', 'PagesController::masterOrgDepartments');
    $routes->get('master/organization/workstations', 'PagesController::masterOrgWorkstations');

    // Master Data - Specimen
    $routes->get('master/specimen/containers', 'PagesController::masterSpecimenContainers');
    $routes->get('master/specimen/preparations', 'PagesController::masterSpecimenPreparations');

    // Master Data - Tests & ValueSets
    $routes->get('master/tests', 'PagesController::masterTests');
    $routes->get('master/valuesets', 'PagesController::masterValueSets');
});

// Faker
$routes->get('faker/faker-patient/(:num)', 'faker\FakerPatient::sendMany/$1');

$routes->group('api', function ($routes) {
    // Auth
    $routes->group('auth', function ($routes) {
        $routes->post('login', 'AuthController::login');
        $routes->post('change_pass', 'AuthController::change_pass');
        $routes->post('register', 'AuthController::register');
        $routes->get('check', 'AuthController::checkAuth');
        $routes->post('logout', 'AuthController::logout');
    });

    // Patient
    $routes->group('patient', function ($routes) {
        $routes->get('/', 'Patient\PatientController::index');
        $routes->post('/', 'Patient\PatientController::create');
        $routes->get('(:num)', 'Patient\PatientController::show/$1');
        $routes->delete('/', 'Patient\PatientController::delete');
        $routes->patch('/', 'Patient\PatientController::update');
        $routes->get('check', 'Patient\PatientController::patientCheck');
    });

    // PatVisit
    $routes->group('patvisit', function ($routes) {
        $routes->get('/', 'PatVisitController::index');
        $routes->post('/', 'PatVisitController::create');
        $routes->get('patient/(:num)', 'PatVisitController::showByPatient/$1');
        $routes->get('(:any)', 'PatVisitController::show/$1');
        $routes->delete('/', 'PatVisitController::delete');
        $routes->patch('/', 'PatVisitController::update');
    });

    $routes->group('patvisitadt', function ($routes) {
        $routes->post('/', 'PatVisitController::createADT');
        $routes->patch('/', 'PatVisitController::updateADT');
    });

    // Master Data
    $routes->group('race', function ($routes) {
        $routes->get('/', 'Race::index');
        $routes->get('(:num)', 'Race::show/$1');
    });

    $routes->group('country', function ($routes) {
        $routes->get('/', 'Country::index');
        $routes->get('(:num)', 'Country::show/$1');
    });

    $routes->group('religion', function ($routes) {
        $routes->get('/', 'Religion::index');
        $routes->get('(:num)', 'Religion::show/$1');
    });

    $routes->group('ethnic', function ($routes) {
        $routes->get('/', 'Ethnic::index');
        $routes->get('(:num)', 'Ethnic::show/$1');
    });

    // Location
    $routes->group('location', function ($routes) {
        $routes->get('/', 'LocationController::index');
        $routes->get('(:num)', 'LocationController::show/$1');
        $routes->post('/', 'LocationController::create');
        $routes->patch('/', 'LocationController::update');
        $routes->delete('/', 'LocationController::delete');
    });

    // Contact
    $routes->group('contact', function ($routes) {
        $routes->get('/', 'Contact\ContactController::index');
        $routes->get('(:num)', 'Contact\ContactController::show/$1');
        $routes->post('/', 'Contact\ContactController::create');
        $routes->patch('/', 'Contact\ContactController::update');
        $routes->delete('/', 'Contact\ContactController::delete');
    });

    $routes->group('occupation', function ($routes) {
        $routes->get('/', 'Contact\OccupationController::index');
        $routes->get('(:num)', 'Contact\OccupationController::show/$1');
        $routes->post('/', 'Contact\OccupationController::create');
        $routes->patch('/', 'Contact\OccupationController::update');
        //$routes->delete('/', 'Contact\OccupationController::delete');
    });

    $routes->group('medicalspecialty', function ($routes) {
        $routes->get('/', 'Contact\MedicalSpecialtyController::index');
        $routes->get('(:num)', 'Contact\MedicalSpecialtyController::show/$1');
        $routes->post('/', 'Contact\MedicalSpecialtyController::create');
        $routes->patch('/', 'Contact\MedicalSpecialtyController::update');
    });

    // ValueSet
    $routes->group('valueset', function ($routes) {
        $routes->get('/', 'ValueSet\ValueSetController::index');
        $routes->get('(:num)', 'ValueSet\ValueSetController::show/$1');
        $routes->get('valuesetdef/(:num)', 'ValueSet\ValueSetController::showByValueSetDef/$1');
        $routes->post('/', 'ValueSet\ValueSetController::create');
        $routes->patch('/', 'ValueSet\ValueSetController::update');
        $routes->delete('/', 'ValueSet\ValueSetController::delete');
    });

    $routes->group('valuesetdef', function ($routes) {
        $routes->get('/', 'ValueSet\ValueSetDefController::index');
        $routes->get('(:segment)', 'ValueSet\ValueSetDefController::show/$1');
        $routes->post('/', 'ValueSet\ValueSetDefController::create');
        $routes->patch('/', 'ValueSet\ValueSetDefController::update');
        $routes->delete('/', 'ValueSet\ValueSetDefController::delete');
    });

    // Counter
    $routes->group('counter', function ($routes) {
        $routes->get('/', 'CounterController::index');
        $routes->get('(:num)', 'CounterController::show/$1');
        $routes->post('/', 'CounterController::create');
        $routes->patch('/', 'CounterController::update');
        $routes->delete('/', 'CounterController::delete');
    });

    // AreaGeo
    $routes->group('areageo', function ($routes) {
        $routes->get('/', 'AreaGeoController::index');
        $routes->get('provinces', 'AreaGeoController::getProvinces');
        $routes->get('cities', 'AreaGeoController::getCities');
    });

    // Organization
    $routes->group('organization', function ($routes) {
        // Account
        $routes->group('account', function ($routes) {
            $routes->get('/', 'Organization\AccountController::index');
            $routes->get('(:num)', 'Organization\AccountController::show/$1');
            $routes->post('/', 'Organization\AccountController::create');
            $routes->patch('/', 'Organization\AccountController::update');
            $routes->delete('/', 'Organization\AccountController::delete');
        });

        // Site
        $routes->group('site', function ($routes) {
            $routes->get('/', 'Organization\SiteController::index');
            $routes->get('(:num)', 'Organization\SiteController::show/$1');
            $routes->post('/', 'Organization\SiteController::create');
            $routes->patch('/', 'Organization\SiteController::update');
            $routes->delete('/', 'Organization\SiteController::delete');
        });

        // Discipline
        $routes->group('discipline', function ($routes) {
            $routes->get('/', 'Organization\DisciplineController::index');
            $routes->get('(:num)', 'Organization\DisciplineController::show/$1');
            $routes->post('/', 'Organization\DisciplineController::create');
            $routes->patch('/', 'Organization\DisciplineController::update');
            $routes->delete('/', 'Organization\DisciplineController::delete');
        });

        // Department
        $routes->group('department', function ($routes) {
            $routes->get('/', 'Organization\DepartmentController::index');
            $routes->get('(:num)', 'Organization\DepartmentController::show/$1');
            $routes->post('/', 'Organization\DepartmentController::create');
            $routes->patch('/', 'Organization\DepartmentController::update');
            $routes->delete('/', 'Organization\DepartmentController::delete');
        });

        // Workstation
        $routes->group('workstation', function ($routes) {
            $routes->get('/', 'Organization\WorkstationController::index');
            $routes->get('(:num)', 'Organization\WorkstationController::show/$1');
            $routes->post('/', 'Organization\WorkstationController::create');
            $routes->patch('/', 'Organization\WorkstationController::update');
            $routes->delete('/', 'Organization\WorkstationController::delete');
        });
    });

    // Specimen
    $routes->group('specimen', function ($routes) {
        // Container aliases - 'container' and 'containerdef' both point to ContainerDefController
        $routes->group('container', function ($routes) {
            $routes->get('/', 'Specimen\ContainerDefController::index');
            $routes->get('(:num)', 'Specimen\ContainerDefController::show/$1');
            $routes->post('/', 'Specimen\ContainerDefController::create');
            $routes->patch('/', 'Specimen\ContainerDefController::update');
        });
        $routes->group('containerdef', function ($routes) {
            $routes->get('/', 'Specimen\ContainerDefController::index');
            $routes->get('(:num)', 'Specimen\ContainerDefController::show/$1');
            $routes->post('/', 'Specimen\ContainerDefController::create');
            $routes->patch('/', 'Specimen\ContainerDefController::update');
        });

        $routes->group('prep', function ($routes) {
            $routes->get('/', 'Specimen\SpecimenPrepController::index');
            $routes->get('(:num)', 'Specimen\SpecimenPrepController::show/$1');
            $routes->post('/', 'Specimen\SpecimenPrepController::create');
            $routes->patch('/', 'Specimen\SpecimenPrepController::update');
        });

        $routes->group('status', function ($routes) {
            $routes->get('/', 'Specimen\SpecimenStatusController::index');
            $routes->get('(:num)', 'Specimen\SpecimenStatusController::show/$1');
            $routes->post('/', 'Specimen\SpecimenStatusController::create');
            $routes->patch('/', 'Specimen\SpecimenStatusController::update');
        });

        $routes->group('collection', function ($routes) {
            $routes->get('/', 'Specimen\SpecimenCollectionController::index');
            $routes->get('(:num)', 'Specimen\SpecimenCollectionController::show/$1');
            $routes->post('/', 'Specimen\SpecimenCollectionController::create');
            $routes->patch('/', 'Specimen\SpecimenCollectionController::update');
        });

        $routes->get('/', 'Specimen\SpecimenController::index');
        $routes->get('(:num)', 'Specimen\SpecimenController::show/$1');
        $routes->post('/', 'Specimen\SpecimenController::create');
        $routes->patch('/', 'Specimen\SpecimenController::update');
    });

    // Tests
    $routes->group('tests', function ($routes) {
        $routes->get('/', 'TestsController::index');
        $routes->get('(:num)', 'TestsController::show/$1');
        $routes->post('/', 'TestsController::create');
        $routes->patch('/', 'TestsController::update');
    });

    // Edge API - Integration with tiny-edge
    $routes->group('edge', function ($routes) {
        $routes->post('results', 'EdgeController::results');
        $routes->get('orders', 'EdgeController::orders');
        $routes->post('orders/(:num)/ack', 'EdgeController::ack/$1');
        $routes->post('status', 'EdgeController::status');
    });
});

// Khusus
/*
$routes->get('/api/zones', 'Zones::index');
$routes->get('/api/zones/synchronize', 'Zones::synchronize');
$routes->get('/api/zones/provinces', 'Zones::getProvinces');
$routes->get('/api/zones/cities', 'Zones::getCities');
*/

