<?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 AdminPlatformOrderIndexRefundInconsistentFilterShouldWorkTest 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_refund_inconsistent_filter_should_include_and_exclude_expected_orders(): void
    {
        $this->loginAsPlatformAdmin();

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'po_refund_inconsistent_filter_plan',
            'name' => '退款不一致筛选测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        // A: payment_status=refunded 但 refund_total < paid_amount（应命中）
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_REFUND_INCONSISTENT_YES_0001',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'refunded',
            'payable_amount' => 10,
            'paid_amount' => 10,
            'meta' => [
                'refund_summary' => [
                    'total_amount' => 9,
                ],
            ],
        ]);

        // B: payment_status=paid 且 refund_total >= paid_amount + 容差（应命中：状态非 refunded 但退款已覆盖已付，口径按系统容差）
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_REFUND_INCONSISTENT_YES_0002',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'paid',
            'payable_amount' => 10,
            'paid_amount' => 10,
            'meta' => [
                'refund_summary' => [
                    // 默认容差为 0.01，因此这里用 10.02 确保命中“退款已覆盖已付 + 容差”
                    'total_amount' => 10.02,
                ],
            ],
        ]);

        // C: payment_status=paid 且 refund_total < paid_amount（不命中）
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_REFUND_INCONSISTENT_NO_0001',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'paid',
            'payable_amount' => 10,
            'paid_amount' => 10,
            'meta' => [
                'refund_summary' => [
                    'total_amount' => 1,
                ],
            ],
        ]);

        $res = $this->get('/admin/platform-orders?refund_inconsistent=1');
        $res->assertOk();

        $res->assertSee('PO_REFUND_INCONSISTENT_YES_0001', false);
        $res->assertSee('PO_REFUND_INCONSISTENT_YES_0002', false);
        $res->assertDontSee('PO_REFUND_INCONSISTENT_NO_0001', false);
    }
}
