<?php

namespace Tests\Feature;

use App\Models\Admin;
use App\Models\Merchant;
use App\Models\PlatformOrder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

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

        // 清理 seed 订单，避免最近订单列表被 seed 污染导致断言不稳定。
        PlatformOrder::query()->delete();

        $merchantId = (int) Merchant::query()->value('id');
        $platformAdminId = (int) Admin::query()->where('email', 'platform.admin@demo.local')->value('id');

        PlatformOrder::query()->create([
            'merchant_id' => $merchantId,
            'plan_id' => null,
            'site_subscription_id' => null,
            'created_by_admin_id' => $platformAdminId ?: null,
            'order_no' => 'PO_DASH_SCANLINE_PARTIAL_REFUND_0001',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'partially_refunded',
            'payable_amount' => 10,
            'paid_amount' => 10,
            'placed_at' => now(),
            'paid_at' => now(),
            'meta' => [
                'refund_summary' => [
                    'total_amount' => 1,
                    'count' => 1,
                ],
            ],
        ]);

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

        $res->assertSee('PO_DASH_SCANLINE_PARTIAL_REFUND_0001');

        // partially_refunded 也应展示退款轨迹（不应为 "-"）
        $res->assertSee('退款：', false);
        $res->assertSee('<strong>有</strong>', false);

        // 并保持最短治理入口：直达 add-refund-receipt
        $res->assertSee('#add-refund-receipt', false);
    }
}
