<?php

namespace Tests\Feature;

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

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

        $res = $this->get('/admin');
        $res->assertOk();

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

        // 行可点击（漏斗 + 治理 + 订阅到期）
        $this->assertStringContainsString('adm-mini-bar-row-link', $html);

        // 漏斗：待支付 / 待生效 / 可同步
        $this->assertStringContainsString('href="/admin/platform-orders?bmpa_processable_only=1&back=%2Fadmin"', $html);
        $this->assertStringContainsString('href="/admin/platform-orders?payment_status=paid&status=pending&sync_status=unsynced&back=%2Fadmin"', $html);
        $this->assertStringContainsString('href="/admin/platform-orders?syncable_only=1&sync_status=unsynced&back=%2Fadmin"', $html);

        // 治理：同步失败 / BMPA失败 / 已付无回执 / 续费缺订阅 / 对账不一致 / 退款不一致
        $this->assertStringContainsString('href="/admin/platform-orders?sync_status=failed&back=%2Fadmin"', $html);
        $this->assertStringContainsString('href="/admin/platform-orders?bmpa_failed_only=1&back=%2Fadmin"', $html);
        $this->assertStringContainsString('href="/admin/platform-orders?payment_status=paid&receipt_status=none&back=%2Fadmin"', $html);
        $this->assertStringContainsString('href="/admin/platform-orders?renewal_missing_subscription=1&back=%2Fadmin"', $html);
        $this->assertStringContainsString('href="/admin/platform-orders?reconcile_mismatch=1&back=%2Fadmin"', $html);
        $this->assertStringContainsString('href="/admin/platform-orders?refund_inconsistent=1&back=%2Fadmin"', $html);

        // 订阅到期治理
        $this->assertStringContainsString('href="/admin/site-subscriptions?expiry=expiring_7d&back=%2Fadmin"', $html);
        $this->assertStringContainsString('href="/admin/site-subscriptions?expiry=expired&back=%2Fadmin"', $html);

        // back 参数不得被 Blade escape 成 &amp;back
        $this->assertStringNotContainsString('&amp;back=', $html);
    }
}
