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

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

        $plan = Plan::query()->create([
            'code' => 'po_show_export_ledger_link_order_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_SHOW_EXPORT_LEDGER_LINK_ORDER_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' => [],
        ]);

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

        $base = '/admin/platform-orders/' . $order->id . '/export-ledger?download=1';
        $withSnapshot = '/admin/platform-orders/' . $order->id . '/export-ledger?download=1&include_order_snapshot=1';

        $res->assertSee($base, false);
        $res->assertSee($withSnapshot, false);

        // 反向断言：不允许 include_order_snapshot 出现在 download 之前（保持 URL 口径稳定，避免回退时测试/前端混乱）
        $res->assertDontSee('/admin/platform-orders/' . $order->id . '/export-ledger?include_order_snapshot=1&download=1', false);
    }
}
