<?php

namespace Tests\Feature;

use App\Models\Merchant;
use App\Models\Plan;
use App\Models\PlatformOrder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Arr;
use Tests\TestCase;

class AdminPlatformOrderFailedReasonStatsHaveEnterFailedSetLinksTest 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_failed_reason_stats_should_render_enter_failed_set_links(): void
    {
        $this->loginAsPlatformAdmin();

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'po_failed_reason_enter_failed_set',
            'name' => '失败原因统计进入失败集合链接测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $syncReason = '订单未生效';
        $bmpaReason = '回执缺失';

        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_FAILED_REASON_SYNC_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' => 10,
            'paid_amount' => 0,
            'placed_at' => now(),
            'meta' => [
                'subscription_activation_error' => [
                    'message' => $syncReason,
                    'at' => now()->format('Y-m-d H:i:s'),
                ],
            ],
        ]);

        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_FAILED_REASON_BMPA_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' => 10,
            'paid_amount' => 0,
            'placed_at' => now(),
            'meta' => [
                'batch_mark_paid_and_activate_error' => [
                    'message' => $bmpaReason,
                    'at' => now()->format('Y-m-d H:i:s'),
                ],
            ],
        ]);

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

        $expectedSyncEnterFailedUrl = '/admin/platform-orders?' . Arr::query([
            'sync_error_keyword' => $syncReason,
            'sync_status' => 'failed',
        ]);

        $expectedBmpaEnterFailedUrl = '/admin/platform-orders?' . Arr::query([
            'bmpa_failed_only' => '1',
            'bmpa_error_keyword' => $bmpaReason,
        ]);

        $res->assertSee('进入失败集合', false);
        $res->assertSee($expectedSyncEnterFailedUrl, false);
        $res->assertSee($expectedBmpaEnterFailedUrl, false);
    }
}
