<?php
require_once __DIR__ . '/../init.php';
require_once PROJECT_ROOT . '/backend/controllers/common_function.php';
require_once PROJECT_ROOT . '/backend/controllers/db_select_helper.php';

// include other controller
require_once PROJECT_ROOT . '/backend/controllers/ActivityController.php';
require_once PROJECT_ROOT . '/backend/controllers/UserController.php';
require_once PROJECT_ROOT . '/backend/controllers/AksesController.php';
require_once PROJECT_ROOT . '/backend/controllers/UserAksesController.php';
require_once PROJECT_ROOT . '/backend/controllers/UserGroupController.php';
require_once PROJECT_ROOT . '/backend/controllers/PerusahaanController.php';
require_once PROJECT_ROOT . '/backend/controllers/BrandController.php';
require_once PROJECT_ROOT . '/backend/controllers/KategoriController.php';
require_once PROJECT_ROOT . '/backend/controllers/SubKategoriController.php';
require_once PROJECT_ROOT . '/backend/controllers/PlayerController.php';
require_once PROJECT_ROOT . '/backend/controllers/MembershipController.php';
require_once PROJECT_ROOT . '/backend/controllers/MembershipPaymentController.php';
require_once PROJECT_ROOT . '/backend/controllers/StageController.php';
require_once PROJECT_ROOT . '/backend/controllers/TournamentController.php';
require_once PROJECT_ROOT . '/backend/controllers/LevelController.php';
require_once PROJECT_ROOT . '/backend/controllers/TournamentCategoryLevelController.php';
require_once PROJECT_ROOT . '/backend/controllers/TournamentMatchController.php';
require_once PROJECT_ROOT . '/backend/controllers/TournamentGroupController.php';
require_once PROJECT_ROOT . '/backend/controllers/TournamentRegistrationController.php';
require_once PROJECT_ROOT . '/backend/controllers/RankingCategoryController.php';

class LauncherController
{
    private $conn;

    public function __construct($conn)
    {
        $this->conn = $conn;
        $this->activityController = new ActivityController($this->conn);
        $this->userController = new UserController($this->conn);
        $this->userAksesController = new UserAksesController($this->conn);
        $this->userGroupController = new UserGroupController($this->conn);
        $this->aksesController = new AksesController($this->conn);
        $this->perusahaanController = new PerusahaanController($this->conn);
        $this->brandController = new BrandController($this->conn);
        $this->kategoriController = new KategoriController($this->conn);
        $this->subKategoriController = new SubKategoriController($this->conn);
        $this->playerController = new PlayerController($this->conn);
        $this->membershipController = new MembershipController($this->conn);
        $this->membershipPaymentController = new MembershipPaymentController($this->conn);
        $this->stageController = new StageController($this->conn);
        $this->tournamentController = new TournamentController($this->conn);
        $this->levelController = new LevelController($this->conn);
        $this->tournamentCategoryLevelController = new TournamentCategoryLevelController($this->conn);
        $this->tournamentMatchController = new TournamentMatchController($this->conn);
        $this->tournamentGroupController = new TournamentGroupController($this->conn);
        $this->tournamentRegistrationController = new TournamentRegistrationController($this->conn);
        $this->rankingCategoryController = new RankingCategoryController($this->conn);
    }

