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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'refund_row_summary_test',
            'name' => '退款行级摘要测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_REFUND_ROW_SUMMARY_0001',
            'order_type' => 'new_purchase',
            'status' => 'activated',
            'payment_status' => 'partially_refunded',
            '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(),
            'meta' => [
                // 故意不给 refund_receipts，验证列表能从 refund_summary 读到
                'refund_summary' => [
                    'count' => 2,
                    'total_amount' => 3.50,
                ],
            ],
        ]);

        $this->get('/admin/platform-orders')
            ->assertOk()
            ->assertSee('PO_REFUND_ROW_SUMMARY_0001')
            ->assertSee('2')
            ->assertSee('¥3.50');
    }
}
