<?php

namespace Tests\Feature;

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

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

        $res = $this->get('/admin/platform-orders?back=%2Fadmin&payment_status=paid');
        $res->assertOk();

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

        $res->assertSee('data-role="platform-orders-created-range-quick-links"', false);

        $res->assertSee('data-role="po-created-range-today"', false);
        $res->assertSee('data-role="po-created-range-7d"', false);
        $res->assertSee('data-role="po-created-range-30d"', false);
        $res->assertSee('data-role="po-created-range-clear"', false);

        // back 必须保留且不应被 escape 成 &amp;back
        $res->assertSee('back=%2Fadmin', false);
        $res->assertDontSee('&amp;back=', false);

        // 应保留原有筛选上下文（例如 payment_status=paid）
        $res->assertSee('payment_status=paid', false);

        $today = now()->format('Y-m-d');
        $from7d = now()->subDays(6)->format('Y-m-d');
        $from30d = now()->subDays(29)->format('Y-m-d');

        // 进一步护栏：断言 4 个快捷链接实际携带的 query 口径正确
        $extractHrefByRole = function (string $role) use ($html): string {
            $m = [];
            preg_match('/data-role="' . preg_quote($role, '/') . '"[^>]*href="([^"]+)"/i', $html, $m);
            return isset($m[1]) ? html_entity_decode((string) $m[1]) : '';
        };

        $todayHref = $extractHrefByRole('po-created-range-today');
        $this->assertNotSame('', $todayHref);
        $this->assertStringContainsString('created_from=' . $today, $todayHref);
        $this->assertStringContainsString('created_to=' . $today, $todayHref);
        $this->assertStringContainsString('payment_status=paid', $todayHref);
        $this->assertStringContainsString('back=%2Fadmin', $todayHref);

        $d7Href = $extractHrefByRole('po-created-range-7d');
        $this->assertNotSame('', $d7Href);
        $this->assertStringContainsString('created_from=' . $from7d, $d7Href);
        $this->assertStringContainsString('created_to=' . $today, $d7Href);
        $this->assertStringContainsString('payment_status=paid', $d7Href);
        $this->assertStringContainsString('back=%2Fadmin', $d7Href);

        $d30Href = $extractHrefByRole('po-created-range-30d');
        $this->assertNotSame('', $d30Href);
        $this->assertStringContainsString('created_from=' . $from30d, $d30Href);
        $this->assertStringContainsString('created_to=' . $today, $d30Href);
        $this->assertStringContainsString('payment_status=paid', $d30Href);
        $this->assertStringContainsString('back=%2Fadmin', $d30Href);

        $clearHref = $extractHrefByRole('po-created-range-clear');
        $this->assertNotSame('', $clearHref);
        $this->assertStringNotContainsString('created_from=', $clearHref);
        $this->assertStringNotContainsString('created_to=', $clearHref);
        $this->assertStringContainsString('payment_status=paid', $clearHref);
        $this->assertStringContainsString('back=%2Fadmin', $clearHref);
    }
}
