<?php

namespace Tests\Feature;

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

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

        $res = $this->get('/admin/platform-orders?' . Arr::query([
            'merchant_id' => 2,
            'plan_id' => 3,
            'keyword' => 'alpha',
            'back' => '/admin/plans',
        ]));
        $res->assertOk();

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

        preg_match('/data-role="po-receipt-status-broad-none-go-paid-no-receipt"[^>]*href="([^"]+)"/u', $html, $m1);
        preg_match('/data-role="po-quickfilter-paid-no-receipt"[^>]*href="([^"]+)"/u', $html, $m2);

        $this->assertNotEmpty($m1[1] ?? '');
        $this->assertNotEmpty($m2[1] ?? '');

        $hintHref = html_entity_decode($m1[1]);
        $quickHref = html_entity_decode($m2[1]);

        $hintQuery = parse_url($hintHref, PHP_URL_QUERY) ?: '';
        $quickQuery = parse_url($quickHref, PHP_URL_QUERY) ?: '';

        parse_str($hintQuery, $hint);
        parse_str($quickQuery, $quick);

        $this->assertSame($quick, $hint);
    }
}
