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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'po_show_reconcile_refund_list_back_plan',
            'name' => '平台订单详情对账/退款不一致列表 back 链接测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        // 造一个“对账不一致”的订单：receipt_total(9) vs paid_amount(10)
        $order = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_SHOW_LIST_BACK_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' => [
                'payment_summary' => ['count' => 1, 'total' => 9],
                'payment_receipts' => [
                    ['amount' => 9, 'paid_at' => now()->toDateTimeString(), 'channel' => 'offline', 'note' => 'test'],
                ],
                // 同时造一个“退款不一致”态：payment_status=refunded 但退款总额不足
                'refund_summary' => ['count' => 1, 'total' => 1],
                'refund_receipts' => [
                    ['amount' => 1, 'refunded_at' => now()->toDateTimeString(), 'channel' => 'offline', 'note' => 'test'],
                ],
            ],
        ]);

        // 为了触发退款不一致提示：将状态改为 refunded
        $order->update(['payment_status' => 'refunded']);

        // 模拟从列表进入详情页（带 back）
        $res = $this->get('/admin/platform-orders/' . $order->id . '?back=' . urlencode('/admin/platform-orders?status=pending'));
        $res->assertOk();

        // 详情页自身（去掉 back）
        $expectedOrderShowSelf = '/admin/platform-orders/' . $order->id;

        $expectedReconcileUrl = '/admin/platform-orders?' . Arr::query([
            'reconcile_mismatch' => 1,
            'back' => $expectedOrderShowSelf,
        ]);

        $expectedRefundUrl = '/admin/platform-orders?' . Arr::query([
            'refund_inconsistent' => 1,
            'back' => $expectedOrderShowSelf,
        ]);

        $res->assertSee($expectedReconcileUrl, false);
        $res->assertSee($expectedRefundUrl, false);

        // 不允许 back 嵌套
        $res->assertDontSee('back%3D', false);
    }
}
