<?php

namespace Tests\Unit;

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

class PlatformOrderReceiptTotalTest extends TestCase
{
    use RefreshDatabase;

    public function test_receipt_total_can_fallback_to_receipts_sum_when_payment_summary_missing(): void
    {
        $merchant = Merchant::query()->create([
            'name' => '单测站点_receipt',
            'slug' => 'unit-test-merchant-receipt',
            'status' => 'active',
        ]);

        $plan = Plan::query()->create([
            'code' => 'unit_test_plan_receipt_total',
            'name' => '单测套餐_receipt',
            '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_UNIT_RECEIPT_TOTAL_0001',
            'order_type' => 'subscription',
            'status' => 'activated',
            'payment_status' => 'paid',
            'plan_name' => 'Unit Test Plan',
            'billing_cycle' => 'monthly',
            'period_months' => 1,
            'quantity' => 1,
            'payable_amount' => 10,
            'paid_amount' => 10,
            'placed_at' => now(),
            'meta' => [
                'payment_receipts' => [
                    ['amount' => 3.25],
                    ['amount' => 1.15],
                ],
            ],
        ]);

        $this->assertEquals(4.40, $order->receiptTotal());
    }
}
