<?php

namespace Tests\Feature;

use App\Models\Merchant;
use App\Models\Plan;
use App\Models\PlatformOrder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'po_paid_no_receipt_filter_plan',
            'name' => '已付无回执筛选测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        // A: 已付 + 无回执（应命中）
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_PAID_NO_RECEIPT_OK_0001',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'paid',
            'payable_amount' => 10,
            'paid_amount' => 10,
            'meta' => [
                // 无 payment_summary / payment_receipts
            ],
        ]);

        // B: 已付 + 有回执（不应命中）
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_PAID_NO_RECEIPT_NO_0001',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'paid',
            'payable_amount' => 10,
            'paid_amount' => 10,
            'meta' => [
                'payment_summary' => ['total_amount' => 10],
            ],
        ]);

        // C: 未付 + 无回执（不应命中，因为入口是“已付无回执”）
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_PAID_NO_RECEIPT_NO_0002',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'unpaid',
            'payable_amount' => 10,
            'paid_amount' => 0,
            'meta' => [],
        ]);

        $res = $this->get('/admin/platform-orders?payment_status=paid&receipt_status=none');
        $res->assertOk();

        $res->assertSee('PO_PAID_NO_RECEIPT_OK_0001', false);
        $res->assertDontSee('PO_PAID_NO_RECEIPT_NO_0001', false);
        $res->assertDontSee('PO_PAID_NO_RECEIPT_NO_0002', false);
    }
}
