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

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

        SiteSubscription::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'status' => 'activated',
            'source' => 'manual',
            'subscription_no' => 'SUB_EXP_MINI_BAR_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),
        ]);

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

        $html = (string) $res->getContent();

        $this->assertStringContainsString('data-role="expiring-7d-merchant-top10"', $html);
        $this->assertStringContainsString('class="adm-mini-bar"', $html);
        $this->assertStringContainsString('class="adm-mini-bar-fill"', $html);

        $css = (string) file_get_contents(public_path('css/admin-components.css'));
        $this->assertStringContainsString('.adm-mini-bar{', $css);
        $this->assertStringContainsString('.adm-mini-bar-fill{', $css);
    }
}
