<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

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

        $res = $this->get('/admin/site-subscriptions?expiry=expiring_7d&merchant_id=2&plan_id=3');
        $res->assertOk();

        $res->assertSee('创建续费订单（当前集合）');
        // 去重降噪：到期集合视图已有专用 CTA，此处不应再出现“续费下单（先选订阅）”工具入口
        $res->assertDontSee('续费下单（先选订阅）');

        // CTA 应带上当前筛选 merchant_id/plan_id，并且 back 回到当前列表（selfWithoutBack）
        $res->assertSee('href="/admin/platform-orders/create?order_type=renewal&require_subscription=1&merchant_id=2&plan_id=3&back=%2Fadmin%2Fsite-subscriptions%3Fexpiry%3Dexpiring_7d%26merchant_id%3D2%26plan_id%3D3"', false);

        // 链接不应出现 &amp;（只检查该 CTA 的 href 文本中不含 &amp;）
        $html = (string) $res->getContent();
        preg_match('/href="([^"]+)"[^>]*>创建续费订单（当前集合）<\/a>/', $html, $m);
        $ctaHref = (string) ($m[1] ?? '');
        $this->assertNotSame('', $ctaHref);
        $this->assertStringNotContainsString('&amp;', $ctaHref);
    }

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

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

        $res->assertDontSee('创建续费订单（当前集合）');
    }
}
