<?php

namespace Tests\Feature;

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

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

        $plan = Plan::query()->create([
            'code' => 'plan_index_rms_link_plan',
            'name' => '套餐页续费缺订阅治理入口测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $res = $this->get('/admin/plans');
        $res->assertOk();

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

        // 操作区应出现“续费缺订阅”入口
        $this->assertStringContainsString('续费缺订阅', $html);

        // 链接应带 plan_id + renewal_missing_subscription=1，并携带 back 回套餐页
        $this->assertStringContainsString('/admin/platform-orders?plan_id=' . $plan->id, $html);
        $this->assertStringContainsString('renewal_missing_subscription=1', $html);
        $this->assertStringContainsString('back=' . urlencode('/admin/plans'), $html);
    }
}
