<?php

namespace Tests\Feature;

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

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

        $plan = Plan::query()->create([
            'code' => 'status_toggle_test',
            'name' => '状态切换测试',
            'billing_cycle' => 'monthly',
            'price' => 9,
            'list_price' => 9,
            'status' => 'draft',
            'sort' => 10,
            'published_at' => null,
        ]);

        $this->post('/admin/plans/' . $plan->id . '/set-status', [
            'status' => 'active',
        ])->assertRedirect();

        $plan->refresh();
        $this->assertSame('active', $plan->status);
        $this->assertNotNull($plan->published_at);

        $this->post('/admin/plans/' . $plan->id . '/set-status', [
            'status' => 'inactive',
        ])->assertRedirect();

        $plan->refresh();
        $this->assertSame('inactive', $plan->status);
    }

    public function test_guest_cannot_set_plan_status(): void
    {
        $this->seed();

        $plan = Plan::query()->create([
            'code' => 'status_toggle_guest_test',
            'name' => '状态切换测试(guest)',
            'billing_cycle' => 'monthly',
            'price' => 9,
            'list_price' => 9,
            'status' => 'draft',
            'sort' => 10,
        ]);

        $this->post('/admin/plans/' . $plan->id . '/set-status', [
            'status' => 'active',
        ])->assertRedirect('/admin/login');
    }
}
