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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'audit_payment_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();
        $this->assertSame('unpaid', (string) $order->payment_status);
        $this->assertSame('pending', (string) $order->status);

        $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();

        $order->refresh();

        $this->assertSame('unpaid', (string) $order->payment_status);
        $this->assertSame('pending', (string) $order->status);
        $this->assertSame(1, (int) data_get($order->meta, 'payment_summary.count'));
        $this->assertSame(30.0, (float) data_get($order->meta, 'payment_summary.total_amount'));

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

        $last = end($audit);
        $this->assertSame('add_payment_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(30.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(30.0, (float) data_get($last, 'snapshot.summary.total_amount'));
    }
}
