<?php

namespace Tests\Feature;

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

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

        $this->get('/admin/site-subscriptions')
            ->assertOk()
            ->assertSee('订阅管理')
            ->assertSee('筛选条件')
            ->assertSee('订阅列表')
            ->assertSee('到期状态');
    }

    public function test_guest_cannot_open_site_subscriptions_page(): void
    {
        $this->get('/admin/site-subscriptions')
            ->assertRedirect('/admin/login');
    }

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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'sub_test_monthly',
            'name' => '订阅测试套餐（月付）',
            'billing_cycle' => 'monthly',
            'price' => 88,
            'list_price' => 88,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        SiteSubscription::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'status' => 'activated',
            'source' => 'manual',
            'subscription_no' => 'SUB202603100101',
            'plan_name' => '订阅测试套餐（月付）',
            'billing_cycle' => 'monthly',
            'period_months' => 1,
            'amount' => 88,
            'starts_at' => now()->subDays(2),
            'ends_at' => now()->addDays(28),
            'activated_at' => now()->subDays(2),
        ]);

        SiteSubscription::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'status' => 'pending',
            'source' => 'manual',
            'subscription_no' => 'SUB202603100102',
            'plan_name' => '订阅测试套餐（月付）',
            'billing_cycle' => 'monthly',
            'period_months' => 1,
            'amount' => 88,
            'starts_at' => now(),
            'ends_at' => now()->addMonth(),
        ]);

        $this->get('/admin/site-subscriptions?status=activated&keyword=SUB202603100101')
            ->assertOk()
            ->assertSee('SUB202603100101')
            ->assertDontSee('SUB202603100102');

        // 到期筛选：构造“已过期”和“7天内到期”的订阅
        SiteSubscription::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'status' => 'activated',
            'source' => 'manual',
            'subscription_no' => 'SUB202603100103',
            'plan_name' => '订阅测试套餐（月付）',
            'billing_cycle' => 'monthly',
            'period_months' => 1,
            'amount' => 88,
            'starts_at' => now()->subDays(40),
            'ends_at' => now()->subDays(1),
            'activated_at' => now()->subDays(40),
        ]);

        SiteSubscription::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'status' => 'activated',
            'source' => 'manual',
            'subscription_no' => 'SUB202603100104',
            'plan_name' => '订阅测试套餐（月付）',
            'billing_cycle' => 'monthly',
            'period_months' => 1,
            'amount' => 88,
            'starts_at' => now()->subDays(25),
            'ends_at' => now()->addDays(3),
            'activated_at' => now()->subDays(25),
        ]);

        $this->get('/admin/site-subscriptions?expiry=expired')
            ->assertOk()
            ->assertSeeInOrder(['SUB202603100103', '已过期'])
            ->assertDontSee('SUB202603100104');

        $this->get('/admin/site-subscriptions?expiry=expiring_7d')
            ->assertOk()
            ->assertSeeInOrder(['SUB202603100104', '7天内到期'])
            ->assertDontSee('SUB202603100103');

        // 新增筛选：按站点/套餐
        $this->get('/admin/site-subscriptions?merchant_id=' . $merchant->id)
            ->assertOk()
            ->assertSee('订阅列表');

        $this->get('/admin/site-subscriptions?plan_id=' . $plan->id)
            ->assertOk()
            ->assertSee('订阅列表');
    }
}
