<?php

namespace Tests\Feature;

use App\Models\SignInRewardConfig;
use Tests\TestCase;

/**
 * 签到奖励配置控制器功能测试
 *
 * 测试 SignInRewardController 的 HTTP 端点
 */
class SignInRewardControllerTest extends TestCase
{
    /**
     * 设置测试环境
     */
    public function setUp(): void
    {
        parent::setUp();

        // 清理测试数据
        SignInRewardConfig::query()->delete();
    }

    /**
     * 测试获取列表页面
     *
     * @return void
     */
    public function test_list_view_returns_success()
    {
        // 创建测试数据
        SignInRewardConfig::create([
            'DayNumber' => 1,
            'RewardScore' => 100,
        ]);

        $response = $this->get('/admin/sign-in-reward/list');

        $response->assertStatus(200);
        $response->assertViewHas('list');
    }

    /**
     * 测试获取添加页面
     *
     * @return void
     */
    public function test_add_view_returns_success()
    {
        $response = $this->get('/admin/sign-in-reward/add');

        $response->assertStatus(200);
    }

    /**
     * 测试成功添加奖励配置
     *
     * @return void
     */
    public function test_add_valid_config_returns_success()
    {
        $data = [
            'DayNumber' => 1,
            'RewardScore' => 100,
        ];

        $response = $this->post('/admin/sign-in-reward/add', $data);

        $response->assertStatus(200);
        $response->assertJson(['code' => 200, 'msg' => '添加成功']);
        $this->assertDatabaseHas('QPAccountsDB.dbo.SignInRewardConfig', $data);
    }

    /**
     * 测试添加时缺少必要字段
     *
     * @return void
     */
    public function test_add_missing_day_number_returns_error()
    {
        $data = [
            'RewardScore' => 100,
        ];

        $response = $this->post('/admin/sign-in-reward/add', $data);

        $response->assertStatus(200);
        $response->assertJson(['code' => 400]);
        $response->assertJsonFragment(['msg' => '签到天数不能为空']);
    }

    /**
     * 测试添加时缺少奖励积分
     *
     * @return void
     */
    public function test_add_missing_reward_score_returns_error()
    {
        $data = [
            'DayNumber' => 1,
        ];

        $response = $this->post('/admin/sign-in-reward/add', $data);

        $response->assertStatus(200);
        $response->assertJson(['code' => 400]);
        $response->assertJsonFragment(['msg' => '奖励积分不能为空']);
    }

    /**
     * 测试添加时使用无效天数
     *
     * @return void
     */
    public function test_add_invalid_day_number_returns_error()
    {
        $data = [
            'DayNumber' => -1,
            'RewardScore' => 100,
        ];

        $response = $this->post('/admin/sign-in-reward/add', $data);

        $response->assertStatus(200);
        $response->assertJson(['code' => 400]);
    }

    /**
     * 测试添加时使用负数奖励积分
     *
     * @return void
     */
    public function test_add_negative_reward_score_returns_error()
    {
        $data = [
            'DayNumber' => 1,
            'RewardScore' => -100,
        ];

        $response = $this->post('/admin/sign-in-reward/add', $data);

        $response->assertStatus(200);
        $response->assertJson(['code' => 400]);
    }

    /**
     * 测试添加重复的天数配置
     *
     * @return void
     */
    public function test_add_duplicate_day_number_returns_error()
    {
        // 创建第一条记录
        SignInRewardConfig::create([
            'DayNumber' => 1,
            'RewardScore' => 100,
        ]);

        // 尝试添加相同的天数
        $data = [
            'DayNumber' => 1,
            'RewardScore' => 200,
        ];

        $response = $this->post('/admin/sign-in-reward/add', $data);

        $response->assertStatus(200);
        $response->assertJson(['code' => 400]);
        $response->assertJsonFragment(['msg' => '已存在']);
    }

    /**
     * 测试获取更新页面
     *
     * @return void
     */
    public function test_update_view_returns_success()
    {
        // 创建测试数据
        SignInRewardConfig::create([
            'DayNumber' => 1,
            'RewardScore' => 100,
        ]);

        $response = $this->get('/admin/sign-in-reward/update/1');

        $response->assertStatus(200);
        $response->assertViewHas('config');
    }

    /**
     * 测试更新奖励配置
     *
     * @return void
     */
    public function test_update_valid_config_returns_success()
    {
        // 创建初始记录
        SignInRewardConfig::create([
            'DayNumber' => 2,
            'RewardScore' => 200,
        ]);

        $data = [
            'RewardScore' => 300,
        ];

        $response = $this->post('/admin/sign-in-reward/update/2', $data);

        $response->assertStatus(200);
        $response->assertJson(['code' => 200, 'msg' => '修改成功']);
        $this->assertDatabaseHas('QPAccountsDB.dbo.SignInRewardConfig', [
            'DayNumber' => 2,
            'RewardScore' => 300,
        ]);
    }

    /**
     * 测试更新时使用无效奖励积分
     *
     * @return void
     */
    public function test_update_invalid_reward_score_returns_error()
    {
        // 创建初始记录
        SignInRewardConfig::create([
            'DayNumber' => 2,
            'RewardScore' => 200,
        ]);

        $data = [
            'RewardScore' => -100,
        ];

        $response = $this->post('/admin/sign-in-reward/update/2', $data);

        $response->assertStatus(200);
        $response->assertJson(['code' => 400]);
    }

    /**
     * 测试更新不存在的记录
     *
     * @return void
     */
    public function test_update_nonexistent_record_returns_error()
    {
        $data = [
            'RewardScore' => 300,
        ];

        $response = $this->post('/admin/sign-in-reward/update/999', $data);

        $response->assertStatus(404);
    }

    /**
     * 测试删除奖励配置
     *
     * @return void
     */
    public function test_delete_config_returns_success()
    {
        // 创建测试数据
        SignInRewardConfig::create([
            'DayNumber' => 3,
            'RewardScore' => 300,
        ]);

        $response = $this->post('/admin/sign-in-reward/delete/3');

        $response->assertStatus(200);
        $response->assertJson(['code' => 200, 'msg' => '删除成功']);
        $this->assertDatabaseMissing('QPAccountsDB.dbo.SignInRewardConfig', [
            'DayNumber' => 3,
        ]);
    }

    /**
     * 测试删除不存在的记录
     *
     * @return void
     */
    public function test_delete_nonexistent_record_returns_error()
    {
        $response = $this->post('/admin/sign-in-reward/delete/999');

        $response->assertStatus(404);
    }

    /**
     * 清理测试数据
     */
    public function tearDown(): void
    {
        SignInRewardConfig::query()->delete();
        parent::tearDown();
    }
}
