<?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 AdminSiteSubscriptionIndexExpiring7dShouldRenderMerchantTop10ListTest 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_expiring_7d_view_should_render_merchant_top10_table(): void
    {
        $this->loginAsPlatformAdmin();

        $m1 = Merchant::query()->firstOrFail();
        $m2 = Merchant::query()->create([
            'name' => '到期治理站点B',
            'slug' => 'expiry-merchant-b',
            'status' => 'active',
        ]);

        $plan = Plan::query()->create([
            'code' => 'sub_expiry_merchant_top10_plan',
            'name' => '到期治理 Top10 测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 1,
            'list_price' => 1,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        // m1: 2 条 7 天内到期
        SiteSubscription::query()->create([
            'merchant_id' => $m1->id,
            'plan_id' => $plan->id,
            'status' => 'activated',
            'source' => 'manual',
            'subscription_no' => 'SUB_EXP_TOP10_M1_0001',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'amount' => 1,
            'starts_at' => now()->subDays(10),
            'ends_at' => now()->addDays(3),
            'activated_at' => now()->subDays(10),
        ]);
        SiteSubscription::query()->create([
            'merchant_id' => $m1->id,
            'plan_id' => $plan->id,
            'status' => 'activated',
            'source' => 'manual',
            'subscription_no' => 'SUB_EXP_TOP10_M1_0002',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'amount' => 1,
            'starts_at' => now()->subDays(10),
            'ends_at' => now()->addDays(6),
            'activated_at' => now()->subDays(10),
        ]);

        // m2: 1 条 7 天内到期
        SiteSubscription::query()->create([
            'merchant_id' => $m2->id,
            'plan_id' => $plan->id,
            'status' => 'activated',
            'source' => 'manual',
            'subscription_no' => 'SUB_EXP_TOP10_M2_0001',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'amount' => 1,
            'starts_at' => now()->subDays(10),
            'ends_at' => now()->addDays(2),
            'activated_at' => now()->subDays(10),
        ]);

        // 一条已过期（不应进入 expiring_7d）
        SiteSubscription::query()->create([
            'merchant_id' => $m2->id,
            'plan_id' => $plan->id,
            'status' => 'activated',
            'source' => 'manual',
            'subscription_no' => 'SUB_EXP_TOP10_EXPIRED_0001',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'amount' => 1,
            'starts_at' => now()->subDays(40),
            'ends_at' => now()->subDays(1),
            'activated_at' => now()->subDays(40),
        ]);

        $res = $this->get('/admin/site-subscriptions?expiry=expiring_7d');
        $res->assertOk();

        $res->assertSee('站点维度提醒清单', false);
        $res->assertSee('data-role="expiring-7d-merchant-top10"', false);

        // m1 的到期订阅数应该显示 2
        $res->assertSee((string) $m1->name, false);
        $res->assertSee('>2<', false);

        // m2 的到期订阅数应该显示 1
        $res->assertSee((string) $m2->name, false);
        $res->assertSee('>1<', false);
    }
}
