<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
namespace app\dice\controller\game;

use app\dice\helper\AdminScopeHelper;
use app\dice\logic\game\DiceGameLogic;
use app\dice\validate\game\DiceGameValidate;
use plugin\saiadmin\basic\BaseController;
use plugin\saiadmin\service\Permission;
use support\Request;
use support\Response;

/**
 * 游戏管理控制器
 */
class DiceGameController extends BaseController
{
    public function __construct()
    {
        $this->logic = new DiceGameLogic();
        $this->validate = new DiceGameValidate();
        parent::__construct();
    }

    #[Permission('游戏管理列表', 'dice:game:index:index')]
    public function index(Request $request): Response
    {
        $where = $request->more([
            ['provider_code', ''],
            ['game_code', ''],
            ['game_type', ''],
            ['status', ''],
        ]);
        $query = $this->logic->search($where);
        AdminScopeHelper::applyConfigScope($query, $this->adminInfo ?? null, $request->input('dept_id'));
        $data = $this->logic->getList($query);
        return $this->success($data);
    }

    #[Permission('游戏管理读取', 'dice:game:index:read')]
    public function read(Request $request): Response
    {
        $id = $request->input('id', '');
        $model = $this->logic->read($id);
        if (!$model) {
            return $this->fail('not found');
        }
        $data = is_array($model) ? $model : $model->toArray();
        return $this->success($data);
    }

    #[Permission('游戏管理添加', 'dice:game:index:save')]
    public function save(Request $request): Response
    {
        $data = $request->post();
        $this->validate('save', $data);
        AdminScopeHelper::prepareConfigSaveData($data, $this->adminInfo ?? null, $request->input('dept_id'), $data);
        $result = $this->logic->add($data);
        if (!$result) {
            return $this->fail('add failed');
        }
        return $this->success('add success');
    }

    #[Permission('游戏管理修改', 'dice:game:index:update')]
    public function update(Request $request): Response
    {
        $data = $request->post();
        if ($data === [] || $data === null) {
            $data = $request->all();
        }
        $this->validate('update', $data);
        $requestDeptId = AdminScopeHelper::pickRequestDeptId($request->input('dept_id'), is_array($data) ? $data : []);
        $model = $this->logic->read($data['id'] ?? 0);
        if ($model) {
            $recordDeptId = is_array($model) ? ($model['dept_id'] ?? null) : ($model->dept_id ?? null);
            if (! AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $recordDeptId, $requestDeptId)) {
                return $this->fail('no permission to update this record');
            }
        }
        $result = $this->logic->edit($data['id'], $data, $this->adminInfo ?? null, $requestDeptId);
        if (!$result) {
            return $this->fail('update failed');
        }
        return $this->success('update success');
    }

    #[Permission('游戏管理删除', 'dice:game:index:destroy')]
    public function destroy(Request $request): Response
    {
        $ids = $request->post('ids', '');
        if ($ids === '' || $ids === null) {
            return $this->fail('please select data to delete');
        }
        $result = $this->logic->destroy($ids);
        if (!$result) {
            return $this->fail('delete failed');
        }
        return $this->success('delete success');
    }
}
