<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

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

        $merchant = \App\Models\Merchant::query()->firstOrFail();
        $plan = \App\Models\Plan::query()->create([
            'code' => 'po_index_back_plan_01',
            'name' => '平台订单列表 back 参数测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $order = \App\Models\PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_INDEX_BACK_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' => [],
        ]);

        // index 页应存在该订单行链接，且链接带 back（保留筛选上下文）
        $res = $this->get('/admin/platform-orders?status=pending');
        $res->assertOk();

        $expectedBack = \Illuminate\Support\Arr::query(['back' => '/admin/platform-orders?status=pending']);
        $res->assertSee('/admin/platform-orders/' . $order->id . '?' . $expectedBack, false);
    }
}
