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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'audit_receipt_action_labels_plan',
            '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_AUDIT_RECEIPT_ACTIONS_0001',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'unpaid',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'quantity' => 1,
            'payable_amount' => 30,
            'paid_amount' => 0,
            'placed_at' => now(),
            'meta' => [
                'audit' => [
                    [
                        'action' => 'add_payment_receipt',
                        'scope' => 'single',
                        'at' => now()->toDateTimeString(),
                        'admin_id' => 1,
                        'snapshot' => [
                            'amount' => 30,
                            'channel' => 'icbc',
                            'paid_at' => now()->toDateTimeString(),
                        ],
                        'note' => '追加支付回执',
                    ],
                    [
                        'action' => 'add_refund_receipt',
                        'scope' => 'single',
                        'at' => now()->toDateTimeString(),
                        'admin_id' => 1,
                        'snapshot' => [
                            'amount' => 10,
                            'channel' => 'icbc',
                            'refunded_at' => now()->toDateTimeString(),
                        ],
                        'note' => '追加退款回执',
                    ],
                ],
            ],
        ]);

        $this->get('/admin/platform-orders/' . $order->id)
            ->assertOk()
            ->assertSee('追加支付回执')
            ->assertSee('追加退款回执');
    }
}
