<?php

namespace Tests\Feature;

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

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

        $this->get('/admin/platform-orders?reconcile_mismatch=1')
            ->assertOk()
            ->assertSee('建议先完成金额/状态治理（补回执/核对退款/修正状态）', false)
            ->assertSee('对账不一致', false);
    }

    public function test_platform_orders_tools_show_governance_hint_when_refund_inconsistent_filter_present(): void
    {
        $this->loginAsPlatformAdmin();

        $this->get('/admin/platform-orders?refund_inconsistent=1')
            ->assertOk()
            ->assertSee('建议先完成金额/状态治理（补回执/核对退款/修正状态）', false)
            ->assertSee('退款不一致', false);
    }

    public function test_platform_orders_tools_show_strong_hint_when_syncable_only_and_governance_filters_present(): void
    {
        $this->loginAsPlatformAdmin();

        $this->get('/admin/platform-orders?syncable_only=1&reconcile_mismatch=1')
            ->assertOk()
            ->assertSee('注意：当前同时勾选了「只看可同步」', false)
            ->assertSee('先去治理（取消只看可同步）', false)
            ->assertSee('syncable_only=', false);
    }

    public function test_platform_orders_tools_show_hint_when_sync_failed_filters_present(): void
    {
        $this->loginAsPlatformAdmin();

        $this->get('/admin/platform-orders?sync_status=failed')
            ->assertOk()
            ->assertSee('当前筛选包含「同步失败/失败原因」范围', false)
            ->assertSee('进入同步失败集合', false)
            ->assertSee('切到只看可同步（用于批量重试同步）', false)
            ->assertSee('sync_status=failed', false)
            ->assertSee('syncable_only=1', false);
    }

    public function test_tools_go_sync_failed_link_should_clear_bmpa_filters_when_cross_context(): void
    {
        $this->loginAsPlatformAdmin();

        // 模拟用户在 BMPA 失败上下文中又叠加了同步失败筛选；
        // 此时“进入同步失败集合”的链接应显式清理 bmpa_failed_only/bmpa_error_keyword，避免跳转后产生筛选冲突。
        $res = $this->get('/admin/platform-orders?sync_status=failed&bmpa_failed_only=1');
        $res->assertOk();

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

        // 只校验“进入同步失败集合”这一条链接本身的 query（避免页面其它区域也出现类似组合链接导致误报）
        $this->assertMatchesRegularExpression('/href="([^"]+)"[^>]*>进入同步失败集合<\/a>/', $html);

        preg_match('/href="([^"]+)"[^>]*>进入同步失败集合<\/a>/', $html, $m);
        $href = html_entity_decode($m[1] ?? '');

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

        $this->assertSame('failed', (string) ($q['sync_status'] ?? ''));
        $this->assertArrayNotHasKey('bmpa_failed_only', $q);
        $this->assertArrayNotHasKey('bmpa_error_keyword', $q);
    }
}
