<?php

namespace Tests\Feature;

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

class AdminPlatformOrderIndexClearErrorButtonsShouldEnableWhenInFailedScopeTest extends TestCase
{
    use RefreshDatabase;

    protected function loginAsPlatformAdmin(): void
    {
        $this->seed();

        $this->post('/admin/login', [
            'email' => 'platform.admin@demo.local',
            'password' => 'Platform@123456',
        ])->assertRedirect('/admin');
    }

    protected function assertButtonEnabled(string $html, string $label): void
    {
        $this->assertStringContainsString($label, $html);

        $pattern = '/' . preg_quote('<button', '/') . '[^>]*>' . preg_quote($label, '/') . preg_quote('</button>', '/') . '/u';
        $this->assertTrue((bool) preg_match($pattern, $html, $m), '未找到按钮：' . $label);

        $btnHtml = (string) ($m[0] ?? '');
        $this->assertFalse(str_contains($btnHtml, 'disabled'), '按钮不应被禁用：' . $label);
    }

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

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

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

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

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

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

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

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