<?php

namespace App\Http\logic\admin;

use App\Models\SignInRewardConfig;

/**
 * 签到奖励配置业务逻辑类
 *
 * 负责签到奖励配置的增删改查操作
 */
class SignInRewardLogic
{
    /**
     * 错误信息存储
     *
     * @var string
     */
    protected $error = '';

    /**
     * 获取错误信息
     *
     * @return string
     */
    public function getError()
    {
        return $this->error;
    }

    /**
     * 设置错误信息
     *
     * @param string $error
     * @return void
     */
    public function setError($error)
    {
        $this->error = $error;
    }

    /**
     * 获取分页列表
     *
     * @param int $perPage
     * @return \Illuminate\Pagination\Paginator
     */
    public function getList($perPage = 10)
    {
        return SignInRewardConfig::getPaginatedList($perPage);
    }

    /**
     * 获取指定天数的奖励配置
     *
     * @param int $dayNumber
     * @return SignInRewardConfig|null
     */
    public function getByDay($dayNumber)
    {
        return SignInRewardConfig::findByDay($dayNumber);
    }

    /**
     * 创建新的签到奖励配置
     *
     * @param array $data 包含 DayNumber 和 RewardScore
     * @return bool
     */
    public function create($data)
    {
        try {
            // 验证数据
            if (!$this->validateData($data)) {
                return false;
            }

            // 检查是否已存在相同的 DayNumber
            if (SignInRewardConfig::where('DayNumber', $data['DayNumber'])->exists()) {
                $this->error = "第 {$data['DayNumber']} 天的配置已存在";
                return false;
            }

            SignInRewardConfig::create([
                'DayNumber' => (int) $data['DayNumber'],
                'RewardScore' => (int) $data['RewardScore'],
            ]);

            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }
    }

    /**
     * 更新签到奖励配置
     *
     * @param int $dayNumber
     * @param array $data
     * @return bool
     */
    public function update($dayNumber, $data)
    {
        try {
            $config = SignInRewardConfig::findOrFail($dayNumber);

            // 验证数据
            if (!$this->validateData($data, true)) {
                return false;
            }

            $config->update([
                'RewardScore' => (int) $data['RewardScore'],
            ]);

            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }
    }

    /**
     * 删除签到奖励配置
     *
     * @param int $dayNumber
     * @return bool
     */
    public function delete($dayNumber)
    {
        try {
            $config = SignInRewardConfig::findOrFail($dayNumber);
            $config->delete();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }
    }

    /**
     * 验证数据有效性
     *
     * @param array $data
     * @param bool $isUpdate 是否为更新操作（如果为 true，DayNumber 不是必需的）
     * @return bool
     */
    protected function validateData($data, $isUpdate = false)
    {
        if (!$isUpdate) {
            // 创建时，DayNumber 是必需的
            if (!isset($data['DayNumber']) || $data['DayNumber'] === '') {
                $this->error = "签到天数不能为空";
                return false;
            }

            if (!is_numeric($data['DayNumber']) || (int) $data['DayNumber'] <= 0) {
                $this->error = "签到天数必须是正整数";
                return false;
            }
        }

        // RewardScore 必需且必须是非负整数
        if (!isset($data['RewardScore']) || $data['RewardScore'] === '') {
            $this->error = "奖励积分不能为空";
            return false;
        }

        if (!is_numeric($data['RewardScore']) || (int) $data['RewardScore'] < 0) {
            $this->error = "奖励积分必须是非负整数";
            return false;
        }

        return true;
    }
}
