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

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

        $plan = Plan::query()->create([
            'code' => 'compact_view_no_receipt_fix_link_test',
            '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_COMPACT_NO_RECEIPT_0001',
            'order_type' => 'new_purchase',
            'status' => 'activated',
            '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(),
            'activated_at' => now(),
            // 关键：无 payment_summary / payment_receipts
            'meta' => [],
        ]);

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

        $indexSelfWithoutBack = '/admin/platform-orders?' . Arr::query([
            'status' => 'activated',
        ]);

        $fixUrl = '/admin/platform-orders/' . $order->id . '?' . Arr::query([
            'back' => $indexSelfWithoutBack,
        ]) . '#add-payment-receipt';

        $res->assertSee('已付无回执', false);
        $res->assertSee($fixUrl, false);
        $res->assertSee('去补回执', false);

        // full 视图：仍应保留该提示（因为“已付无回执”不属于对账差额/退款总额等可选列的内容）
        $this->get('/admin/platform-orders?status=activated&view=full')
            ->assertOk()
            ->assertSee('已付无回执', false);
    }
}
