<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\logic\admin\SignInRewardLogic;
use App\Models\SignInRewardConfig;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

/**
 * 签到奖励配置控制器
 */
class SignInRewardController extends Controller
{
    /**
     * @var SignInRewardLogic
     */
    protected $logic;

    public function __construct()
    {
        $this->logic = new SignInRewardLogic();
    }

    /**
     * 列表页面
     *
     * @param Request $request
     * @return \Illuminate\View\View
     */
    public function listView(Request $request)
    {
        $list = $this->logic->getList(10);
        return view('admin.sign_in_reward.list', [
            'list' => $list,
        ]);
    }

    /**
     * 更新页面
     *
     * @param int $dayNumber
     * @return \Illuminate\View\View
     */
    public function updateView($dayNumber)
    {
        $config = SignInRewardConfig::findOrFail($dayNumber);
        return view('admin.sign_in_reward.update', [
            'config' => $config,
        ]);
    }

    /**
     * 处理更新请求
     *
     * @param Request $request
     * @param int $dayNumber
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request, $dayNumber)
    {
        $data = $request->only(['RewardScore']);

        // 验证输入
        $validator = Validator::make($data, [
            'RewardScore' => ['required', 'integer', 'min:0'],
        ], [
            'RewardScore.required' => '奖励积分不能为空',
            'RewardScore.integer' => '奖励积分必须是整数',
            'RewardScore.min' => '奖励积分不能为负数',
        ]);

        if ($validator->fails()) {
            return $this->json(400, $validator->errors()->first());
        }

        // 使用 Logic 处理业务逻辑
        if (!$this->logic->update($dayNumber, $data)) {
            return $this->json(400, $this->logic->getError());
        }

        return $this->json(200, '修改成功');
    }
}
