<?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 AdminPlatformOrderReconcileMismatchToleranceConfigFilterTest 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_reconcile_mismatch_filter_should_respect_amount_tolerance_config(): void
    {
        // 将容差调大，确保“差额小于容差”不被判定为对账不一致
        config()->set('saasshop.amounts.tolerance', 0.05);

        $this->loginAsPlatformAdmin();

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'po_reconcile_mismatch_tol_plan',
            'name' => '对账不一致容差配置测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        // A：差额 0.04 < tol(0.05) => 不应命中
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_RECON_TOL_NO_0001',
            'order_type' => 'new_purchase',
            'status' => 'activated',
            '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(),
            'activated_at' => now(),
            'meta' => [
                'payment_summary' => [
                    'count' => 1,
                    'total_amount' => 9.96,
                    'last_at' => now()->toDateTimeString(),
                    'last_amount' => 9.96,
                    'last_channel' => 'bank',
                ],
            ],
        ]);

        // B：差额 0.06 >= tol(0.05) => 应命中
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_RECON_TOL_YES_0002',
            'order_type' => 'new_purchase',
            'status' => 'activated',
            '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(),
            'activated_at' => now(),
            'meta' => [
                'payment_summary' => [
                    'count' => 1,
                    'total_amount' => 9.94,
                    'last_at' => now()->toDateTimeString(),
                    'last_amount' => 9.94,
                    'last_channel' => 'bank',
                ],
            ],
        ]);

        $res = $this->get('/admin/platform-orders?reconcile_mismatch=1');
        $res->assertOk();

        $res->assertDontSee('PO_RECON_TOL_NO_0001', false);
        $res->assertSee('PO_RECON_TOL_YES_0002', false);
    }
}
