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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'audit_refund_receipt_plan',
            'name' => '审计退款用套餐',
            'billing_cycle' => 'monthly',
            'price' => 30,
            'list_price' => 30,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $this->post('/admin/platform-orders', [
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_type' => 'new_purchase',
            'quantity' => 1,
            'discount_amount' => 0,
            'payment_channel' => 'bank_transfer',
            'remark' => '审计退款测试',
        ])->assertRedirect();

        /** @var PlatformOrder $order */
        $order = PlatformOrder::query()->latest('id')->firstOrFail();

        // 先让订单进入 paid，便于验证 addRefundReceipt 的“自动推进支付状态”口径
        $this->post('/admin/platform-orders/' . $order->id . '/add-payment-receipt', [
            'type' => 'bank_transfer',
            'channel' => 'icbc',
            'amount' => 30,
            'paid_at' => now()->format('Y-m-d H:i:s'),
            'note' => '财务确认',
        ])->assertRedirect();

        $this->post('/admin/platform-orders/' . $order->id . '/mark-paid-and-activate')
            ->assertRedirect();

        $order->refresh();
        $this->assertSame('paid', (string) $order->payment_status);
        $this->assertSame('activated', (string) $order->status);

        // 追加一笔部分退款
        $this->post('/admin/platform-orders/' . $order->id . '/add-refund-receipt', [
            'type' => 'refund',
            'channel' => 'icbc',
            'amount' => 10,
            'refunded_at' => now()->format('Y-m-d H:i:s'),
            'note' => '部分退款',
        ])->assertRedirect();

        $order->refresh();
        $this->assertSame('partially_refunded', (string) $order->payment_status);
        $this->assertSame(1, (int) data_get($order->meta, 'refund_summary.count'));
        $this->assertSame(10.0, (float) data_get($order->meta, 'refund_summary.total_amount'));

        $audit = (array) (data_get($order->meta, 'audit', []) ?? []);
        $this->assertNotEmpty($audit);

        $last = end($audit);
        $this->assertSame('add_refund_receipt', (string) data_get($last, 'action'));
        $this->assertSame('single', (string) data_get($last, 'scope'));
        $this->assertNotEmpty((string) data_get($last, 'at'));
        $this->assertGreaterThan(0, (int) data_get($last, 'admin_id'));
        $this->assertSame(10.0, (float) data_get($last, 'snapshot.amount'));
        $this->assertSame('icbc', (string) data_get($last, 'snapshot.channel'));
        $this->assertSame(1, (int) data_get($last, 'snapshot.summary.count'));
        $this->assertSame(10.0, (float) data_get($last, 'snapshot.summary.total_amount'));
    }
}
