<?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 AdminPlatformOrderBatchMarkPaidAndActivateToleranceConfigTest 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_batch_mark_paid_and_activate_uses_configured_tolerance_for_receipt_mismatch_safety_valve(): void
    {
        // 将容差放大到 0.05（5 分），以验证 safety valve 使用配置，而不是硬编码 0.01（1 分）
        config()->set('saasshop.amounts.tolerance', 0.05);

        $this->loginAsPlatformAdmin();

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

        // 应付 10.00，回执 10.02（差 2 分）
        // 如果容差=5 分，则允许通过，不应写入 batch_mark_paid_and_activate_error
        $order = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_BMPA_TOL_0001',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'unpaid',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'quantity' => 1,
            'payable_amount' => 10,
            'paid_amount' => 0,
            'placed_at' => now(),
            'meta' => [
                'payment_receipts' => [
                    [
                        'type' => 'manual',
                        'channel' => 'test',
                        'amount' => 10.02,
                        'paid_at' => now()->toDateTimeString(),
                        'note' => '回执略微偏差',
                    ],
                ],
                'payment_summary' => [
                    'count' => 1,
                    'total_amount' => 10.02,
                    'last_at' => now()->toDateTimeString(),
                    'last_amount' => 10.02,
                    'last_channel' => 'test',
                ],
            ],
        ]);

        $this->post('/admin/platform-orders/batch-mark-paid-and-activate', [
            'scope' => 'filtered',
            'status' => 'pending',
            'payment_status' => 'unpaid',
            'limit' => 50,
        ])->assertRedirect();

        $order->refresh();

        $this->assertEmpty(data_get($order->meta, 'batch_mark_paid_and_activate_error.message'));
        $this->assertSame('paid', $order->payment_status);
        $this->assertSame('activated', $order->status);
    }
}
