<?php

namespace Tests\Feature;

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

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

        // 模拟用户从某个“工具型集合”进入（fail_only/bmpa_failed_only 等），
        // 再点击快捷筛选时，不应把这些工具型开关继承过去，避免叠加冲突导致空结果。
        $res = $this->get('/admin/platform-orders?merchant_id=1&bmpa_failed_only=1&fail_only=1');
        $res->assertOk();

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

        // 1) 快捷筛选：同步失败
        $this->assertMatchesRegularExpression('/<a[^>]*href="([^"]+)"[^>]*class="muted"[^>]*>同步失败<\/a>/', $html);
        preg_match('/<a[^>]*href="([^"]+)"[^>]*class="muted"[^>]*>同步失败<\/a>/', $html, $m1);
        $href1 = html_entity_decode($m1[1] ?? '');
        $query1 = parse_url($href1, PHP_URL_QUERY) ?: '';
        parse_str($query1, $q1);

        $this->assertSame('failed', (string) ($q1['sync_status'] ?? ''));
        $this->assertSame('1', (string) ($q1['merchant_id'] ?? ''));
        $this->assertArrayNotHasKey('bmpa_failed_only', $q1);
        $this->assertArrayNotHasKey('bmpa_error_keyword', $q1);
        $this->assertArrayNotHasKey('fail_only', $q1);

        // 2) 快捷筛选：可BMPA处理
        $this->assertMatchesRegularExpression('/<a[^>]*href="([^"]+)"[^>]*class="muted"[^>]*>可BMPA处理<\/a>/', $html);
        preg_match('/<a[^>]*href="([^"]+)"[^>]*class="muted"[^>]*>可BMPA处理<\/a>/', $html, $m2);
        $href2 = html_entity_decode($m2[1] ?? '');
        $query2 = parse_url($href2, PHP_URL_QUERY) ?: '';
        parse_str($query2, $q2);

        $this->assertSame('1', (string) ($q2['bmpa_processable_only'] ?? ''));
        $this->assertSame('1', (string) ($q2['merchant_id'] ?? ''));
        $this->assertArrayNotHasKey('bmpa_failed_only', $q2);
        $this->assertArrayNotHasKey('fail_only', $q2);

        // 3) 快捷筛选：对账不一致
        $this->assertMatchesRegularExpression('/<a[^>]*href="([^"]+)"[^>]*class="muted"[^>]*>对账不一致<\/a>/', $html);
        preg_match('/<a[^>]*href="([^"]+)"[^>]*class="muted"[^>]*>对账不一致<\/a>/', $html, $m3);
        $href3 = html_entity_decode($m3[1] ?? '');
        $query3 = parse_url($href3, PHP_URL_QUERY) ?: '';
        parse_str($query3, $q3);

        $this->assertSame('1', (string) ($q3['reconcile_mismatch'] ?? ''));
        $this->assertSame('1', (string) ($q3['merchant_id'] ?? ''));
        $this->assertArrayNotHasKey('bmpa_failed_only', $q3);
        $this->assertArrayNotHasKey('bmpa_error_keyword', $q3);
        $this->assertArrayNotHasKey('fail_only', $q3);

        // 4) 快捷筛选：退款不一致
        $this->assertMatchesRegularExpression('/<a[^>]*href="([^"]+)"[^>]*class="muted"[^>]*>退款不一致<\/a>/', $html);
        preg_match('/<a[^>]*href="([^"]+)"[^>]*class="muted"[^>]*>退款不一致<\/a>/', $html, $m4);
        $href4 = html_entity_decode($m4[1] ?? '');
        $query4 = parse_url($href4, PHP_URL_QUERY) ?: '';
        parse_str($query4, $q4);

        $this->assertSame('1', (string) ($q4['refund_inconsistent'] ?? ''));
        $this->assertSame('1', (string) ($q4['merchant_id'] ?? ''));
        $this->assertArrayNotHasKey('bmpa_failed_only', $q4);
        $this->assertArrayNotHasKey('bmpa_error_keyword', $q4);
        $this->assertArrayNotHasKey('fail_only', $q4);

        // 5) 快捷筛选：续费缺订阅
        $this->assertMatchesRegularExpression('/<a[^>]*href="([^"]+)"[^>]*class="muted"[^>]*>续费缺订阅<\/a>/', $html);
        preg_match('/<a[^>]*href="([^"]+)"[^>]*class="muted"[^>]*>续费缺订阅<\/a>/', $html, $m5);
        $href5 = html_entity_decode($m5[1] ?? '');
        $query5 = parse_url($href5, PHP_URL_QUERY) ?: '';
        parse_str($query5, $q5);

        $this->assertSame('1', (string) ($q5['renewal_missing_subscription'] ?? ''));
        $this->assertSame('1', (string) ($q5['merchant_id'] ?? ''));
        $this->assertArrayNotHasKey('bmpa_failed_only', $q5);
        $this->assertArrayNotHasKey('bmpa_error_keyword', $q5);
        $this->assertArrayNotHasKey('fail_only', $q5);
    }
}
