<?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 AdminDashboardBillingWorkbenchBmpaProcessableCountShouldExcludeRefundTraceAndRenewalMissingSubscriptionTest 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_bmpa_processable_count_should_exclude_refund_trace_and_renewal_missing_subscription(): void
    {
        Cache::flush();

        $this->loginAsPlatformAdmin();

        // 清理 seed 订单，避免统计受 seed 影响。
        PlatformOrder::query()->delete();

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

        // 1) 可处理：pending+unpaid 且无退款轨迹
        PlatformOrder::query()->create([
            'merchant_id' => $merchantId,
            'plan_id' => null,
            'site_subscription_id' => null,
            'created_by_admin_id' => $platformAdminId ?: null,
            'order_no' => 'PO_DASH_BMPA_PROCESSABLE_0001',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'unpaid',
            'payable_amount' => 9,
            'paid_amount' => 0,
            'meta' => [],
        ]);

        // 2) 不可处理：pending+unpaid 但有退款轨迹（refund_total>0）
        PlatformOrder::query()->create([
            'merchant_id' => $merchantId,
            'plan_id' => null,
            'site_subscription_id' => null,
            'created_by_admin_id' => $platformAdminId ?: null,
            'order_no' => 'PO_DASH_BMPA_BLOCKED_REFUND_TRACE_0002',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'unpaid',
            'payable_amount' => 9,
            'paid_amount' => 0,
            'meta' => [
                'refund_summary' => [
                    'total_amount' => 1,
                    'count' => 1,
                ],
            ],
        ]);

        // 3) 不可处理：pending+unpaid 且续费缺订阅
        PlatformOrder::query()->create([
            'merchant_id' => $merchantId,
            'plan_id' => null,
            'site_subscription_id' => null,
            'created_by_admin_id' => $platformAdminId ?: null,
            'order_no' => 'PO_DASH_BMPA_BLOCKED_RENEWAL_MISSING_SUB_0003',
            'order_type' => 'renewal',
            'status' => 'pending',
            'payment_status' => 'unpaid',
            'payable_amount' => 9,
            'paid_amount' => 0,
            'meta' => [],
        ]);

        Cache::flush();

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

        // 只应统计 1 条真正可 BMPA 处理的订单
        $res->assertSee('可BMPA处理（1）');
        $res->assertDontSee('可BMPA处理（3）');
        $res->assertDontSee('可BMPA处理（2）');
    }
}
