<?php
declare (strict_types=1);

namespace app\controller;

use app\BaseController;
use app\service\MenuService;
use app\service\PermissionService;

class Menu extends BaseController
{
    /**
     * 获取用户菜单列表
     */
    public function getUserMenus()
    {

        // return json_success([
        //     [
        //         'meta' => [
        //             'title' => 'page.dashboard.title'
        //         ],
        //         'name' => 'Dashboard',
        //         'path' => '/',
        //         'redirect' => '/analytics',
        //         'children' => [
        //             [
        //                 'name' => 'Analytics',
        //                 'path' => '/analytics',
        //                 'component' => '/dashboard/analytics/index',
        //                 'meta' => [
        //                     'affixTab' => true,
        //                     'title' => 'page.dashboard.analytics'
        //                 ]
        //             ]
        //         ]
        //     ]
        // ]);

        $webMenu = [];
        $webMenu[] = [
            "meta" => [
                "icon" => "solar:widget-5-linear",
                "order" => -1,
                "title" => "page.dashboard.title",
            ],
            "name" => "Dashboard",
            "path" => "/dashboard",
            "children" => [
                [
                    "name" => "Analytics",
                    "path" => "/analytics",
                    "component" => "/dashboard/analytics/index.vue",
                    "meta" => [
                        "affixTab" => true,
                        "icon" => "lucide:area-chart",
                        "title" => "dashboard.analytics_title",
                    ],
                ],
                [
                    "name" => "GgrReport",
                    "path" => "/ggr-report",
                    "component" => "/dashboard/ggr_report/index.vue",
                    "meta" => [
                        "icon" => "solar:align-bottom-outline",
                        "title" => "dashboard.ggr_report_title",
                    ],
                ],
            ],
        ];

        // 综合数据
        $webMenu[] = [
            "meta"     => [
                "keepAlive" => true,
                "icon"      => "solar:cloud-check-outline",
                "order"     => 1,
                "title"     => "data_statistics.title",
            ],
            "name"     => "DataStatistics",
            "path"     => "/data-statistics",
            "children" => [
                [
                    "meta"      => [
                        "title"     => "data_statistics.data_agent_title",
                        "icon"      => "solar:server-minimalistic-linear",
                        "keepAlive" => true,
                    ],
                    "name"      => "Agent",
                    "path"      => "/data-statistics/agent",
                    "component" => "/data_statistics/agent/index.vue",
                ],
                [
                    "meta"     => [
                        "title"     => "data_statistics.daily_title",
                        "icon"      => "solar:notebook-linear",
                        "keepAlive" => true,
                    ],
                    "name"     => "Daily",
                    "path"     => "/data-statistics/daily",
                    "children" => [
                        [
                            "meta"      => [
                                "title"     => "data_statistics.agent_title",
                                "keepAlive" => true,
                            ],
                            "name"      => "DailyAgent",
                            "path"      => "/data-statistics/daily/agent",
                            "component" => "/data_statistics/daily/agent/index.vue",
                        ],
                        [
                            "meta"      => [
                                "title"     => "data_statistics.game_title",
                                "keepAlive" => true,
                            ],
                            "name"      => "DailyGame",
                            "path"      => "/data-statistics/daily/game",
                            "component" => "/data_statistics/daily/game/index.vue",
                        ],
                    ],
                ],
                [
                    "meta"     => [
                        "title"     => "data_statistics.history_title",
                        "icon"      => "solar:sort-by-time-outline",
                        "keepAlive" => true,
                    ],
                    "name"     => "History",
                    "path"     => "/data-statistics/history",
                    "children" => [
                        [
                            "meta"      => [
                                "title"     => "data_statistics.history_agent_title",
                                "keepAlive" => true,
                            ],
                            "name"      => "HistoryAgent",
                            "path"      => "/data-statistics/history/agent",
                            "component" => "/data_statistics/history/agent/index.vue",
                        ],
                    ],
                ],
                [
                    "meta"     => [
                        "title"     => "data_statistics.trend_title",
                        "icon"      => "solar:chart-square-outline",
                        "keepAlive" => true,
                    ],
                    "name"     => "Trend",
                    "path"     => "/data-statistics/trend",
                    "children" => [
                        [
                            "meta"      => [
                                "title"     => "data_statistics.trend_win_lose_title",
                                "keepAlive" => true,
                            ],
                            "name"      => "TrendWinLose",
                            "path"      => "/data-statistics/trend/win_lose",
                            "component" => "/data_statistics/trend/win_lose/index.vue",
                        ],
                        [
                            "meta"      => [
                                "title"     => "data_statistics.trend_rewards_title",
                                "keepAlive" => true,
                            ],
                            "name"      => "TrendRewards",
                            "path"      => "/data-statistics/trend/rewards",
                            "component" => "/data_statistics/trend/rewards/index.vue",
                        ],
                        [
                            "meta"      => [
                                "title"     => "data_statistics.trend_agent_title",
                                "keepAlive" => true,
                            ],
                            "name"      => "TrendAgent",
                            "path"      => "/data-statistics/trend/agent",
                            "component" => "/data_statistics/trend/agent/index.vue",
                        ],
                    ],
                ],
                [
                    "meta"     => [
                        "title"     => "data_statistics.rank_title",
                        "icon"      => "solar:ranking-outline",
                        "keepAlive" => true,
                    ],
                    "name"     => "Rank",
                    "path"     => "/data-statistics/rank",
                    "children" => [
                        [
                            "meta"      => [
                                "title"     => "data_statistics.rank_win_title",
                                "keepAlive" => true,
                            ],
                            "name"      => "RankWin",
                            "path"      => "/data-statistics/rank/win",
                            "component" => "/data_statistics/rank/win/index.vue",
                        ],
                        [
                            "meta"      => [
                                "title"     => "data_statistics.rank_lose_title",
                                "keepAlive" => true,
                            ],
                            "name"      => "RankLose",
                            "path"      => "/data-statistics/rank/lose",
                            "component" => "/data_statistics/rank/lose/index.vue",
                        ],
                        [
                            "meta"      => [
                                "title"     => "data_statistics.rank_bet_title",
                                "keepAlive" => true,
                            ],
                            "name"      => "RankBet",
                            "path"      => "/data-statistics/rank/bet_amount",
                            "component" => "/data_statistics/rank/bet/index.vue",
                        ],
                    ],
                ],
            ],
        ];

        // 玩家数据

        $webMenu[] = [
            "meta"     => [
                "keepAlive" => true,
                "icon"      => "solar:user-broken",
                "order"     => 2,
                "title"     => "player_data.title",
            ],
            "name"     => "PlayerData",
            "path"     => "/player-data",
            "children" => [
                [
                    "meta"      => [
                        "title"     => "player_data.player_list",
                        "icon"      => "solar:users-group-rounded-outline",
                        "keepAlive" => true,
                    ],
                    "name"      => "AntDesignPlayerList",
                    "path"      => "/player-data/player-list",
                    "component" => "/player_data/player_list/index.vue",
                ],
                [
                    "meta"      => [
                        "title"     => "player_data.game_records",
                        "icon"      => "solar:gamepad-minimalistic-outline",
                        "keepAlive" => true,
                    ],
                    "name"      => "GameRecordList",
                    "path"      => "/player-data/game-records",
                    "component" => "/player_data/game_records/index.vue",
                ],
                [
                    "meta"      => [
                        "title"     => "player_data.funds_change",
                        "icon"      => "solar:reorder-broken",
                        "keepAlive" => true,
                    ],
                    "name"      => "FundsChangeList",
                    "path"      => "/player-data/funds-change",
                    "component" => "/player_data/funds_change/index.vue",
                ],
                [
                    "meta"      => [
                        "title"     => "player_data.transform_records",
                        "icon"      => "solar:card-recive-line-duotone",
                        "keepAlive" => true,
                    ],
                    "name"      => "TransformRecordsList",
                    "path"      => "/player-data/transform-records",
                    "component" => "/player_data/transform_records/index.vue",
                ],
            ],
        ];

        // 游戏数据
        $webMenu[] = [
            "meta"     => [
                "icon"      => "solar:ghost-smile-outline",
                "keepAlive" => true,
                "order"     => 3,
                "title"     => "game_data.title",
            ],
            "name"     => "gameData",
            "path"     => "/game-data",
            "children" => [
                [
                    "meta"      => [
                        "title"     => "game_data.game_summary_title",
                        "icon"      => "solar:dollar-outline",
                        "keepAlive" => true,
                    ],
                    "name"      => "gameDataGameSummary",
                    "path"      => "/game-data/game-summary",
                    "component" => "/game_data/game_summary/index.vue",
                ],
                [
                    "meta"      => [
                        "title"     => "game_data.bet_level_title",
                        "icon"      => "solar:card-transfer-outline",
                        "keepAlive" => true,
                    ],
                    "name"      => "gameDataBetLevel",
                    "path"      => "/game-data/bet-level",
                    "component" => "/game_data/bet_level/index.vue",
                ],
            ],
        ];

        // 游戏调整
        $webMenu[] = [
            "meta"     => [
                "icon"      => "solar:gamepad-outline",
                "keepAlive" => true,
                "order"     => 4,
                "title"     => "game_control.title",
            ],
            "name"     => "gameControl",
            "path"     => "/game-control",
            "children" => [
                [
                    "meta"      => [
                        "title"     => "game_control.game_config",
                        "icon"      => "solar:gamepad-old-outline",
                        "keepAlive" => true,
                    ],
                    "name"      => "GameControlGameConfig",
                    "path"      => "/game-control/game_config",
                    "component" => "/game_control/game_config/index.vue",
                ],
                [
                    "meta"     => [
                        "title"     => "game_control.play_control_title",
                        "icon"      => "solar:remote-controller-outline",
                        "keepAlive" => true,
                    ],
                    "name"     => "GameControlGamePlayerControl",
                    "path"     => "/game-control/player-control",
                    "children" => [
                        [
                            "meta"      => [
                                "title"     => "game_control.play_control_list_title",
                                "keepAlive" => true,
                            ],
                            "name"      => "GameControlGamePlayerControlList",
                            "path"      => "/game-control/player-control/list",
                            "component" =>
                                "/game_control/player_control/list/index.vue",
                        ],
                        [
                            "meta"      => [
                                "title"     => "game_control.play_control_record_title",
                                "keepAlive" => true,
                            ],
                            "name"      => "GameControlGamePlayerControlRecord",
                            "path"      => "/game-control/player-control/record",
                            "component" =>
                                "/game_control/player_control/record/index.vue",
                        ],
                    ],
                ],
                [
                    "meta"     => [
                        "title"     => "game_control.control_auto_rtp_title",
                        "icon"      => "solar:station-outline",
                        "keepAlive" => true,
                    ],
                    "name"     => "GameControlGameAutoRtpControl",
                    "path"     => "/game-control/auto-rtp",
                    "children" => [
                        [
                            "meta"      => [
                                "title"     => "game_control.control_auto_rtp_config_title",
                                "keepAlive" => true,
                            ],
                            "name"      => "GameControlGameAutoRtpControlConfig",
                            "path"      => "/game-control/auto-rtp/config",
                            "component" => "/game_control/auto_rtp/config/index.vue",
                        ],
                    ],
                ],
                [
                    "meta"     => [
                        "title"     => "feed_user.feed_user_title",
                        "icon"      => "solar:bomb-emoji-broken",
                        "keepAlive" => true,
                    ],
                    "name"     => "FeedUser",
                    "path"     => "/game-control/feed-user",
                    "children" => [
                        [
                            "meta"      => [
                                "title"     => "feed_user.feed_user_global_title",
                                "keepAlive" => true,
                            ],
                            "name"      => "FeedUserGlobal",
                            "path"      => "/game-control/feed-user/global",
                            "component" => "/game_control/feed_user/global/index.vue",
                        ],
                        [
                            "meta"      => [
                                "title"     => "feed_user.feed_user_list_title",
                                "keepAlive" => true,
                            ],
                            "name"      => "FeedUserList",
                            "path"      => "/game-control/feed-user/list",
                            "component" => "/game_control/feed_user/list/index.vue",
                        ],
                        [
                            "meta"      => [
                                "title"     => "feed_user.feed_user_record_title",
                                "keepAlive" => true,
                            ],
                            "name"      => "FeedUserRecord",
                            "path"      => "/game-control/feed-user/record",
                            "component" => "/game_control/feed_user/record/index.vue",
                        ],
                    ],
                ],
            ],
        ];

        // 日志管理
        $webMenu[] = [
            "meta"     => [
                "keepAlive" => true,
                "icon"      => "solar:document-text-outline",
                "order"     => 4,
                "title"     => "log.title",
            ],
            "name"     => "Log",
            "path"     => "/log",
            "children" => [
                [
                    "meta"     => [
                        "title"     => "log.agent_title",
                        "icon"      => "solar:document-add-linear",
                        "keepAlive" => true,
                    ],
                    "name"     => "LogAgent",
                    "path"     => "/log/agent",
                    "children" => [
                        [
                            "meta"      => [
                                "title"     => "log.login_title",
                                "keepAlive" => true,
                            ],
                            "name"      => "LogAgentLogin",
                            "path"      => "/log/agent/login",
                            "component" => "/log/agent/login/index.vue",
                        ],
                        [
                            "meta"      => [
                                "title"     => "log.operation_title",
                                "keepAlive" => true,
                            ],
                            "name"      => "LogOperation",
                            "path"      => "/log/agent/operation",
                            "component" => "/log/agent/operation/index.vue",
                        ],
                    ],
                ],
            ],
        ];

        // 权限管理

        $webMenu[] = [
            "meta"     => [
                "icon"      => "solar:verified-check-outline",
                "keepAlive" => true,
                "order"     => 4,
                "title"     => "user.title",
            ],
            "name"     => "Permission",
            "path"     => "/user",
            "children" => [
                [
                    "meta"      => [
                        "title" => "user.role_list",
                        "icon"  => "solar:shield-user-outline",
                    ],
                    "name"      => "RoleList",
                    "path"      => "/user/role-list",
                    "component" => "/user/role_list/index.vue",
                ],
                [
                    "meta"      => [
                        "title" => "user.user_list",
                        "icon"  => "solar:users-group-rounded-outline",
                    ],
                    "name"      => "UserList",
                    "path"      => "/user/user-list",
                    "component" => "/user/user_list/index.vue",
                ],
            ],
        ];


        try {
            $menus = MenuService::getMenuTree($this->userId);
            $menus = $webMenu;
            return json_success($menus);
        } catch (\Exception $e) {
            return json_error('获取菜单失败：' . $e->getMessage());
        }
    }

    /**
     * 获取所有权限配置（用于角色权限分配）
     */
    public function getAllPermissions()
    {
        try {
            $permissions = PermissionService::getAllPermissions();
            return json_success($permissions);
        } catch (\Exception $e) {
            return json_error([], '获取权限配置失败：' . $e->getMessage());
        }
    }
}