<?php

/**
 * 文件功能：后台签到奖励规则管理测试
 *
 * 覆盖后台连续签到奖励档位的创建、更新、启停与校验。
 */

namespace Tests\Feature\Feature;

use App\Models\SignInRewardRule;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

/**
 * 类功能：验证后台签到奖励规则管理页行为。
 */
class AdminSignInRewardRuleControllerTest extends TestCase
{
    use RefreshDatabase;

    /**
     * 测试管理员可以创建签到奖励规则。
     */
    public function test_admin_can_create_sign_in_reward_rule(): void
    {
        $admin = $this->createSuperAdmin();

        $response = $this->actingAs($admin)->post(route('admin.sign-in-rules.store'), [
            'streak_days' => 11,
            'gold_reward' => 700,
            'exp_reward' => 70,
            'charm_reward' => 7,
            'identity_badge_code' => 'sign_11',
            'identity_badge_name' => '十一日星辉',
            'identity_badge_icon' => '🔥',
            'identity_badge_color' => '#0f766e',
            'identity_duration_days' => 30,
            'sort_order' => 11,
            'is_enabled' => '1',
        ]);

        $response->assertRedirect(route('admin.sign-in-rules.index'));
        $this->assertDatabaseHas('sign_in_reward_rules', [
            'streak_days' => 11,
            'gold_reward' => 700,
            'identity_badge_code' => 'sign_11',
            'identity_duration_days' => 30,
            'is_enabled' => true,
        ]);
    }

    /**
     * 测试管理员可以更新并停用签到奖励规则。
     */
    public function test_admin_can_update_and_toggle_sign_in_reward_rule(): void
    {
        $admin = $this->createSuperAdmin();
        $rule = SignInRewardRule::query()->where('streak_days', 3)->firstOrFail();
        $rule->update(['gold_reward' => 100, 'is_enabled' => true]);

        $this->actingAs($admin)->put(route('admin.sign-in-rules.update', $rule), [
            'streak_days' => 5,
            'gold_reward' => 500,
            'exp_reward' => 50,
            'charm_reward' => 5,
            'identity_badge_code' => 'sign_5',
            'identity_badge_name' => '五日之星',
            'identity_badge_icon' => '⭐',
            'identity_badge_color' => '#4338ca',
            'identity_duration_days' => 10,
            'sort_order' => 5,
            'is_enabled' => '1',
        ])->assertRedirect(route('admin.sign-in-rules.index'));

        $this->assertDatabaseHas('sign_in_reward_rules', [
            'id' => $rule->id,
            'streak_days' => 5,
            'gold_reward' => 500,
            'identity_badge_name' => '五日之星',
        ]);

        $this->actingAs($admin)
            ->postJson(route('admin.sign-in-rules.toggle', $rule))
            ->assertOk()
            ->assertJsonPath('is_enabled', false);
    }

    /**
     * 测试重复连续天数会被校验拦截。
     */
    public function test_duplicate_streak_days_are_rejected(): void
    {
        $admin = $this->createSuperAdmin();
        $this->actingAs($admin)->post(route('admin.sign-in-rules.store'), [
            'streak_days' => 7,
            'gold_reward' => 100,
            'exp_reward' => 10,
            'charm_reward' => 0,
            'identity_duration_days' => 0,
            'sort_order' => 0,
        ])->assertSessionHasErrors('streak_days');
    }

    /**
     * 创建可访问后台的超级管理员账号。
     */
    private function createSuperAdmin(): User
    {
        return User::factory()->create([
            'user_level' => 100,
        ]);
    }
}
