<?php

namespace Tests\Feature;

use App\Models\Admin;
use App\Models\Merchant;
use App\Models\PlatformOrder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Tests\TestCase;

class AdminDashboardBillingWorkbenchQuickLinksShouldShowCountsTest 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_billing_workbench_quick_links_should_show_counts(): void
    {
        Cache::flush();

        $this->loginAsPlatformAdmin();

        $merchantId = (int) Merchant::query()->value('id');
        $platformAdminId = (int) Admin::query()->where('email', 'platform.admin@demo.local')->value('id');

        // 构造三类集合：待支付 / 待生效 / 同步失败
        PlatformOrder::query()->create([
            'merchant_id' => $merchantId,
            'plan_id' => null,
            'site_subscription_id' => null,
            'created_by_admin_id' => $platformAdminId ?: null,
            'order_no' => 'PO_DASHBOARD_UNPAID_PENDING',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'unpaid',
            'payable_amount' => 9,
            'paid_amount' => 0,
        ]);

        PlatformOrder::query()->create([
            'merchant_id' => $merchantId,
            'plan_id' => null,
            'site_subscription_id' => null,
            'created_by_admin_id' => $platformAdminId ?: null,
            'order_no' => 'PO_DASHBOARD_PAID_PENDING',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'paid',
            'payable_amount' => 9,
            'paid_amount' => 9,
        ]);

        // 脏数据兜底：即使 status=pending + paid，但若 meta 已写入 subscription_activation（视为已同步），不应计入“待生效(unsynced)”集合。
        PlatformOrder::query()->create([
            'merchant_id' => $merchantId,
            'plan_id' => null,
            'site_subscription_id' => null,
            'created_by_admin_id' => $platformAdminId ?: null,
            'order_no' => 'PO_DASHBOARD_PAID_PENDING_BUT_ALREADY_SYNCED',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'paid',
            'payable_amount' => 9,
            'paid_amount' => 9,
            'meta' => [
                'subscription_activation' => [
                    'subscription_id' => 123,
                ],
            ],
        ]);

        PlatformOrder::query()->create([
            'merchant_id' => $merchantId,
            'plan_id' => null,
            'site_subscription_id' => null,
            'created_by_admin_id' => $platformAdminId ?: null,
            'order_no' => 'PO_DASHBOARD_SYNC_FAILED',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'paid',
            'payable_amount' => 9,
            'paid_amount' => 9,
            'meta' => [
                'subscription_activation_error' => [
                    'message' => 'sync failed',
                ],
            ],
        ]);

        Cache::flush();

        // Debug：确保我们构造的数据在 DB 里
        $this->assertSame(1, PlatformOrder::query()->where('payment_status', 'unpaid')->where('status', 'pending')->count());
        $this->assertSame(3, PlatformOrder::query()->where('payment_status', 'paid')->where('status', 'pending')->count());

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

        // 快捷区已按“治理导向”收敛：待支付/待生效不再作为默认快捷标签。
        // 本用例仅验证计数渲染：可BMPA处理（对应 unpaid+pending）、同步失败。
        $res->assertSee('可BMPA处理（1）');
        $res->assertSee('同步失败（1）');

        // 待生效属于“高级筛选”（默认收起），仍需渲染正确计数。
        $res->assertSee('待生效（1）');
    }
}
