<?php

namespace Tests\Feature;

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

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

        // 从复杂上下文进入：保留 merchant_id + 时间范围，但不应把工具型开关继承到快捷筛选链接里。
        $res = $this->get('/admin/platform-orders?' . http_build_query([
            'merchant_id' => 1,
            'created_from' => '2026-03-01',
            'created_to' => '2026-03-18',
            'fail_only' => 1,
            'bmpa_failed_only' => 1,
        ]));
        $res->assertOk();

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

        // 定位“已付无回执”快捷筛选链接
        $this->assertMatchesRegularExpression('/<a[^>]*href="([^"]+)"[^>]*class="muted"[^>]*>已付无回执<\/a>/', $html);
        preg_match('/<a[^>]*href="([^"]+)"[^>]*class="muted"[^>]*>已付无回执<\/a>/', $html, $m);
        $href = html_entity_decode($m[1] ?? '');

        $query = parse_url($href, PHP_URL_QUERY) ?: '';
        parse_str($query, $q);

        // 目标筛选：payment_status=paid + receipt_status=none
        $this->assertSame('paid', (string) ($q['payment_status'] ?? ''));
        $this->assertSame('none', (string) ($q['receipt_status'] ?? ''));

        // 保留业务上下文
        $this->assertSame('1', (string) ($q['merchant_id'] ?? ''));
        $this->assertSame('2026-03-01', (string) ($q['created_from'] ?? ''));
        $this->assertSame('2026-03-18', (string) ($q['created_to'] ?? ''));

        // 不继承工具型开关
        $this->assertArrayNotHasKey('fail_only', $q);
        $this->assertArrayNotHasKey('bmpa_failed_only', $q);
    }
}
