<?php

namespace Tests\Feature;

use App\Models\Plan;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AdminPlanSeedDefaultsTest extends TestCase
{
    use RefreshDatabase;

    protected function loginAsPlatformAdmin(): void
    {
        $this->seed();

        $this->post('/admin/login', [
            'email' => 'platform.admin@demo.local',
            'password' => 'Platform@123456',
        ])->assertRedirect('/admin');
    }

    public function test_platform_admin_can_seed_default_plans_when_empty(): void
    {
        $this->loginAsPlatformAdmin();

        // 清空 seed 带来的默认套餐（测试口径：模拟空库）
        Plan::query()->delete();
        $this->assertSame(0, Plan::query()->count());

        $this->post('/admin/plans/seed-defaults')
            ->assertRedirect('/admin/plans');

        $this->assertSame(2, Plan::query()->count());
        $this->assertNotNull(Plan::query()->where('code', 'starter_monthly')->first());
        $this->assertNotNull(Plan::query()->where('code', 'pro_yearly')->first());

        $this->get('/admin/plans')
            ->assertOk()
            ->assertSee('基础版（月付）')
            ->assertSee('专业版（年付）');
    }

    public function test_seed_default_plans_is_blocked_when_plans_exist(): void
    {
        $this->loginAsPlatformAdmin();

        $this->assertTrue(Plan::query()->count() > 0);

        $this->post('/admin/plans/seed-defaults')
            ->assertRedirect();

        // 不应新增到 2 条以上；且仍能打开列表
        $this->get('/admin/plans')
            ->assertOk()
            ->assertSee('套餐管理');
    }
}
