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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'audit_snapshot_fallback_plan_01',
            'name' => '审计快照降级展示测试',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $order = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_AUDIT_SNAPSHOT_FALLBACK_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' => [
                'audit' => [
                    [
                        'action' => 'batch_activate_subscription',
                        'scope' => 'filtered',
                        'at' => now()->toDateTimeString(),
                        'admin_id' => 1,
                        'subscription_id' => 123,
                        'filters' => 'syncable_only=1&refund_inconsistent=1',
                        'note' => '测试用审计记录',
                    ],
                ],
            ],
        ]);

        $this->get('/admin/platform-orders/' . $order->id)
            ->assertOk()
            ->assertSee('subscription_id=123')
            ->assertSee('filters=syncable_only=1');
    }
}
