<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\dice\controller\player;

use app\dice\helper\AdminScopeHelper;
use app\dice\model\lottery_pool_config\DiceLotteryPoolConfig;
use plugin\saiadmin\app\model\system\SystemDept;
use plugin\saiadmin\app\model\system\SystemUser;
use plugin\saiadmin\basic\BaseController;
use app\dice\logic\player\DicePlayerLogic;
use app\dice\validate\player\DicePlayerValidate;
use plugin\saiadmin\service\Permission;
use support\Request;
use support\Response;
use app\api\cache\UserCache;
use Tinywan\Jwt\JwtToken;
use app\dice\model\player\DicePlayer;

/**
 * 玩家控制器
 */
class DicePlayerController extends BaseController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new DicePlayerLogic();
        $this->validate = new DicePlayerValidate;
        parent::__construct();
    }

    /**
     * 获取彩金池配置选项（id、name）
     * @param Request $request
     * @return Response
     */
    #[Permission('玩家列表', 'dice:player:index:index')]
    public function getLotteryConfigOptions(Request $request): Response
    {
        $query = DiceLotteryPoolConfig::field('id,name,remark')->order('id', 'asc');
        $requestDeptId = AdminScopeHelper::pickRequestDeptId(
            $request->input('dept_id'),
            $request->all()
        );
        AdminScopeHelper::applyConfigScope($query, $this->adminInfo ?? null, $requestDeptId);
        $list = $query->select();
        $data = $list->map(function ($item) {
            $row = is_array($item) ? $item : $item->toArray();
            return [
                'id'           => (int) ($row['id'] ?? 0),
                'name'         => (string) ($row['name'] ?? ''),
                'remark'       => (string) ($row['remark'] ?? ''),
                'display_name' => DiceLotteryPoolConfig::displayLabel($row),
            ];
        })->toArray();
        return $this->success($data);
    }

    /**
     * 获取后台管理员选项（id、username、realname）
     * @param Request $request
     * @return Response
     */
    #[Permission('玩家列表', 'dice:player:index:index')]
    public function getSystemUserOptions(Request $request): Response
    {
        $query = SystemUser::field('id,username,realname,dept_id')->where('status', 1)->order('id', 'asc');
        $requestDeptId = AdminScopeHelper::pickRequestDeptId(
            $request->input('dept_id'),
            $request->all()
        );
        $allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
        if ($allowedIds !== null) {
            if ($allowedIds === []) {
                return $this->success([]);
            }
            $query->whereIn('id', $allowedIds);
        }
        $list = $query->select();
        $data = $list->map(function ($item) {
            $label = trim((string) ($item['realname'] ?? '')) ?: (string) ($item['username'] ?? '');
            return [
                'id' => (int) $item['id'],
                'username' => (string) ($item['username'] ?? ''),
                'realname' => (string) ($item['realname'] ?? ''),
                'dept_id' => isset($item['dept_id']) ? (int) $item['dept_id'] : null,
                'label' => $label ?: (string) $item['id'],
            ];
        })->toArray();
        return $this->success($data);
    }

    /**
     * 超管：按渠道树状展示全部管理员；非超管：同 getSystemUserOptions 扁平列表
     */
    #[Permission('玩家列表', 'dice:player:index:index')]
    public function getSystemUserTreeOptions(Request $request): Response
    {
        if (!AdminScopeHelper::isSuperAdmin($this->adminInfo ?? null)) {
            return $this->getSystemUserOptions($request);
        }

        $users = SystemUser::field('id,username,realname,dept_id')
            ->where('status', 1)
            ->order('id', 'asc')
            ->select()
            ->toArray();

        $depts = SystemDept::field('id,name')
            ->where('status', 1)
            ->order('id', 'asc')
            ->select()
            ->toArray();

        $deptNameMap = [];
        foreach ($depts as $dept) {
            $deptNameMap[(int) $dept['id']] = (string) ($dept['name'] ?? $dept['id']);
        }

        $grouped = [];
        $unassigned = [];
        foreach ($users as $user) {
            $item = [
                'id' => (int) $user['id'],
                'username' => (string) ($user['username'] ?? ''),
                'realname' => (string) ($user['realname'] ?? ''),
                'dept_id' => isset($user['dept_id']) ? (int) $user['dept_id'] : null,
            ];
            $label = trim($item['realname']) ?: $item['username'];
            $item['label'] = $label ?: (string) $item['id'];
            $deptId = $item['dept_id'] ?? 0;
            if ($deptId > 0 && isset($deptNameMap[$deptId])) {
                if (!isset($grouped[$deptId])) {
                    $grouped[$deptId] = [];
                }
                $grouped[$deptId][] = $item;
            } else {
                $unassigned[] = $item;
            }
        }

        $tree = [];
        foreach ($depts as $dept) {
            $deptId = (int) $dept['id'];
            $children = $grouped[$deptId] ?? [];
            if ($children === []) {
                continue;
            }
            $tree[] = [
                'id' => 'dept_' . $deptId,
                'label' => (string) ($dept['name'] ?? $deptId),
                'disabled' => true,
                'children' => $children,
            ];
        }

        if ($unassigned !== []) {
            $tree[] = [
                'id' => 'dept_unassigned',
                'label' => '__unassigned__',
                'disabled' => true,
                'children' => $unassigned,
            ];
        }

        return $this->success($tree);
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    #[Permission('玩家列表', 'dice:player:index:index')]
    public function index(Request $request): Response
    {
        $where = $request->more([
            ['username', ''],
            ['name', ''],
            ['phone', ''],
            ['status', ''],
            ['coin', ''],
            ['lottery_config_id', ''],
        ]);
        $query = $this->logic->search($where);
        AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null, $request->input('dept_id'));
        $query->with(['diceLotteryPoolConfig']);
        $data = $this->logic->getList($query);
        return $this->success($data);
    }

    /**
     * 读取数据
     * @param Request $request
     * @return Response
     */
    #[Permission('玩家读取', 'dice:player:index:read')]
    public function read(Request $request): Response
    {
        $id = $request->input('id', '');
        $model = $this->logic->read($id);
        if (!$model) {
            return $this->fail('not found');
        }
        if (!AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $model->dept_id ?? null)) {
            return $this->fail('no permission to view this record');
        }
        $data = is_array($model) ? $model : $model->toArray();
        return $this->success($data);
    }

    /**
     * 保存数据
     * @param Request $request
     * @return Response
     */
    #[Permission('玩家添加', 'dice:player:index:save')]
    public function save(Request $request): Response
    {
        $data = $request->post();
        if (empty($data['admin_id']) && isset($this->adminInfo['id']) && (int) $this->adminInfo['id'] > 0) {
            $data['admin_id'] = (int) $this->adminInfo['id'];
        }
        AdminScopeHelper::prepareBusinessSaveData(
            $data,
            $this->adminInfo ?? null,
            $request->input('dept_id'),
            $data
        );
        $this->validate('save', $data);
        try {
            $result = $this->logic->add($data);
        } catch (\Throwable $e) {
            if (self::isDeptUsernameDuplicateException($e)) {
                return $this->fail('PLAYER_USERNAME_DEPT_UNIQUE');
            }
            throw $e;
        }
        $playerId = is_array($result) ? ($result['id'] ?? null) : $result;
        if ($playerId) {
            UserCache::deleteUser($playerId);
            $player = DicePlayer::find($playerId);
            if ($player && $player->username !== '') {
                UserCache::deletePlayerByUsername($player->username);
            }
            return $this->success('ADD_SUCCESS');
        }
        return $this->fail('add failed');
    }

    /**
     * 更新数据
     * @param Request $request
     * @return Response
     */
    #[Permission('玩家修改', 'dice:player:index:update')]
    public function update(Request $request): Response
    {
        $data = $request->post();
        $model = $this->logic->read($data['id'] ?? 0);
        if ($model) {
            if (!AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $model->dept_id ?? null, $request->input('dept_id'))) {
                return $this->fail('no permission to update this record');
            }
            if (!isset($data['dept_id']) || $data['dept_id'] === '' || $data['dept_id'] === null) {
                $data['dept_id'] = $model->dept_id ?? null;
            }
        }
        $this->validate('update', $data);
        $result = $this->logic->edit($data['id'], $data);
        if ($result) {
            // 出于安全：删除该玩家缓存，后续 API 按需重建
            UserCache::deleteUser($data['id']);
            $player = DicePlayer::find($data['id']);
            if ($player && $player->username !== '') {
                UserCache::deletePlayerByUsername($player->username);
            }
            return $this->success('UPDATE_SUCCESS');
        }
        return $this->fail('update failed');
    }

    /**
     * 更新状态
     * @param Request $request
     * @return Response
     */
    #[Permission('玩家状态修改', 'dice:player:index:update')]
    public function updateStatus(Request $request): Response
    {
        $id = $request->input('id');
        $status = $request->input('status');
        if ($id === null || $id === '') {
            return $this->fail('missing parameter id');
        }
        if ($status === null || $status === '') {
            return $this->fail('missing parameter status');
        }
        $model = $this->logic->read($id);
        if ($model) {
            if (!AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $model->dept_id ?? null, $request->input('dept_id'))) {
                return $this->fail('no permission to update this record');
            }
        }
        $this->logic->edit($id, ['status' => (int) $status]);
        // 出于安全：删除该玩家缓存，后续 API 按需重建
        UserCache::deleteUser($id);
        $player = DicePlayer::find($id);
        if ($player && $player->username !== '') {
            UserCache::deletePlayerByUsername($player->username);
        }
        return $this->success('update success');
    }

    /**
     * 获取游戏链接
     * @param Request $request
     * @return Response
     */
    #[Permission('获取游戏链接', 'dice:player:index:getGameUrl')]
    public function getGameUrl(Request $request): Response
    {
        $id = $request->input('id');
        if ($id === null || $id === '') {
            return $this->fail('missing parameter id');
        }
        $player = $this->logic->read($id);
        if (!$player) {
            return $this->fail('not found');
        }
        if (!AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $player->dept_id ?? null)) {
            return $this->fail('no permission to view this record');
        }
        if ((int) ($player->status ?? 1) === 0) {
            return $this->fail('account is disabled');
        }
        $username = trim((string) ($player->username ?? ''));
        if ($username === '') {
            return $this->fail('username is empty');
        }
        $lang = trim((string) $request->input('lang', 'zh'));
        if (!in_array($lang, ['zh', 'en'], true)) {
            $lang = 'zh';
        }

        $exp = (int) config('api.session_expire', 604800);
        $tokenResult = JwtToken::generateToken([
            'id' => (int) $player->id,
            'username' => $username,
            'plat' => 'api_login',
            'access_exp' => $exp,
        ]);
        $token = (string) ($tokenResult['access_token'] ?? '');
        if ($token === '') {
            return $this->fail('generate token failed');
        }
        UserCache::setSessionByUsername($username, $token);
        UserCache::setCurrentUserToken((int) $player->id, $token);
        $userArr = $player->hidden(['password', 'lottery_config_id', 't1_weight', 't2_weight', 't3_weight', 't4_weight', 't5_weight'])->toArray();
        UserCache::setUser((int) $player->id, $userArr);
        UserCache::setPlayerByUsername($username, $userArr);

        $gameUrlBase = rtrim((string) env('GAME_URL', (string) config('api.game_url', '')), '/');
        if ($gameUrlBase === '') {
            return $this->fail('GAME_URL is not configured');
        }
        if (!str_starts_with($gameUrlBase, 'http://') && !str_starts_with($gameUrlBase, 'https://')) {
            $gameUrlBase = 'https://' . $gameUrlBase;
        }
        $tokenInUrl = str_replace('%3D', '=', urlencode($token));
        $url = $gameUrlBase . '/?token=' . $tokenInUrl . '&lang=' . $lang;
        return $this->success(['url' => $url]);
    }

    /**
     * 删除数据
     * @param Request $request
     * @return Response
     */
    #[Permission('玩家删除', 'dice:player:index:destroy')]
    public function destroy(Request $request): Response
    {
        $ids = $request->post('ids', '');
        if (empty($ids)) {
            return $this->fail('please select data to delete');
        }
        $ids = is_array($ids) ? $ids : explode(',', (string) $ids);
        $deptId = AdminScopeHelper::getDeptId($this->adminInfo ?? null);
        if ($deptId !== null) {
            $models = $this->logic->model->whereIn('id', $ids)->column('dept_id', 'id');
            $validIds = [];
            foreach ($ids as $id) {
                if (AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $models[$id] ?? null)) {
                    $validIds[] = $id;
                }
            }
            $ids = $validIds;
            if (empty($ids)) {
                return $this->fail('no permission to delete selected data');
            }
        }
        $result = $this->logic->destroy($ids);
        if ($result) {
            // 出于安全：删除相关玩家缓存，后续 API 按需重建
            foreach ($ids as $id) {
                UserCache::deleteUser($id);
                $player = DicePlayer::find($id);
                if ($player && $player->username !== '') {
                    UserCache::deletePlayerByUsername($player->username);
                }
            }
            return $this->success('delete success');
        }
        return $this->fail('delete failed');
    }

    /**
     * 判断是否违反 dice_player (dept_id, username) 唯一索引
     */
    private static function isDeptUsernameDuplicateException(\Throwable $e): bool
    {
        $message = $e->getMessage();
        if ($message === '') {
            return false;
        }
        return str_contains($message, 'uk_dice_player_dept_username')
            || (str_contains($message, 'Duplicate entry') && str_contains($message, 'username'));
    }

}
