<?php

namespace Tests\Feature;

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

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

        $this->loginAsPlatformAdmin();

        // 清理 seed 中的订单数据，避免 seed 口径变化影响该用例
        PlatformOrder::query()->delete();

        $merchantId = (int) Merchant::query()->value('id');

        // A：已付 + 无回执（应计入）
        PlatformOrder::query()->create([
            'merchant_id' => $merchantId,
            'plan_id' => null,
            'site_subscription_id' => null,
            'created_by_admin_id' => null,
            'order_no' => 'PO_DASH_PAID_NO_RECEIPT_CNT_0001',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'paid',
            'payable_amount' => 9,
            'paid_amount' => 9,
            'meta' => [],
        ]);

        // B：未付 + 无回执（不应计入）
        PlatformOrder::query()->create([
            'merchant_id' => $merchantId,
            'plan_id' => null,
            'site_subscription_id' => null,
            'created_by_admin_id' => null,
            'order_no' => 'PO_DASH_PAID_NO_RECEIPT_CNT_0002',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'unpaid',
            'payable_amount' => 9,
            'paid_amount' => 0,
            'meta' => [],
        ]);

        Cache::flush();

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

        // 只统计已付无回执
        $res->assertSee('已付无回执（1）', false);
        $res->assertDontSee('已付无回执（2）', false);
    }
}
