<?php

namespace Tests\Feature;

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

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

        $res = $this->get('/admin/platform-orders?lead_id=12&merchant_id=2&plan_id=3&site_subscription_id=4&back=%2Fadmin%2Fplans&syncable_only=1&page=8');
        $res->assertOk();

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

        $matched = preg_match('/<a[^>]+href="([^"]+)"[^>]*>\s*查看已付无回执（该线索）\s*<\/a>/u', $html, $m);
        $this->assertSame(1, $matched, '未找到线索治理入口：查看已付无回执（该线索）');

        $url = $m[1] ?? '';
        $parts = parse_url($url);
        parse_str($parts['query'] ?? '', $q);

        $this->assertSame('12', (string) ($q['lead_id'] ?? ''));
        $this->assertSame('2', (string) ($q['merchant_id'] ?? ''));
        $this->assertSame('3', (string) ($q['plan_id'] ?? ''));
        $this->assertSame('4', (string) ($q['site_subscription_id'] ?? ''));
        $this->assertSame('/admin/plans', (string) ($q['back'] ?? ''));

        $this->assertSame('paid', (string) ($q['payment_status'] ?? ''));
        $this->assertSame('none', (string) ($q['receipt_status'] ?? ''));

        $this->assertArrayNotHasKey('syncable_only', $q);
        $this->assertArrayNotHasKey('page', $q);
    }
}
