<?php

use CodeIgniter\Router\RouteCollection;

/**
 * @var RouteCollection $routes
 */

$routes->addRedirect('frontend/(.*)', '/');

// Rute publik untuk webhook dari payment gateway dan external link
$routes->post('webhook/payment', 'WebhookController::handlePayment');
$routes->get('verify/link/(:hash)', 'Frontend\AuthController::processVerificationLink/$1', ['as' => 'verify.link']);
$routes->get('profile/verify-email/(:hash)', 'Frontend\ProfileController::verifyNewEmail/$1', ['as' => 'profile.email.verify']);


// Rute ke login
$routes->get('/', 'Frontend\AuthController::showLoginForm', ['as' => 'login.show', 'filter' => 'guest']);

/**
 * Public Frontend Routes (No Login Required)
 */
$routes->group('', ['filter' => 'guest'], static function($routes) {
    $routes->get('login', 'Frontend\AuthController::showLoginForm');
    $routes->post('login', 'Frontend\AuthController::processLogin', ['as' => 'login.attempt']);
    $routes->get('register', 'Frontend\RegistrationController::showRegistrationForm', ['as' => 'register.show']);
    $routes->post('register', 'Frontend\RegistrationController::processRegistration', ['as' => 'register.create']);

    $routes->get('verify', 'Frontend\AuthController::showVerificationChoice', ['as' => 'verify.choice']);
    $routes->post('verify/send', 'Frontend\AuthController::sendVerificationLink', ['as' => 'verify.send']);
    
    // --- RUTE FORGOT PASSWORD ---
    $routes->get('forgot-password', 'Frontend\AuthController::showForgotPasswordForm', ['as' => 'forgot.form']);
    $routes->post('forgot-password', 'Frontend\AuthController::sendResetLink', ['as' => 'forgot.send']);
    $routes->get('reset-password/(:hash)', 'Frontend\AuthController::showResetForm/$1', ['as' => 'reset.form']);
    $routes->post('reset-password', 'Frontend\AuthController::processReset', ['as' => 'reset.process']);
});

/**
 * Protected Frontend Routes (Requires Login)
 */
$routes->group('', ['filter' => 'authUser'], static function($routes) {
    // --- Rute Dashboard & Logout ---
    $routes->get('dashboard', 'Frontend\DashboardController::showDashboard', ['as' => 'dashboard.show']);
    $routes->get('logout', 'Frontend\AuthController::logout', ['as' => 'logout']);

    // --- Rute Profil ---
    $routes->get('profile', 'Frontend\ProfileController::index', ['as' => 'profile.show']);
    $routes->post('profile/update-details', 'Frontend\ProfileController::updateDetails', ['as' => 'profile.update.details']);
    $routes->post('profile/update-password', 'Frontend\ProfileController::updatePassword', ['as' => 'profile.update.password']);
    $routes->post('profile/request-email-change', 'Frontend\ProfileController::requestEmailChange', ['as' => 'profile.email.request']);

    // --- Rute Transaksi ---
    $routes->get('transaction/new', 'Frontend\TransactionController::new', ['as' => 'transaction.new']);
    $routes->get('transaction/register/(:segment)', 'Frontend\TransactionController::showParticipantForm/$1', ['as' => 'transaction.participants.form']);
    $routes->post('transaction/create', 'Frontend\TransactionController::create', ['as' => 'transaction.create']);
    $routes->post('transaction/validate-promo', 'Frontend\TransactionController::validatePromo', ['as' => 'transaction.promo.validate']);
    $routes->get('transaction/(:segment)', 'Frontend\TransactionController::show/$1', ['as' => 'transaction.show']);
    $routes->get('transactions/pdf/(:segment)', 'Frontend\TransactionController::downloadPdf/$1', [
        'as' => 'transaction.pdf'
    ]);
    $routes->get('terms/(:segment)', 'Frontend\TransactionController::showTerms/$1', ['as' => 'terms.show']);

});