    public function select($post)
    {
        try {
            return runWithTransaction($this->conn, function ($conn) use ($post) {

                $returnData = [
                    "Status" => 1,
                    "Keterangan" => ""
                ];


                // --------------------------------------
                // MULAI USER
                // --------------------------------------
                $listUser = [];
                if (!empty($post['getUser'])) {
                    $filterParam = [];
                    $userRowsPerPage = 0;

                    if (!empty($post['IDUser'])) {
                        $filterParam[] = ["IDUser", $post['IDUser'], 1];
                        $userRowsPerPage = 1;
                    }

                    $selectParam = [
                        "filters" => $filterParam,
                        "rows_per_page" => $userRowsPerPage,
                        "page_number" => 1,
                        "no_count" => true
                    ];

                    // Jalankan select() bawaan controller
                    $result = $this->userController->select($selectParam);

                    if ($result['Status'] == 0 || empty($result['list_data'])) {
                        $msg = "login:user_tidak_ditemukan";
                        throw new Exception($msg);
                    }

                    $listUser = $result['list_data'];
                    $returnData['listUser'] = $listUser ?? [];
                }
                // --------------------------------------
                // END USER
                // --------------------------------------

                // --------------------------------------
                // MULAI AKSES
                // --------------------------------------
                $listAkses = [];
                if (!empty($post['getAksesList'])) {
                    $filterParam = [];
                    $aksesRowsPerPage = 0;

                    if (
                        isset($post['getAkses_IDUser']) && $post['getAkses_IDUser'] > 0
                    ) {
                        $filterParam[] = ["IDUser", $post['getAkses_IDUser'], 1];
                        $aksesRowsPerPage = 1;
                    }

                    $selectParam = [
                        "filters" => $filterParam,
                        "rows_per_page" => $aksesRowsPerPage,
                        "page_number" => 1,
                        "no_count" => true
                    ];

                    // Jalankan select() bawaan controller anggota
                    $result = $this->aksesController->select($selectParam);
                    $listAkses = $result['list_data'];
                    $returnData['listAkses'] = $listAkses ?? [];
                }
                // --------------------------------------
                // END AKSES
                // --------------------------------------

                // --------------------------------------
                // MULAI Check AKSES
                // --------------------------------------
                $listAkses = [];
                if (!empty($post['getAkses'])) {
                    $filterParam = [];
                    $aksesRowsPerPage = 0;

                    if (
                        isset($post['getAkses_IDUser']) && $post['getAkses_IDUser'] > 0
                    ) {
                        $filterParam[] = ["IDUser", $post['getAkses_IDUser'], 1];
                        $aksesRowsPerPage = 1;
                    }

                    $selectParam = [
                        "filters" => $filterParam,
                        "rows_per_page" => $aksesRowsPerPage,
                        "page_number" => 1,
                        "no_count" => true
                    ];

                    // Jalankan select() bawaan controller anggota
                    $result = $this->userAksesController->select($selectParam);
                    $listAkses = $result['list_data'];
                    $returnData['listAkses'] = $listAkses ?? [];
                }
                // --------------------------------------
                // END Check AKSES
                // --------------------------------------

                // --------------------------------------
                // MULAI Group
                // --------------------------------------
                if (!empty($post['getUserGroup'])) {

                    $groupRowsPerPage = 0;

                    // Daftar field yang bisa difilter
                    $filterableFields = [
                        'getGroup_IDUserGroup' => 'IDUserGroup',
                        'getGroup_StatusAktif' => 'StatusAktif',
                        'getGroup_AdminOnly' => 'AdminOnly',
                    ];

                    // Bangun filter otomatis
                    $filterParam = [];
                    foreach ($filterableFields as $postKey => $dbField) {
                        if (!empty($post[$postKey]) && $post[$postKey] > 0) {
                            $filterParam[] = [$dbField, $post[$postKey], 1];
                        }
                    }

                    $selectParam = [
                        "filters" => $filterParam,
                        "rows_per_page" => $groupRowsPerPage,
                        "page_number" => 1,
                        "no_count" => true
                    ];

                    // Jalankan select() bawaan controller anggota
                    $result = $this->userGroupController->select($selectParam);
                    if (isset($result['Status']) && $result['Status'] == 1) {
                        $returnData['listUserGroup'] = $result['list_data'] ?? [];
                    }

                }
                // --------------------------------------
                // END Group
                // --------------------------------------

                // --------------------------------------
                // MULAI Perusahaan
                // --------------------------------------
                if (!empty($post['getPerusahaan'])) {
                    $filterParam = [];

                    $filterParam[] = ["StatusAktif", "1", 1];

                    $selectParam = [
                        "filters" => $filterParam,
                        "rows_per_page" => 1,
                        "page_number" => 1,
                        "no_count" => true
                    ];


                    // Jalankan getJadwal() dari controller ShiftPegawai 
                    $result = $this->perusahaanController->select($selectParam);

                    if (is_array($result)) {
                        if (isset($result['Status']) && $result['Status'] == 1) {
                            $returnData['listPerusahaan'] = $result['list_data'] ?? [];
                        } else {
                            throw new Exception($result['Keterangan'] ?? 'Terjadi kesalahan pada Perusahaan.');
                        }
                    }

                }
                // --------------------------------------
                // END Perusahaan
                // --------------------------------------

                // --------------------------------------
                // MULAI Brand
                // --------------------------------------
                if (!empty($post['getBrand'])) {

                    $brandRowsPerPage = 0;

                    // Daftar field yang bisa difilter
                    $filterableFields = [
                        'getBrand_IDBrand' => 'IDBrand',
                        'getBrand_StatusBrand' => 'StatusBrand',
                    ];

                    // Bangun filter otomatis
                    $filterParam = [];
                    foreach ($filterableFields as $postKey => $dbField) {
                        if (isset($post[$postKey]) && $post[$postKey] !== '') {
                            $filterParam[] = [$dbField, $post[$postKey], 1];
                        }
                    }

                    $selectParam = [
                        "filters" => $filterParam,
                        "rows_per_page" => $brandRowsPerPage,
                        "page_number" => 1,
                        "no_count" => true
                    ];

                    // Jalankan select() bawaan controller anggota
                    $result = $this->brandController->select($selectParam);
                    if (isset($result['Status']) && $result['Status'] == 1) {
                        $returnData['listBrand'] = $result['list_data'] ?? [];
                    }

                }
                // --------------------------------------
                // END Brand
                // --------------------------------------

                // --------------------------------------
                // MULAI Kategori
                // --------------------------------------
                if (!empty($post['getKategori'])) {

                    $kategoriRowsPerPage = 0;
                    $kategoriOrderBy = "IDKategori";
                    $KategoriOrderType = "DESC";

                    // Daftar field yang bisa difilter
                    $filterableFields = [
                        'getKategori_IDKategori' => 'IDKategori',
                        'getKategori_StatusKategori' => 'StatusKategori',
                    ];

                    // Bangun filter otomatis
                    $filterParam = [];
                    foreach ($filterableFields as $postKey => $dbField) {
                        if (isset($post[$postKey]) && $post[$postKey] !== '') {
                            $filterParam[] = [$dbField, $post[$postKey], 1];
                        }
                    }

                    if (!empty($post['getKategori_kategoriOrderBy'])) {
                        $kategoriOrderBy = $post['getKategori_kategoriOrderBy'];
                    }
                    if (!empty($post['getKategori_kategoriOrderType'])) {
                        $KategoriOrderType = $post['getKategori_kategoriOrderType'];
                    }

                    $selectParam = [
                        "filters" => $filterParam,
                        "rows_per_page" => $kategoriRowsPerPage,
                        "page_number" => 1,
                        "order_by" => $kategoriOrderBy,
                        "order_type" => $KategoriOrderType,
                        "no_count" => true
                    ];

                    // Jalankan select() bawaan controller anggota
                    $result = $this->kategoriController->select($selectParam);
                    if (isset($result['Status']) && $result['Status'] == 1) {
                        $returnData['listKategori'] = $result['list_data'] ?? [];
                    }

                }
                // --------------------------------------
                // END Kategori
                // --------------------------------------

                // --------------------------------------
                // MULAI SubKategori
                // --------------------------------------
                if (!empty($post['getSubKategori'])) {

                    $subKategoriRowsPerPage = 0;

                    // Daftar field yang bisa difilter
                    $filterableFields = [
                        'getSubKategori_IDSubKategori' => 'IDSubKategori',
                        'getSubKategori_StatusSubKategori' => 'StatusSubKategori',
                        'getSubKategori_IDKategori' => 'IDKategori',
                    ];

                    // Bangun filter otomatis
                    $filterParam = [];
                    foreach ($filterableFields as $postKey => $dbField) {
                        if (isset($post[$postKey]) && $post[$postKey] !== '') {
                            $filterParam[] = [$dbField, $post[$postKey], 1];
                        }
                    }

                    $selectParam = [
                        "filters" => $filterParam,
                        "rows_per_page" => $subKategoriRowsPerPage,
                        "page_number" => 1,
                        "no_count" => true
                    ];

                    // Jalankan select() bawaan controller anggota
                    $result = $this->subKategoriController->select($selectParam);
                    if (isset($result['Status']) && $result['Status'] == 1) {
                        $returnData['listSubKategori'] = $result['list_data'] ?? [];
                    }

                }
                // --------------------------------------
                // END SubKategori
                // --------------------------------------


                // --------------------------------------
                // MULAI Player - minidahsboard
                // --------------------------------------
                if (!empty($post['getPlayer_minidashboard'])) {

                    // Jalankan select() bawaan controller anggota
                    $result = $this->playerController->select_miniDashboard();
                    if (isset($result['Status']) && $result['Status'] == 1) {
                        $returnData['listPlayer_minidashboard'] = $result['list_data'] ?? [];
                    }

                }
                // --------------------------------------
                // END Player - minidahsboard
                // --------------------------------------



                // --------------------------------------
                // MULAI Membership - minidahsboard
                // --------------------------------------
                if (!empty($post['getMembership_minidashboard'])) {



                    // Jalankan select() bawaan controller anggota
                    $result = $this->membershipController->select_miniDashboard();
                    if (isset($result['Status']) && $result['Status'] == 1) {
                        $returnData['listMembership_minidashboard'] = $result['list_data'] ?? [];
                    }

                }
                // --------------------------------------
                // END Membership - minidahsboard
                // --------------------------------------

                // --------------------------------------
                // MULAI Tournament - minidahsboard
                // --------------------------------------
                if (!empty($post['getTournament_minidashboard'])) {



                    // Jalankan select() bawaan controller anggota
                    $result = $this->tournamentController->select_miniDashboard();
                    if (isset($result['Status']) && $result['Status'] == 1) {
                        $returnData['listTournament_minidashboard'] = $result['list_data'] ?? [];
                    }

                }
                // --------------------------------------
                // END Tournament - minidahsboard
                // --------------------------------------


                // --------------------------------------
                // MULAI Membership
                // --------------------------------------
                if (!empty($post['getMembership'])) {

                    $membershipRowsPerPage = 0;

                    // Daftar field yang bisa difilter
                    $filterableFields = [
                        'getMembership_IDPlayer' => 'IDPlayer',
                    ];

                    // Bangun filter otomatis
                    $filterParam = [];
                    foreach ($filterableFields as $postKey => $dbField) {
                        if (isset($post[$postKey]) && $post[$postKey] !== '') {
                            $filterParam[] = [$dbField, $post[$postKey], 1];
                        }
                    }

                    $selectParam = [
                        "filters" => $filterParam,
                        "rows_per_page" => $membershipRowsPerPage,
                        "page_number" => 1,
                        "no_count" => true
                    ];

                    // Jalankan select() bawaan controller anggota
                    $result = $this->membershipController->select($selectParam);
                    if (isset($result['Status']) && $result['Status'] == 1) {
                        $returnData['listMembership'] = $result['list_data'] ?? [];
                    }
                }
                // --------------------------------------
                // END Membership
                // --------------------------------------

                // --------------------------------------
                // MULAI MembershipPayment
                // --------------------------------------
                if (!empty($post['getMembershipPayment'])) {

                    $membershipPaymentRowsPerPage = 0;

                    // Daftar field yang bisa difilter
                    $filterableFields = [
                        'getMembershipPayment_IDPlayer' => 'IDPlayer',
                    ];

                    // Bangun filter otomatis
                    $filterParam = [];
                    foreach ($filterableFields as $postKey => $dbField) {
                        if (isset($post[$postKey]) && $post[$postKey] !== '') {
                            $filterParam[] = [$dbField, $post[$postKey], 1];
                        }
                    }

                    $selectParam = [
                        "filters" => $filterParam,
                        "rows_per_page" => $membershipPaymentRowsPerPage,
                        "page_number" => 1,
                        "no_count" => true
                    ];

                    // Jalankan select() bawaan controller anggota
                    $result = $this->membershipPaymentController->select($selectParam);
                    if (isset($result['Status']) && $result['Status'] == 1) {
                        $returnData['listMembershipPayment'] = $result['list_data'] ?? [];
                    }
                }
                // --------------------------------------
                // END MembershipPayment
                // --------------------------------------

                // --------------------------------------
                // MULAI Level
                // --------------------------------------
                if (!empty($post['getLevel'])) {

                    $levelRowsPerPage = 0;
                    $levelOrderBy = "IDLevel";
                    $LevelOrderType = "DESC";

                    // Daftar field yang bisa difilter
                    $filterableFields = [
                        'getLevel_IDLevel' => 'IDLevel',
                        'getLevel_StatusLevel' => 'StatusLevel',
                    ];

                    // Bangun filter otomatis
                    $filterParam = [];
                    foreach ($filterableFields as $postKey => $dbField) {
                        if (isset($post[$postKey]) && $post[$postKey] !== '') {
                            $filterParam[] = [$dbField, $post[$postKey], 1];
                        }
                    }

                    if (!empty($post['getLevel_OrderBy'])) {
                        $levelOrderBy = $post['getLevel_OrderBy'];
                    }
                    if (!empty($post['getLevel_OrderType'])) {
                        $LevelOrderType = $post['getLevel_OrderType'];
                    }


                    $selectParam = [
                        "filters" => $filterParam,
                        "rows_per_page" => $levelRowsPerPage,
                        "page_number" => 1,
                        "no_count" => true,
                        "order_by" => $levelOrderBy,
                        "order_type" => $LevelOrderType
                    ];

                    // Jalankan select() bawaan controller anggota
                    $result = $this->levelController->select($selectParam);
                    if (isset($result['Status']) && $result['Status'] == 1) {
                        $returnData['listLevel'] = $result['list_data'] ?? [];
                    }

                }
                // --------------------------------------
                // END Level
                // --------------------------------------



                // --------------------------------------
                // MULAI TournamentCategoryLevel
                // --------------------------------------
                if (!empty($post['getTournamentCategoryLevel'])) {

                    $tournament_category_levelRowsPerPage = 0;
                    $tournament_category_levelOrderBy = "IDTournamentCategoryLevel";
                    $TournamentCategoryLevelOrderType = "DESC";

                    // Daftar field yang bisa difilter
                    $filterableFields = [
                        'getTournamentCategoryLevel_IDTournamentCategoryLevel' => 'IDTournamentCategoryLevel',
                        'getTournamentCategoryLevel_StatusTournamentCategoryLevel' => 'StatusTournamentCategoryLevel',
                        'getTournamentCategoryLevel_IDTournament' => 'IDTournament',
                    ];

                    // Bangun filter otomatis
                    $filterParam = [];
                    foreach ($filterableFields as $postKey => $dbField) {
                        if (isset($post[$postKey]) && $post[$postKey] !== '') {
                            $filterParam[] = [$dbField, $post[$postKey], 1];
                        }
                    }

                    if (!empty($post['getTournamentCategoryLevel_OrderBy'])) {
                        $tournament_category_levelOrderBy = $post['getTournamentCategoryLevel_OrderBy'];
                    }
                    if (!empty($post['getTournamentCategoryLevel_OrderType'])) {
                        $TournamentCategoryLevelOrderType = $post['getTournamentCategoryLevel_OrderType'];
                    }


                    $selectParam = [
                        "filters" => $filterParam,
                        "rows_per_page" => $tournament_category_levelRowsPerPage,
                        "page_number" => 1,
                        "no_count" => true,
                        "order_by" => $tournament_category_levelOrderBy,
                        "order_type" => $TournamentCategoryLevelOrderType
                    ];

                    // Jalankan select() bawaan controller anggota
                    $result = $this->tournamentCategoryLevelController->select($selectParam);
                    if (isset($result['Status']) && $result['Status'] == 1) {
                        $returnData['listTournamentCategoryLevel'] = $result['list_data'] ?? [];
                    }

                }
                // --------------------------------------
                // END TournamentCategoryLevel
                // --------------------------------------

                // --------------------------------------
                // MULAI Stage
                // --------------------------------------
                if (!empty($post['getStage'])) {

                    $stageRowsPerPage = 0;
                    $stageOrderBy = "IDStage";
                    $StageOrderType = "DESC";

                    // Daftar field yang bisa difilter
                    $filterableFields = [
                        'getStage_IDStage' => 'IDStage',
                        'getStage_StatusStage' => 'StatusStage',
                    ];

                    // Bangun filter otomatis
                    $filterParam = [];
                    foreach ($filterableFields as $postKey => $dbField) {
                        if (isset($post[$postKey]) && $post[$postKey] !== '') {
                            $filterParam[] = [$dbField, $post[$postKey], 1];
                        }
                    }

                    if (!empty($post['getStage_OrderBy'])) {
                        $stageOrderBy = $post['getStage_OrderBy'];
                    }
                    if (!empty($post['getStage_OrderType'])) {
                        $StageOrderType = $post['getStage_OrderType'];
                    }


                    $selectParam = [
                        "filters" => $filterParam,
                        "rows_per_page" => $stageRowsPerPage,
                        "page_number" => 1,
                        "no_count" => true,
                        "order_by" => $stageOrderBy,
                        "order_type" => $StageOrderType
                    ];

                    // Jalankan select() bawaan controller anggota
                    $result = $this->stageController->select($selectParam);
                    if (isset($result['Status']) && $result['Status'] == 1) {
                        $returnData['listStage'] = $result['list_data'] ?? [];
                    }

                }
                // --------------------------------------
                // END Stage
                // --------------------------------------

                // --------------------------------------
                // MULAI TournamentMatch - summary
                // --------------------------------------
                if (!empty($post['getTournamentMatchSummary'])) {

                    $TournamentMatchRowsPerPage = 0;
                    $TournamentMatchOrderBy = "MatchNumber";
                    $TournamentMatchOrderType = "ASC";

                    $selectParam = [
                        "rows_per_page" => $TournamentMatchRowsPerPage,
                        "page_number" => 1,
                        "no_count" => true,
                        "order_by" => $TournamentMatchOrderBy,
                        "order_type" => $TournamentMatchOrderType
                    ];

                    $filterableFields = [
                        'getTournamentMatch_IDTournament' => 'IDTournament',
                        'getTournamentMatch_IDTCL' => 'IDTCL',
                    ];

                    foreach ($filterableFields as $postKey => $dbField) {
                        if (isset($post[$postKey]) && $post[$postKey] !== '') {
                            $selectParam[$dbField] = $post[$postKey];
                        }
                    }

                    $result = $this->tournamentMatchController->getSummary($selectParam);

                    if (isset($result['Status']) && $result['Status'] == 1) {
                        $returnData['listTournamentMatch'] = [
                            "TournamentFormat" => $result['TournamentFormat'] ?? '',
                            "TournamentInfo" => $result['TournamentInfo'] ?? [],
                            "listTCL" => $result['listTCL'] ?? [],
                            "listMatches" => $result['listMatches'] ?? [],
                            "listMatchSets" => $result['listMatchSets'] ?? [],
                            "listGroups" => $result['listGroups'] ?? [],
                            "listGroupTeams" => $result['listGroupTeams'] ?? [],
                            "listStanding" => $result['listStanding'] ?? [],
                            "teamMembers" => $result['teamMembers'] ?? [],      // NEW
                            "players" => $result['players'] ?? [],              // NEW
                            "playerRankings" => $result['playerRankings'] ?? [] // NEW
                        ];
                    }
                }
                // --------------------------------------
                // END TournamentMatch
                // --------------------------------------

                // --------------------------------------
                // MULAI TournamentGroup
                // --------------------------------------
                if (!empty($post['getTournamentGroup'])) {

                    $tournament_category_levelRowsPerPage = 0;
                    $tournament_category_levelOrderBy = "IDTournamentGroup";
                    $TournamentGroupOrderType = "DESC";

                    // Daftar field yang bisa difilter
                    $filterableFields = [
                        'getTournamentGroup_IDTournamentGroup' => 'IDTournamentGroup',
                        'getTournamentGroup_IDTournament' => 'IDTournament',
                    ];

                    // Bangun filter otomatis
                    $filterParam = [];
                    foreach ($filterableFields as $postKey => $dbField) {
                        if (isset($post[$postKey]) && $post[$postKey] !== '') {
                            $filterParam[] = [$dbField, $post[$postKey], 1];
                        }
                    }

                    if (!empty($post['getTournamentGroup_OrderBy'])) {
                        $tournament_category_levelOrderBy = $post['getTournamentGroup_OrderBy'];
                    }
                    if (!empty($post['getTournamentGroup_OrderType'])) {
                        $TournamentGroupOrderType = $post['getTournamentGroup_OrderType'];
                    }


                    $selectParam = [
                        "filters" => $filterParam,
                        "rows_per_page" => $tournament_category_levelRowsPerPage,
                        "page_number" => 1,
                        "no_count" => true,
                        "order_by" => $tournament_category_levelOrderBy,
                        "order_type" => $TournamentGroupOrderType
                    ];

                    // Jalankan select() bawaan controller anggota
                    $result = $this->tournamentGroupController->select($selectParam);
                    if (isset($result['Status']) && $result['Status'] == 1) {
                        $returnData['listTournamentGroup'] = $result['list_data'] ?? [];
                    }

                }
                // --------------------------------------
                // END TournamentGroup
                // --------------------------------------

                // --------------------------------------
                // MULAI TournamentRegistration
                // --------------------------------------
                if (!empty($post['getTournamentRegistration'])) {

                    $tournament_category_levelRowsPerPage = 0;
                    $tournament_category_levelOrderBy = "IDTournamentRegistration";
                    $TournamentRegistrationOrderType = "DESC";

                    // Daftar field yang bisa difilter
                    $filterableFields = [
                        'getTournamentRegistration_IDTournamentRegistration' => 'IDTournamentRegistration',
                        'getTournamentRegistration_IDTournament' => 'IDTournament',
                        'getTournamentRegistration_Status' => 'Status',
                    ];

                    // Bangun filter otomatis
                    $filterParam = [];
                    foreach ($filterableFields as $postKey => $dbField) {
                        if (isset($post[$postKey]) && $post[$postKey] !== '') {
                            $filterParam[] = [$dbField, $post[$postKey], 1];
                        }
                    }

                    if (!empty($post['getTournamentRegistration_OrderBy'])) {
                        $tournament_category_levelOrderBy = $post['getTournamentRegistration_OrderBy'];
                    }
                    if (!empty($post['getTournamentRegistration_OrderType'])) {
                        $TournamentRegistrationOrderType = $post['getTournamentRegistration_OrderType'];
                    }


                    $selectParam = [
                        "filters" => $filterParam,
                        "rows_per_page" => $tournament_category_levelRowsPerPage,
                        "page_number" => 1,
                        "no_count" => true,
                        "order_by" => $tournament_category_levelOrderBy,
                        "order_type" => $TournamentRegistrationOrderType
                    ];

                    // Jalankan select() bawaan controller anggota
                    $result = $this->tournamentRegistrationController->select($selectParam);
                    if (isset($result['Status']) && $result['Status'] == 1) {
                        $returnData['listTournamentRegistration'] = $result['list_data'] ?? [];
                    }

                }
                // --------------------------------------
                // END TournamentRegistration
                // --------------------------------------

                // --------------------------------------
                // MULAI Player - Result
                // --------------------------------------
                if (!empty($post['getPlayerResult'])) {

                    $IDPlayer = isset($post['getPlayerResult_IDPlayer'])
                        ? intval($post['getPlayerResult_IDPlayer'])
                        : 0;

                    if ($IDPlayer > 0) {

                        $result = $this->playerController->player_result([
                            "IDPlayer" => $IDPlayer
                        ]);

                        if (!empty($result['Status'])) {
                            $returnData['listPlayer_TournamentHistory'] = $result['list_tournamenthistory'] ?? [];
                            $returnData['listPlayer_TotalPoint'] = $result['list_totalpoint'] ?? [];
                            $returnData['listPlayer_Ranking'] = $result['list_ranking'] ?? [];
                            $returnData['listPlayer_Statistics'] = $result['list_stats'] ?? [];
                            $returnData['listPlayer_RankCategories'] = $result['list_player_rank'] ?? [];
                        }
                    }
                }
                // --------------------------------------
                // END Player - Result
                // --------------------------------------

                // --------------------------------------
                // MULAI Rank Category
                // --------------------------------------
                if (!empty($post['getRankingCategory'])) {

                    $ranking_category_levelRowsPerPage = 0;
                    $ranking_category_levelOrderBy = "CategoryName";
                    $ranking_category_OrderType = "ASC";

                    // Daftar field yang bisa difilter
                    $filterableFields = [
                        'getRankingCategory_IDRankCategory' => 'IDRankCategory',
                        'getRankingCategory_CategoryName' => 'CategoryName',
                        'getRankingCategory_CategoryCode' => 'CategoryCode',
                        'getRankingCategory_IsActive' => 'IsActive'
                    ];

                    // Bangun filter otomatis
                    $filterParam = [];
                    foreach ($filterableFields as $postKey => $dbField) {
                        if (isset($post[$postKey]) && $post[$postKey] !== '') {
                            $filterParam[] = [$dbField, $post[$postKey], 1];
                        }
                    }

                    if (!empty($post['getRankingCategory_OrderBy'])) {
                        $ranking_category_levelOrderBy = $post['getRankingCategory_OrderBy'];
                    }
                    if (!empty($post['getRankingCategory_OrderType'])) {
                        $ranking_category_OrderType = $post['getRankingCategory_OrderType'];
                    }


                    $selectParam = [
                        "filters" => $filterParam,
                        "rows_per_page" => $ranking_category_levelRowsPerPage,
                        "page_number" => 1,
                        "no_count" => true,
                        "order_by" => $ranking_category_levelOrderBy,
                        "order_type" => $ranking_category_OrderType
                    ];

                    // Jalankan select() bawaan controller anggota
                    $result = $this->rankingCategoryController->select($selectParam);
                    if (isset($result['Status']) && $result['Status'] == 1) {
                        $returnData['listRankingCategory'] = $result['list_data'] ?? [];
                    }
                }
                // --------------------------------------
                // END Rank Category
                // --------------------------------------

                return $returnData;

            });
        } catch (Exception $e) {
            return [
                "Status" => 0,
                "Keterangan" => $e->getMessage(),
                "list_data" => []
            ];
        }
    }
}
