<?php

namespace Tests\Feature;

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

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

        // 构造一个会触发 batch_bmpa 被阻断的复杂筛选；
        // blocked hint 应给出“切到可BMPA处理集合”入口，并清理冲突开关。
        $res = $this->get('/admin/platform-orders?sync_status=failed&syncable_only=1&bmpa_failed_only=1&page=2&back=/admin');
        $res->assertOk();

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

        $this->assertStringContainsString('data-role="batch-bmpa-blocked-hint"', $html);
        $this->assertMatchesRegularExpression('/href="([^"]+)"[^>]*>切到可BMPA处理集合<\/a>/', $html);

        preg_match('/href="([^"]+)"[^>]*>切到可BMPA处理集合<\/a>/', $html, $m);
        $href = html_entity_decode((string) ($m[1] ?? ''));

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

        $this->assertSame('1', (string) ($q['bmpa_processable_only'] ?? ''));
        $this->assertSame('/admin', (string) ($q['back'] ?? ''));
        $this->assertArrayNotHasKey('page', $q);
        $this->assertArrayNotHasKey('bmpa_failed_only', $q);
        $this->assertArrayNotHasKey('sync_status', $q);
        $this->assertArrayNotHasKey('syncable_only', $q);
    }
}
