<?php

namespace Tests\Feature;

use App\Models\Merchant;
use App\Models\SiteSubscription;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

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

        $merchant = Merchant::query()->firstOrFail();

        $sub = SiteSubscription::query()->create([
            'merchant_id' => $merchant->id,
            'status' => 'pending',
            'source' => 'manual',
            'subscription_no' => 'SUB_STATUS_TOGGLE_0001',
            'plan_name' => '测试套餐',
            'billing_cycle' => 'monthly',
            'period_months' => 1,
            'amount' => 10,
            'starts_at' => now()->subDay(),
            'ends_at' => now()->addDays(29),
        ]);

        $this->post('/admin/site-subscriptions/' . $sub->id . '/set-status', [
            'status' => 'activated',
        ])->assertRedirect();

        $sub->refresh();
        $this->assertSame('activated', $sub->status);
    }

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

        $merchant = Merchant::query()->firstOrFail();

        $sub = SiteSubscription::query()->create([
            'merchant_id' => $merchant->id,
            'status' => 'pending',
            'source' => 'manual',
            'subscription_no' => 'SUB_STATUS_TOGGLE_GUEST_0001',
            'plan_name' => '测试套餐',
            'billing_cycle' => 'monthly',
            'period_months' => 1,
            'amount' => 10,
            'starts_at' => now()->subDay(),
            'ends_at' => now()->addDays(29),
        ]);

        $this->post('/admin/site-subscriptions/' . $sub->id . '/set-status', [
            'status' => 'activated',
        ])->assertRedirect('/admin/login');
    }
}
