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

use app\dice\model\lottery_pool_config\DiceLotteryPoolConfig;
use plugin\saiadmin\basic\BaseController;
use app\dice\logic\lottery_pool_config\DiceLotteryPoolConfigLogic;
use app\dice\validate\lottery_pool_config\DiceLotteryPoolConfigValidate;
use plugin\saiadmin\service\Permission;
use support\Request;
use support\Response;

/**
 * 色子奖池配置控制器
 */
class DiceLotteryPoolConfigController extends BaseController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new DiceLotteryPoolConfigLogic();
        $this->validate = new DiceLotteryPoolConfigValidate;
        parent::__construct();
    }

    /**
     * 获取 DiceLotteryPoolConfig 列表数据，用于 lottery_config_id 下拉（值为 id，显示为 name），并附带 type、T1-T5 档位权重
     * type：0=付费抽奖券，1=免费抽奖券；一键测试权重中付费默认选 type=0，免费默认选 type=1
     * @param Request $request
     * @return Response 返回 [ ['id' => int, 'name' => string, 'type' => int, 't1_weight' => int, ... 't5_weight' => int], ... ]
     */
    #[Permission('色子奖池配置列表', 'dice:lottery_pool_config:index:index')]
    public function getOptions(Request $request): Response
    {
        $list = DiceLotteryPoolConfig::field('id,name,type,t1_weight,t2_weight,t3_weight,t4_weight,t5_weight')
            ->order('id', 'asc')
            ->select();
        $data = $list->map(function ($item) {
            return [
                'id'         => (int) $item['id'],
                'name'       => (string) ($item['name'] ?? ''),
                'type'       => (int) ($item['type'] ?? 0),
                't1_weight'  => (int) ($item['t1_weight'] ?? 0),
                't2_weight'  => (int) ($item['t2_weight'] ?? 0),
                't3_weight'  => (int) ($item['t3_weight'] ?? 0),
                't4_weight'  => (int) ($item['t4_weight'] ?? 0),
                't5_weight'  => (int) ($item['t5_weight'] ?? 0),
            ];
        })->toArray();
        return $this->success($data);
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    #[Permission('色子奖池配置列表', 'dice:lottery_pool_config:index:index')]
    public function index(Request $request): Response
    {
        $where = $request->more([
            ['name', ''],
            ['type', ''],
        ]);
        $query = $this->logic->search($where);
        $data = $this->logic->getList($query);
        return $this->success($data);
    }

    /**
     * 读取数据
     * @param Request $request
     * @return Response
     */
    #[Permission('色子奖池配置读取', 'dice:lottery_pool_config:index:read')]
    public function read(Request $request): Response
    {
        $id = $request->input('id', '');
        $model = $this->logic->read($id);
        if ($model) {
            $data = is_array($model) ? $model : $model->toArray();
            return $this->success($data);
        } else {
            return $this->fail('未查找到信息');
        }
    }

    /**
     * 保存数据
     * @param Request $request
     * @return Response
     */
    #[Permission('色子奖池配置添加', 'dice:lottery_pool_config:index:save')]
    public function save(Request $request): Response
    {
        $data = $request->post();
        $this->validate('save', $data);
        $result = $this->logic->add($data);
        if ($result) {
            return $this->success('添加成功');
        } else {
            return $this->fail('添加失败');
        }
    }

    /**
     * 更新数据
     * @param Request $request
     * @return Response
     */
    #[Permission('色子奖池配置修改', 'dice:lottery_pool_config:index:update')]
    public function update(Request $request): Response
    {
        $data = $request->post();
        $this->validate('update', $data);
        $result = $this->logic->edit($data['id'], $data);
        if ($result) {
            return $this->success('修改成功');
        } else {
            return $this->fail('修改失败');
        }
    }

    /**
     * 删除数据
     * @param Request $request
     * @return Response
     */
    #[Permission('色子奖池配置删除', 'dice:lottery_pool_config:index:destroy')]
    public function destroy(Request $request): Response
    {
        $ids = $request->post('ids', '');
        if (empty($ids)) {
            return $this->fail('请选择要删除的数据');
        }
        $result = $this->logic->destroy($ids);
        if ($result) {
            return $this->success('删除成功');
        } else {
            return $this->fail('删除失败');
        }
    }

    /**
     * 获取当前彩金池（Redis 实例化，无则按 type=0 创建）
     * 返回含 profit_amount 实时值，供前端轮询展示
     */
    #[Permission('色子奖池配置列表', 'dice:lottery_pool_config:index:index')]
    public function getCurrentPool(Request $request): Response
    {
        $data = $this->logic->getCurrentPool();
        return $this->success($data);
    }

    /**
     * 更新当前彩金池：仅可修改 safety_line、t1_weight～t5_weight，不可修改 profit_amount
     */
    #[Permission('色子奖池配置修改', 'dice:lottery_pool_config:index:update')]
    public function updateCurrentPool(Request $request): Response
    {
        $data = $request->post();
        $this->logic->updateCurrentPool($data);
        return $this->success('保存成功');
    }
}
