<?php


namespace App\Http\Controllers\admin;

use App\Constants\HttpStatus;
use App\Http\Controllers\Controller;
use App\Services\GameplayRuleService;
use Exception;

use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;

class GameplayRule extends Controller
{


    /**
     * @api {get} /admin/role 角色列表
     * @apiGroup 玩法规则管理
     *
     * @apiUse result
     * @apiUse header
     * @apiVersion 1.0.0
     *
     * @apiParam {int} [page=1]
     * @apiParam {int} [limit=10]
     * @apiParam {string} [display_name] 角色名称
     *
     * @apiSuccess (data) {Object} data
     * @apiSuccess (data) {int} data.total 数量
     * @apiSuccess (data) {Object[]} data.data 列表
     * @apiSuccess (data) {int} data.data.id
     * @apiSuccess (data) {string} data.data.display_name 角色名称（显示用）
     * @apiSuccess (data) {string} data.data.description 角色描述
     * @apiSuccess (data) {array} data.data.menus_ids 角色拥有的菜单
     * @apiSuccess (data) {string} data.data.updated_at
     * @apiSuccess (data) {string} data.data.created_at
     */
    public function index()
    {
        try {
            request()->validate([
                'group' => ['nullable', 'string'],
                'keywords' => ['nullable', 'string'],
            ]);
            $search = request()->all();
            $result = GameplayRuleService::paginate($search);
        } catch (ValidationException $e) {
            return $this->error(HttpStatus::VALIDATION_FAILED, '', $e->errors());
        } catch (Exception $e) {
            return $this->error(intval($e->getCode()));
        }
        return $this->success($result);
    }


    /**
     * @api {post} /admin/role/submit 修改角色
     * @apiGroup 玩法规则管理
     *
     * @apiUse result
     * @apiUse header
     * @apiVersion 1.0.0
     *
     * @apiParam {int} id 角色ID
     * @apiParam {string} display_name 角色名称（显示用）
     * @apiParam {string} description 角色描述
     * @apiParam {array} [menus_ids] 角色菜单
     */
    public function store()
    {
        // try {
            $params = request()->all();

            $validator = [
                'keywords' => 'required|string|max:50',
                'groups'    => 'required|string|max:50',
                'maxinum'  => 'required|integer|min:1',
                'mininum'  => 'required|integer|min:0',
                'odds'     => 'required|numeric|min:0',
            ];

            request()->validate($validator);

            $ret = GameplayRuleService::submit($params);
            if ($ret['code'] == GameplayRuleService::NOT) {
                return $this->error($ret['code'], $ret['msg']);
            }
        // } catch (ValidationException $e) {
        //     return $this->error(HttpStatus::VALIDATION_FAILED, '', $e->errors());
        // } catch (Exception $e) {
        //     return $this->error(intval($e->getCode()));
        // }
        return $this->success([], $ret['msg']);

    }

    /**
     * @api {post} /admin/role/delete 删除角色
     * @apiGroup 玩法规则管理
     *
     * @apiUse result
     * @apiUse header
     * @apiVersion 1.0.0
     *
     * @apiParam {int} id 角色ID
     */
    public function destroy()
    {
        $id = request()->post('id');
        // 示例：通过 ID 删除菜单
        $info = GameplayRuleService::findOne(['id' => $id]);
        if (!$info) {
            return $this->error(0, '玩法不存在');
        }
        GameplayRuleService::clearCache($info->keywords);
        $info->delete();

        return $this->success([], '删除成功');
    }

    public function info()
    {
        $keywords = request()->get('keywords', '大');
        $result = GameplayRuleService::getGameplayRules($keywords);
        return $this->success($result);
    }

}