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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'refund_idempotent_status',
            'name' => '退款状态幂等测试',
            'billing_cycle' => 'monthly',
            'price' => 30,
            'list_price' => 30,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $order = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_REFUND_IDEMP_0001',
            'order_type' => 'new_purchase',
            'status' => 'activated',
            'payment_status' => 'refunded',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'quantity' => 1,
            'payable_amount' => 30,
            'paid_amount' => 30,
            'placed_at' => now(),
            'paid_at' => now(),
            'activated_at' => now(),
            'refunded_at' => now()->subHour(),
            'meta' => [
                'refund_receipts' => [
                    [
                        'type' => 'refund',
                        'channel' => 'wechat',
                        'amount' => 30,
                        'refunded_at' => now()->subHour()->format('Y-m-d H:i:s'),
                        'created_at' => now()->subHour()->toDateTimeString(),
                        'admin_id' => 1,
                    ],
                ],
                'refund_summary' => [
                    'count' => 1,
                    'total_amount' => 30,
                    'last_at' => now()->subHour()->toDateTimeString(),
                    'last_amount' => 30,
                    'last_channel' => 'wechat',
                ],
            ],
        ]);

        // 追加一条 0 元退款（例如：仅留痕/重复通知），不应把 refunded 降级成 partially_refunded
        $this->post('/admin/platform-orders/' . $order->id . '/add-refund-receipt', [
            'type' => 'refund',
            'channel' => 'wechat',
            'amount' => 0,
            'refunded_at' => now()->format('Y-m-d H:i:s'),
            'note' => '0元留痕',
        ])->assertRedirect();

        $order->refresh();

        $this->assertSame('refunded', $order->payment_status);
        $this->assertNotNull($order->refunded_at);

        $refundSummaryTotal = (float) data_get($order->meta, 'refund_summary.total_amount');
        $this->assertSame(30.0, $refundSummaryTotal);
    }
}
