<?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 PlatformOrderReconcileMismatchTest extends TestCase
{
    use RefreshDatabase;

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

        $plan = Plan::query()->create([
            'code' => 'unit_test_plan_reconcile_mismatch',
            'name' => '单测套餐_reconcile',
            '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_RECONCILE_MISMATCH_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_summary' => [
                    'total_amount' => 9.99,
                ],
            ],
        ]);

        $this->assertTrue($order->isReconcileMismatch());
    }

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

        $plan = Plan::query()->create([
            'code' => 'unit_test_plan_reconcile_none',
            'name' => '单测套餐_reconcile_none',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        // 无 payment_summary/payment_receipts：不应判定为对账不一致
        $order = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_UNIT_RECONCILE_NONE_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' => [],
        ]);

        $this->assertFalse($order->isReconcileMismatch());
    }

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

        $plan = Plan::query()->create([
            'code' => 'unit_test_plan_reconcile_match',
            'name' => '单测套餐_reconcile_2',
            '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_RECONCILE_MATCH_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_summary' => [
                    'total_amount' => 10.00,
                ],
            ],
        ]);

        $this->assertFalse($order->isReconcileMismatch());
    }
}
