<?php
require_once __DIR__ . '/../init.php';

header('Content-Type: application/json');

try {
    $post = $_POST;
    $files = $_FILES;
    $type = $post['p_type'] ?? '';
    $action = $post['p_action'] ?? '';

    if ($type === '' || $action === '') {
        throw new Exception("invalid_request");
    }

    switch (strtolower($type)) {
        case 'user':
            require_once PROJECT_ROOT . '/backend/controllers/UserController.php';
            require_once PROJECT_ROOT . '/backend/controllers/LoginHistoryController.php';

            $ctrl = new UserController($conn);
            $ctrl_loginhistory = new LoginHistoryController($conn);
            break;

        case 'user_group':
            require_once PROJECT_ROOT . '/backend/controllers/UserGroupController.php';
            $ctrl = new UserGroupController($conn);
            break;

        case 'activity':
            require_once PROJECT_ROOT . '/backend/controllers/ActivityController.php';
            $ctrl = new ActivityController($conn);
            break;

        case 'activity_detail':
            require_once PROJECT_ROOT . '/backend/controllers/ActivityDetailController.php';
            $ctrl = new ActivityDetailController($conn);
            break;

        case 'login_session':
            require_once PROJECT_ROOT . '/backend/controllers/UserLoginDevicesController.php';
            $ctrl = new UserLoginDevicesController($conn);
            break;

        case 'launcher':
            require_once PROJECT_ROOT . '/backend/controllers/LauncherController.php';
            $ctrl = new LauncherController($conn);
            break;

        case 'brand':
            require_once PROJECT_ROOT . '/backend/controllers/BrandController.php';
            $ctrl = new BrandController($conn);
            break;

        case 'kategori':
            require_once PROJECT_ROOT . '/backend/controllers/KategoriController.php';
            $ctrl = new KategoriController($conn);
            break;

        case 'lokasi':
            require_once PROJECT_ROOT . '/backend/controllers/LokasiController.php';
            $ctrl = new LokasiController($conn);
            break;


        case 'stage':
            require_once PROJECT_ROOT . '/backend/controllers/StageController.php';
            $ctrl = new StageController($conn);
            break;

        case 'tournament':
            require_once PROJECT_ROOT . '/backend/controllers/TournamentController.php';
            $ctrl = new TournamentController($conn);
            break;

        case 'level':
            require_once PROJECT_ROOT . '/backend/controllers/LevelController.php';
            $ctrl = new LevelController($conn);
            break;

        case 'slides':
            require_once PROJECT_ROOT . '/backend/controllers/SlidesController.php';
            $ctrl = new SlidesController($conn);
            break;

        case 'news':
            require_once PROJECT_ROOT . '/backend/controllers/NewsController.php';
            $ctrl = new NewsController($conn);
            break;

        case 'player':
            require_once PROJECT_ROOT . '/backend/controllers/PlayerController.php';
            $ctrl = new PlayerController($conn);
            break;

        case 'subkategori':
            require_once PROJECT_ROOT . '/backend/controllers/SubKategoriController.php';
            $ctrl = new SubKategoriController($conn);
            break;
        case 'perusahaan':
            require_once PROJECT_ROOT . '/backend/controllers/PerusahaanController.php';
            $ctrl = new PerusahaanController($conn);
            break;

        case 'hero_banner':
            require_once PROJECT_ROOT . '/backend/controllers/HeroBannerController.php';
            $ctrl = new HeroBannerController($conn);
            break;

        case 'barang':
            require_once PROJECT_ROOT . '/backend/controllers/BarangController.php';
            $ctrl = new BarangController($conn);
            break;

        case 'barang_spesifikasi':
            require_once PROJECT_ROOT . '/backend/controllers/BarangSpesifikasiController.php';
            $ctrl = new BarangSpesifikasiController($conn);
            break;

        case 'attachment':
            require_once PROJECT_ROOT . '/backend/controllers/AttachmentController.php';
            $ctrl = new AttachmentController($conn);
            break;

        case 'membership':
            require_once PROJECT_ROOT . '/backend/controllers/MembershipController.php';
            $ctrl = new MembershipController($conn);
            break;

        case 'membership_payment':
            require_once PROJECT_ROOT . '/backend/controllers/MembershipPaymentController.php';
            $ctrl = new MembershipPaymentController($conn);
            break;

        case 'tournament_category_level':
            require_once PROJECT_ROOT . '/backend/controllers/TournamentCategoryLevelController.php';
            $ctrl = new TournamentCategoryLevelController($conn);
            break;

        case 'tournament_registration':
            require_once PROJECT_ROOT . '/backend/controllers/TournamentRegistrationController.php';
            $ctrl = new TournamentRegistrationController($conn);
            break;

        case 'tournament_stage':
            require_once PROJECT_ROOT . '/backend/controllers/TournamentStageController.php';
            $ctrl = new TournamentStageController($conn);
            break;

        case 'tournament_group':
            require_once PROJECT_ROOT . '/backend/controllers/TournamentGroupController.php';
            $ctrl = new TournamentGroupController($conn);
            break;

        case 'tournament_group_team':
            require_once PROJECT_ROOT . '/backend/controllers/TournamentGroupTeamController.php';
            $ctrl = new TournamentGroupTeamController($conn);
            break;

        case 'tournament_match':
            require_once PROJECT_ROOT . '/backend/controllers/TournamentMatchController.php';
            $ctrl = new TournamentMatchController($conn);
            break;

        case 'about':
            require_once PROJECT_ROOT . '/backend/controllers/AboutController.php';
            $ctrl = new AboutController($conn);
            break;

        case 'ranking_category':
            require_once PROJECT_ROOT . '/backend/controllers/RankingCategoryController.php';
            $ctrl = new RankingCategoryController($conn);
            break;

        case 'membership_payment_setting':
            require_once PROJECT_ROOT . '/backend/controllers/MembershipPaymentSettingController.php';
            $ctrl = new MembershipPaymentSettingController($conn);
            break;

        case 'bank_tournament':
            require_once PROJECT_ROOT . '/backend/controllers/BankTournamentController.php';
            $ctrl = new BankTournamentController($conn);
            break;

        default:
            throw new Exception("unknown_type " . $type);
    }

    switch (strtolower($action)) {
        case 'select':
            $result = $ctrl->select($post);
            break;
        case 'select_general':
            $result = $ctrl->select_general($post);
            break;
        case 'login':
            $result = $ctrl->login($post);

            if (!empty($result)) {
                $ctrl_loginhistory->insert($post, $result['Status'], $result['Keterangan']);
            }
            break;
        case 'logout':
            $result = $ctrl->logout($post);
            break;
        case "update":
            $result = $ctrl->update($post, $files);
            break;
        case "delete":
            $result = $ctrl->delete($post);
            break;
        case "reset_password":
            $result = $ctrl->reset_password($post);
            break;
        case "change_password":
            $result = $ctrl->change_password($post);
            break;
        case "update_profile":
            $result = $ctrl->update_profile($post, $files);
            break;
        case "delete_from_id":
            $result = $ctrl->delete_from_id($post);
            break;
        case "check":
            $result = $ctrl->check($post);
            break;
        case "check_session":
            $result = $ctrl->checkSession($post);
            break;
        case "revoke":
            $result = $ctrl->revoke($post);
            break;
        case "reject":
            $result = $ctrl->reject($post, $files);
            break;
        case "approve":
            $result = $ctrl->approve($post, $files);
            break;
        case "UpdateSession":
            $result = $ctrl->UpdateSession($post);
            break;
        case "get_detail_data":
            $result = $ctrl->getDetailData($post);
            break;
        case "update_status":
            $result = $ctrl->updateStatus($post);
            break;
        case "add_participant":
            $result = $ctrl->addParticipant($post);
            break;
        case "deleteParticipant":
            $result = $ctrl->deleteParticipant($post);
            break;
        case "addMatch":
            $result = $ctrl->addMatch($post);
            break;
        case "update_match";
            $result = $ctrl->updateMatch($post);
            break;
        case "update_match_score":
            $result = $ctrl->updateMatchScore($post);
            break;
        case "get_participants":
            $result = $ctrl->getParticipants($post);
            break;
        case "getregistrationdetail":
            $result = $ctrl->getRegistrationDetail($post);
            break;
        case "setregistrationpaymentapproval":
            $result = $ctrl->setRegistrationPaymentApproval($post);
            break;
        case "setregistartiontournamentstatus":
            $result = $ctrl->setRegistartionTournamentStatus($post);
            break;
        case "setregistrationtcl":
            $result = $ctrl->setRegistrationTCL($post);
            break;
        case "get_matches":
            $result = $ctrl->getMatches($post);
            break;
        case "get_summary":
            $result = $ctrl->getSummary($post);
            break;
        case "generate":
            $result = $ctrl->generate($post, $files);
            break;
        case "cancel":
            $result = $ctrl->cancel($post, $files);
            break;
        case "create_group":
            $result = $ctrl->createGroup($post);
            break;
        case "list_groups":
            $result = $ctrl->listGroups($post);
            break;
        case "auto_assign_teams_to_groups":
            $result = $ctrl->autoAssignTeamsToGroups($post);
            break;
        case "auto_generate_matches":
            $result = $ctrl->autoGenerateMatches($post);
            break;
        case "generate_automatic":
            $result = $ctrl->generate_automatic($post);
            break;
        case "generate_auto_and_team":
            $result = $ctrl->generate_auto_and_team($post);
            break;
        case "generate_match":
            $result = $ctrl->generate_match($post);
            break;
        case "save_matches_bulk":
            $result = $ctrl->saveMatchesBulk($post);
            break;
        case "delete_score":
            $result = $ctrl->delete_score($post);
            break;
        case "generate_next_round":
            $result = $ctrl->generateNextRound($post);
            break;
        case "cancel_round":
            $result = $ctrl->cancelRound($post);
            break;
        case "clear_matches":
            $result = $ctrl->clear_matches($post);
            break;
        case "clear_all_scores":
            $result = $ctrl->clear_all_scores($post);
            break;
        case "generate_report":
            $result = $ctrl->generateReport($post);
            break;
        case "set_rank":
            $result = $ctrl->set_rank($post);
            break;
        default:
            throw new Exception("unknown_action " . $action . " for type " . $type);
    }

    echo json_encode($result, JSON_UNESCAPED_UNICODE);

} catch (Exception $e) {
    echo json_encode([
        'Status' => 0,
        'Keterangan' => $e->getMessage()
    ]);
}
