<?php

namespace Tests\Feature;

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

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

        // 默认列表（未进入失败集合）
        $res = $this->get('/admin/platform-orders');
        $res->assertOk();

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

        $this->assertStringContainsString('清除同步失败标记（当前筛选范围）', $html);
        $this->assertStringContainsString('data-role="clear-sync-errors-blocked-hint"', $html);
        $this->assertStringContainsString('同步失败', $html);

        $this->assertStringContainsString('清除批量标记支付失败标记（当前筛选范围）', $html);
        $this->assertStringContainsString('data-role="clear-bmpa-errors-blocked-hint"', $html);
        $this->assertStringContainsString('BMPA失败', $html);

        // 按钮 disabled（宽松断言）
        $this->assertTrue(str_contains($html, 'type="submit" disabled') || str_contains($html, 'disabled="disabled"'));
    }

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

        $res = $this->get('/admin/platform-orders?sync_status=failed&syncable_only=1');
        $res->assertOk();

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

        $this->assertStringContainsString('清除同步失败标记（当前筛选范围）', $html);
        $this->assertStringContainsString('data-role="clear-sync-errors-blocked-hint"', $html);
        $this->assertStringContainsString('只看可同步', $html);

        // 仍应禁用
        $this->assertTrue(str_contains($html, 'type="submit" disabled') || str_contains($html, 'disabled="disabled"'));
    }

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

        $res = $this->get('/admin/platform-orders?sync_status=failed&synced_only=1');
        $res->assertOk();

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

        $this->assertStringContainsString('清除同步失败标记（当前筛选范围）', $html);
        $this->assertStringContainsString('data-role="clear-sync-errors-blocked-hint"', $html);
        $this->assertStringContainsString('只看已同步', $html);

        $this->assertTrue(str_contains($html, 'type="submit" disabled') || str_contains($html, 'disabled="disabled"'));
    }

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

        $res = $this->get('/admin/platform-orders?bmpa_failed_only=1&syncable_only=1');
        $res->assertOk();

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

        $this->assertStringContainsString('清除批量标记支付失败标记（当前筛选范围）', $html);
        $this->assertStringContainsString('data-role="clear-bmpa-errors-blocked-hint"', $html);
        $this->assertStringContainsString('只看可同步', $html);

        $this->assertTrue(str_contains($html, 'type="submit" disabled') || str_contains($html, 'disabled="disabled"'));
    }

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

        // 即使 bmpa_failed_only=1，但叠加了 sync_error_keyword/fail_only，也应禁用（避免误清理）。
        $res = $this->get('/admin/platform-orders?bmpa_failed_only=1&fail_only=1&sync_error_keyword=timeout');
        $res->assertOk();

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

        $this->assertStringContainsString('清除批量标记支付失败标记（当前筛选范围）', $html);
        $this->assertStringContainsString('data-role="clear-bmpa-errors-blocked-hint"', $html);
        $this->assertStringContainsString('同步失败', $html);

        $this->assertTrue(str_contains($html, 'type="submit" disabled') || str_contains($html, 'disabled="disabled"'));
    }

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

        $res = $this->get('/admin/platform-orders?bmpa_failed_only=1&sync_status=failed');
        $res->assertOk();

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

        $this->assertStringContainsString('清除批量标记支付失败标记（当前筛选范围）', $html);
        $this->assertStringContainsString('data-role="clear-bmpa-errors-blocked-hint"', $html);
        $this->assertStringContainsString('同步状态', $html);

        $this->assertTrue(str_contains($html, 'type="submit" disabled') || str_contains($html, 'disabled="disabled"'));
    }

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

        $res = $this->get('/admin/platform-orders?bmpa_failed_only=1&synced_only=1');
        $res->assertOk();

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

        $this->assertStringContainsString('清除批量标记支付失败标记（当前筛选范围）', $html);
        $this->assertStringContainsString('data-role="clear-bmpa-errors-blocked-hint"', $html);
        $this->assertStringContainsString('已同步', $html);

        $this->assertTrue(str_contains($html, 'type="submit" disabled') || str_contains($html, 'disabled="disabled"'));
    }
}
