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

        $merchant = Merchant::query()->firstOrFail();

        $plan = Plan::query()->create([
            'code' => 'po_index_row_warn_prefix_plan',
            'name' => '平台订单列表 row-warn prefix 测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_INDEX_ROW_WARN_PREFIX_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' => [
                'subscription_activation_error' => [
                    'message' => 'SIM_SYNC_ERR_2',
                    'at' => now()->toDateTimeString(),
                ],
                'batch_mark_paid_and_activate_error' => [
                    'message' => 'SIM_BMPA_ERR_2',
                    'at' => now()->toDateTimeString(),
                ],
            ],
        ]);

        $res = $this->get('/admin/platform-orders?keyword=PO_INDEX_ROW_WARN_PREFIX_0001');
        $res->assertOk();

        $res->assertSee('<span class="row-warn-prefix">已付无回执</span>', false);
        $res->assertSee('<span class="row-warn-prefix">原因</span>', false);
        $res->assertSee('<span class="row-warn-prefix">BMPA</span>', false);
    }
}
