<?php

namespace Tests\Feature;

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

class AdminPlanTest 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_open_plans_page(): void
    {
        $this->loginAsPlatformAdmin();

        $this->get('/admin/plans')
            ->assertOk()
            ->assertSee('套餐管理')
            ->assertSee('筛选条件')
            ->assertSee('套餐列表');
    }

    public function test_guest_cannot_open_plans_page(): void
    {
        $this->get('/admin/plans')->assertRedirect('/admin/login');
    }

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

        Plan::query()->create([
            'code' => 'basic_monthly',
            'name' => '基础版（月付）',
            'billing_cycle' => 'monthly',
            'price' => 99,
            'list_price' => 99,
            'status' => 'active',
            'sort' => 20,
            'description' => '适合起步商家',
            'published_at' => now(),
        ]);

        Plan::query()->create([
            'code' => 'enterprise_yearly_draft',
            'name' => '企业版（年付草稿）',
            'billing_cycle' => 'yearly',
            'price' => 2999,
            'list_price' => 3999,
            'status' => 'draft',
            'sort' => 30,
            'description' => '仅供内部评审',
            'published_at' => null,
        ]);

        $this->get('/admin/plans?status=active&billing_cycle=monthly')
            ->assertOk()
            ->assertSee('基础版（月付）')
            ->assertDontSee('企业版（年付草稿）')
            ->assertSee('月付');

        // 发布状态筛选（按 published_at）：基础版已发布，企业版未发布
        $this->get('/admin/plans?published=published')
            ->assertOk()
            ->assertSee('基础版（月付）')
            ->assertDontSee('企业版（年付草稿）');

        $this->get('/admin/plans?published=unpublished')
            ->assertOk()
            ->assertSee('企业版（年付草稿）')
            ->assertDontSee('基础版（月付）');
    }

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

        $this->get('/admin/plans/create')
            ->assertOk()
            ->assertSee('新建套餐')
            ->assertSee('套餐名称');

        $back = '/admin/plans?status=active&billing_cycle=monthly';

        $this->post('/admin/plans', [
            'code' => 'pro_monthly_form',
            'name' => '专业版（月付）',
            'billing_cycle' => 'monthly',
            'price' => 199,
            'list_price' => 299,
            'status' => 'active',
            'sort' => 5,
            'description' => '主要面向成长型站点',
            'published_at' => now()->format('Y-m-d H:i:s'),
            'back' => $back,
        ])->assertRedirect($back);

        $this->get('/admin/plans')
            ->assertSee('专业版（月付）')
            ->assertSee('主要面向成长型站点');
    }

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

        $plan = Plan::query()->create([
            'code' => 'update_test',
            'name' => '更新前套餐',
            'billing_cycle' => 'monthly',
            'price' => 50,
            'list_price' => 80,
            'status' => 'draft',
            'sort' => 11,
        ]);

        $this->get('/admin/plans/' . $plan->id . '/edit')
            ->assertOk()
            ->assertSee('编辑套餐');

        $back = '/admin/plans?published=unpublished';

        $this->post('/admin/plans/' . $plan->id, [
            'code' => 'update_test',
            'name' => '更新后套餐',
            'billing_cycle' => 'monthly',
            'price' => 66,
            'list_price' => 88,
            'status' => 'active',
            'sort' => 10,
            'description' => '通过编辑表单更新',
            'back' => $back,
        ])->assertRedirect($back);

        $this->get('/admin/plans')
            ->assertSee('更新后套餐')
            ->assertSee('¥66.00');
    }
}
