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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'po_show_batch_run_id_plan_01',
            'name' => '平台订单详情批次run_id链接测试套餐',
            '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_SHOW_BATCH_RUN_ID_0001',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            '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(),
            'meta' => [
                'batch_activation' => [
                    'run_id' => 'RUN_BAS_0001',
                ],
                'batch_mark_paid_and_activate' => [
                    'run_id' => 'RUN_BMPA_0001',
                ],
            ],
        ]);

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

        // BAS run_id 应链接到批次复盘页（携带 back 回到订单详情）
        $res->assertSee('RUN_BAS_0001', false);
        $res->assertSee('/admin/platform-batches/show?type=bas&run_id=RUN_BAS_0001', false);
        $res->assertSee('back=%2Fadmin%2Fplatform-orders%2F' . $order->id, false);

        // BMPA run_id 应链接到批次复盘页（携带 back 回到订单详情）
        $res->assertSee('RUN_BMPA_0001', false);
        $res->assertSee('/admin/platform-batches/show?type=bmpa&run_id=RUN_BMPA_0001', false);
    }
}