$routes->group('backend', static function($routes) {

    // --- Rute Login & Logout Admin ---
    $routes->get('login', 'Backend\AuthController::showLoginForm', ['as' => 'admin.login.show', 'filter' => 'guest:admin']);
    $routes->post('login', 'Backend\AuthController::processLogin', ['as' => 'admin.login.attempt']);
    $routes->get('logout', 'Backend\AuthController::logout', ['as' => 'admin.logout']);
    
    // Semua perlu login admin
    $routes->group('', ['filter' => 'authAdmin'], static function($routes) {

        // Util
        $routes->get('utils/clear-system-cache', 'Backend\DashboardController::clearCache', [
            'as' => 'admin.clear_cache',
            'filter' => 'permission:manage-system' // Anda perlu membuat permission ini
        ]);

        // Dashboard
        $routes->get('dashboard', 'Backend\DashboardController::index', ['as' => 'admin.dashboard']);

        // PROFILE
        $routes->get('profile', 'Backend\ProfileController::index', ['as' => 'admin.profile.show']);
        $routes->post('profile/update-password', 'Backend\ProfileController::updatePassword', ['as' => 'admin.profile.update_password']);

        // --- RUTE BARU UNTUK USER MANAGEMENT ---
        $routes->get('users', 'Backend\UsersController::index', ['as' => 'admin.users.index', 'filter' => 'permission:view-user-list']);
        $routes->get('users/edit/(:num)', 'Backend\UsersController::edit/$1', ['as' => 'admin.users.edit', 'filter' => 'permission:edit-user']);
        $routes->post('users/update/(:num)', 'Backend\UsersController::update/$1', ['as' => 'admin.users.update', 'filter' => 'permission:edit-user']);
        $routes->post('users/delete/(:num)', 'Backend\UsersController::delete/$1', ['as' => 'admin.users.delete', 'filter' => 'permission:delete-user']);

        // --- Rute Churches ----
        $routes->get('churches', 'Backend\ChurchesController::index', ['as' => 'admin.churches.index', 'filter' => 'permission:view-church-list']);
        $routes->get('churches/new', 'Backend\ChurchesController::new', ['as' => 'admin.churches.new', 'filter' => 'permission:create-church']);
        $routes->post('churches', 'Backend\ChurchesController::create', ['as' => 'admin.churches.create', 'filter' => 'permission:create-church']);
        $routes->get('churches/edit/(:num)', 'Backend\ChurchesController::edit/$1', ['as' => 'admin.churches.edit', 'filter' => 'permission:edit-church']);
        $routes->post('churches/update/(:num)', 'Backend\ChurchesController::update/$1', ['as' => 'admin.churches.update', 'filter' => 'permission:edit-church']);
        $routes->post('churches/delete/(:num)', 'Backend\ChurchesController::delete/$1', ['as' => 'admin.churches.delete', 'filter' => 'permission:delete-church']);

        // --- Rute Event ---
        $routes->group('events', ['filter' => 'permission:view-event-list'], static function($routes) {
            
            $routes->get('/', 'Backend\EventsController::index', ['as' => 'admin.events.index']);
            $routes->get('new', 'Backend\EventsController::new', ['as' => 'admin.events.new', 'filter' => 'permission:create-event']);
            $routes->post('/', 'Backend\EventsController::create', ['as' => 'admin.events.create', 'filter' => 'permission:create-event']);
            
            // Halaman hub event
            $routes->get('(:num)', 'Backend\EventsController::show/$1', ['as' => 'admin.events.show']);
            
            // Edit & Update Event Utama
            $routes->get('(:num)/edit', 'Backend\EventsController::edit/$1', ['as' => 'admin.events.edit', 'filter' => 'permission:edit-event']);
            $routes->post('(:num)/update', 'Backend\EventsController::update/$1', ['as' => 'admin.events.update', 'filter' => 'permission:edit-event']);
            $routes->post('(:num)/delete', 'Backend\EventsController::delete/$1', ['as' => 'admin.events.delete', 'filter' => 'permission:delete-event']);

            // --- RUTE UNTUK EVENT PACKAGES (EKSPLISIT) ---
            $routes->get('(:num)/packages/new', 'Backend\EventPackagesController::new/$1', ['as' => 'admin.events.packages.new', 'filter' => 'permission:create-event']);
            $routes->post('(:num)/packages', 'Backend\EventPackagesController::create/$1', ['as' => 'admin.events.packages.create', 'filter' => 'permission:create-event']);
            $routes->get('(:num)/packages/(:num)/edit', 'Backend\EventPackagesController::edit/$1/$2', ['as' => 'admin.events.packages.edit', 'filter' => 'permission:edit-event']);
            $routes->post('(:num)/packages/(:num)', 'Backend\EventPackagesController::update/$1/$2', ['as' => 'admin.events.packages.update', 'filter' => 'permission:edit-event']);
            $routes->post('(:num)/packages/(:num)/delete', 'Backend\EventPackagesController::delete/$1/$2', ['as' => 'admin.events.packages.delete', 'filter' => 'permission:edit-event']);


            // --- RUTE UNTUK EVENT PAYMENT METHOD (EKSPLISIT) ---
            $routes->get('(:num)/payment_method/new', 'Backend\EventPaymentMethodController::new/$1', ['as' => 'admin.events.payment_method.new', 'filter' => 'permission:create-event']);
            $routes->post('(:num)/payment_method', 'Backend\EventPaymentMethodController::create/$1', ['as' => 'admin.events.payment_method.create', 'filter' => 'permission:create-event']);
            $routes->get('(:num)/payment_method/(:num)/edit', 'Backend\EventPaymentMethodController::edit/$1/$2', ['as' => 'admin.events.payment_method.edit', 'filter' => 'permission:edit-event']);
            $routes->post('(:num)/payment_method/(:num)', 'Backend\EventPaymentMethodController::update/$1/$2', ['as' => 'admin.events.payment_method.update', 'filter' => 'permission:edit-event']);
            $routes->post('(:num)/payment_method/(:num)/delete', 'Backend\EventPaymentMethodController::delete/$1/$2', ['as' => 'admin.events.payment_method.delete', 'filter' => 'permission:edit-event']);

            // --- RUTE UNTUK EVENT CUSTOM FIELD (EKSPLISIT) ---
            $routes->get('(:num)/event_fields/new', 'Backend\EventFieldsController::new/$1', ['as' => 'admin.events.event_fields.new', 'filter' => 'permission:create-event']);
            $routes->post('(:num)/event_fields', 'Backend\EventFieldsController::create/$1', ['as' => 'admin.events.event_fields.create', 'filter' => 'permission:create-event']);
            $routes->get('(:num)/event_fields/(:num)/edit', 'Backend\EventFieldsController::edit/$1/$2', ['as' => 'admin.events.event_fields.edit', 'filter' => 'permission:edit-event']);
            $routes->post('(:num)/event_fields/(:num)', 'Backend\EventFieldsController::update/$1/$2', ['as' => 'admin.events.event_fields.update', 'filter' => 'permission:edit-event']);
            $routes->post('(:num)/event_fields/(:num)/delete', 'Backend\EventFieldsController::delete/$1/$2', ['as' => 'admin.events.event_fields.delete', 'filter' => 'permission:edit-event']);
        });

        // --- Rute Promotion ---
        $routes->get('promotions', 'Backend\PromotionsController::index', ['as' => 'admin.promotions.index', 'filter' => 'permission:view-promotion-list']);
        $routes->get('promotions/new', 'Backend\PromotionsController::new', ['as' => 'admin.promotions.new', 'filter' => 'permission:create-promotion']);
        $routes->post('promotions', 'Backend\PromotionsController::create', ['as' => 'admin.promotions.create', 'filter' => 'permission:create-promotion']);
        $routes->get('promotions/edit/(:num)', 'Backend\PromotionsController::edit/$1', ['as' => 'admin.promotions.edit', 'filter' => 'permission:edit-promotion']);
        $routes->post('promotions/update/(:num)', 'Backend\PromotionsController::update/$1', ['as' => 'admin.promotions.update', 'filter' => 'permission:edit-promotion']);
        $routes->post('promotions/delete/(:num)', 'Backend\PromotionsController::delete/$1', ['as' => 'admin.promotions.delete', 'filter' => 'permission:delete-promotion']);
        
        // --- Rute Transaksi ---
        $routes->get('transactions', 'Backend\DataTransactionController::index', [
            'as' => 'admin.transactions.index',
            'filter' => 'permission:view-transaction-list'
        ]);
        $routes->get('transactions/show/(:segment)', 'Backend\DataTransactionController::show/$1', [
            'as' => 'admin.transactions.show',
            'filter' => 'permission:view-transaction-detail' // Gunakan permission baru
        ]);

        $routes->get('transactions/pdf/(:segment)', 'Backend\DataTransactionController::downloadPdf/$1', [
            'as' => 'admin.transactions.pdf', 
            'filter' => 'permission:view-transaction-detail'
        ]);

        $routes->resource('transactions', [
            'controller' => 'Backend\DataTransactionController',
            'as'         => 'admin.transactions',
            'placeholder'=> '(:segment)',
            'except'     => 'index, show' // Kecualikan index dan show
        ]);
        
        $routes->post('transactions/update-status/(:segment)', 'Backend\DataTransactionController::updateStatus/$1', [
            'as' => 'admin.transactions.update_status',
            'filter' => 'permission:edit-transaction'
        ]);

        $routes->post('transactions/resend-notification/(:segment)', 'Backend\DataTransactionController::resendNotification/$1', 
            ['as' => 'admin.transactions.resend_notification', 
            'filter' => 'permission:edit-transaction']);



        $routes->get('transactions/participants/edit/(:num)', 'Backend\DataTransactionController::editParticipant/$1', [
            'as' => 'admin.participants.edit', 
            'filter' => 'permission:edit-participant'
        ]);
        $routes->post('transactions/participants/update/(:num)', 'Backend\DataTransactionController::updateParticipant/$1', [
            'as' => 'admin.participants.update', 
            'filter' => 'permission:edit-participant'
        ]);

        // --- Rute Bulk Transactions
        $routes->get('bulk-transactions', 'Backend\BulkTransactionsController::index', ['as' => 'admin.bulk.index', 'filter' => 'permission:view-bulk-transaction']);
        $routes->get('bulk-transactions/new', 'Backend\BulkTransactionsController::new', ['as' => 'admin.bulk.new', 'filter' => 'permission:create-bulk-transaction']);
        $routes->post('bulk-transactions', 'Backend\BulkTransactionsController::create', ['as' => 'admin.bulk.create', 'filter' => 'permission:create-bulk-transaction']);
        $routes->get('bulk-transactions/show/(:segment)', 'Backend\BulkTransactionsController::show/$1', ['as' => 'admin.bulk.show', 'filter' => 'permission:create-bulk-transaction']);
        $routes->post('bulk-transactions/(:num)/delete', 'Backend\BulkTransactionsController::delete/$1', ['as' => 'admin.bulk.delete', 'filter' => 'permission:create-bulk-transaction']); 
        $routes->post('bulk-transactions/(:num)/process', 'Backend\BulkTransactionsController::process/$1', ['as' => 'admin.bulk.process', 'filter' => 'permission:create-bulk-transaction' ]);
        // Rute untuk mengambil paket berdasarkan event via AJAX
        $routes->get('ajax/packages/(:segment)', 'Backend\AjaxController::getPackagesByEvent/$1', ['as' => 'admin.ajax.packages']);
        $routes->get('ajax/users', 'Backend\AjaxController::searchUsers', ['as' => 'admin.ajax.users']);
        $routes->get('ajax/churches', 'Backend\AjaxController::searchChurches', ['as' => 'admin.ajax.churches']);

        // --- Rute Report Peserta ---
        $routes->get('reports/participants', 'Backend\ParticipantReportController::index', [
            'as' => 'admin.reports.participants',
            'filter' => 'permission:view-participant-report' // Kita butuh permission baru
        ]);

        $routes->get('reports/participants/pdf/(:num)', 'Backend\ParticipantReportController::downloadTicket/$1', [
            'as' => 'admin.reports.participant_pdf', 
            'filter' => 'permission:view-participant-report'
        ]);

        $routes->get('reports/participants/export', 'Backend\ParticipantReportController::exportExcel', [
            'as' => 'admin.reports.participants_export', 
            'filter' => 'permission:view-participant-report'
        ]);

        // --- Rute Admin ---
        $routes->get('admins', 'Backend\AdminsController::index', ['as' => 'admin.admins.index', 'filter' => 'permission:view-admin-list']);
        $routes->get('admins/new', 'Backend\AdminsController::new', ['as' => 'admin.admins.new', 'filter' => 'permission:create-admin']);
        $routes->post('admins', 'Backend\AdminsController::create', ['as' => 'admin.admins.create', 'filter' => 'permission:create-admin']);
        $routes->get('admins/edit/(:num)', 'Backend\AdminsController::edit/$1', ['as' => 'admin.admins.edit', 'filter' => 'permission:edit-admin']);
        $routes->post('admins/update/(:num)', 'Backend\AdminsController::update/$1', ['as' => 'admin.admins.update', 'filter' => 'permission:edit-admin']);
        $routes->post('admins/delete/(:num)', 'Backend\AdminsController::delete/$1', ['as' => 'admin.admins.delete', 'filter' => 'permission:delete-admin']);

        // --- Rute Roles ---
        $routes->get('roles', 'Backend\RolesController::index', ['as' => 'admin.roles.index', 'filter' => 'permission:view-role-list']);
        $routes->get('roles/new', 'Backend\RolesController::new', ['as' => 'admin.roles.new', 'filter' => 'permission:create-role']);
        $routes->post('roles', 'Backend\RolesController::create', ['as' => 'admin.roles.create', 'filter' => 'permission:create-role']);
        $routes->get('roles/edit/(:num)', 'Backend\RolesController::edit/$1', ['as' => 'admin.roles.edit', 'filter' => 'permission:edit-role']);
        $routes->post('roles/update/(:num)', 'Backend\RolesController::update/$1', ['as' => 'admin.roles.update', 'filter' => 'permission:edit-role']);

    });
});