<?php

namespace Tests\Feature;

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

/**
 * 退款轨迹筛选（refund_status）应按“退款总额是否>0”判定。
 * 目的：避免 refund_summary.total_amount=0 这种“空字段”被误判为“有退款”。
 */
class AdminPlatformOrderRefundStatusFilterShouldTreatZeroRefundAsNoneTest 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_status_filter_should_treat_zero_refund_summary_as_none(): void
    {
        $this->loginAsPlatformAdmin();

        $merchant = Merchant::query()->firstOrFail();

        $plan = Plan::query()->create([
            'code' => 'refund_status_zero_should_be_none',
            'name' => '退款轨迹筛选：0 退款应判定为 none',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        // 1) refund_summary.total_amount=0：应视为“无退款”
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_REFUND_SUMMARY_ZERO_0001',
            'order_type' => 'new_purchase',
            'status' => 'activated',
            'payment_status' => 'paid',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'quantity' => 1,
            'payable_amount' => 10,
            'paid_amount' => 10,
            'placed_at' => now(),
            'paid_at' => now(),
            'activated_at' => now(),
            'meta' => [
                'refund_summary' => [
                    'count' => 0,
                    'total_amount' => 0,
                ],
            ],
        ]);

        // 2) refund_receipts 有金额：应视为“有退款”
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_REFUND_RECEIPT_POSITIVE_0002',
            'order_type' => 'new_purchase',
            'status' => 'activated',
            'payment_status' => 'partially_refunded',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'quantity' => 1,
            'payable_amount' => 10,
            'paid_amount' => 10,
            'placed_at' => now(),
            'paid_at' => now(),
            'activated_at' => now(),
            'meta' => [
                'refund_receipts' => [
                    ['amount' => 1.00, 'refunded_at' => now()->toDateTimeString()],
                ],
            ],
        ]);

        $this->get('/admin/platform-orders?refund_status=has')
            ->assertOk()
            ->assertSee('PO_REFUND_RECEIPT_POSITIVE_0002')
            ->assertDontSee('PO_REFUND_SUMMARY_ZERO_0001');

        $this->get('/admin/platform-orders?refund_status=none')
            ->assertOk()
            ->assertSee('PO_REFUND_SUMMARY_ZERO_0001')
            ->assertDontSee('PO_REFUND_RECEIPT_POSITIVE_0002');
    }
}
