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

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

        $plan = Plan::query()->create([
            'code' => 'po_index_export_ledger_link_01',
            'name' => '平台订单列表导出对账明细入口测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $withEvidence = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_INDEX_LEDGER_EVIDENCE_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' => [
                'payment_summary' => [
                    'count' => 1,
                    'total_amount' => 10,
                ],
            ],
        ]);

        $withoutEvidence = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_INDEX_LEDGER_NO_EVIDENCE_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' => [],
        ]);

        $url = '/admin/platform-orders/' . $withEvidence->id . '/export-ledger?download=1&include_order_snapshot=1';

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

        $res->assertSee($url, false);
        $res->assertSee('导出对账明细', false);
        $res->assertSee('target="_blank"', false);

        // 口径稳定：download=1 必须排在 query 第一位（安全阀 + 测试/前端稳定）
        $res->assertDontSee('/admin/platform-orders/' . $withEvidence->id . '/export-ledger?include_order_snapshot=1&download=1', false);

        $url2 = '/admin/platform-orders/' . $withoutEvidence->id . '/export-ledger?download=1&include_order_snapshot=1';
        $res->assertDontSee($url2, false);
    }
}
