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

        // 设置较大的容差（用于验证“refund_total + tol >= paid”口径）
        config(['saasshop.amounts.tolerance' => 0.50]);

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

        // 已付 10.00；退款总额 9.60；在 tol=0.50 下应允许标记为 refunded
        $order = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_REFUND_TOL_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' => [
                'refund_summary' => [
                    'count' => 1,
                    'total_amount' => 9.60,
                ],
            ],
        ]);

        $this->post('/admin/platform-orders/' . $order->id . '/mark-refunded')
            ->assertRedirect();

        $order->refresh();
        $this->assertSame('refunded', $order->payment_status);
        $this->assertNotNull($order->refunded_at);
    }

    public function test_mark_partially_refunded_is_blocked_when_refund_total_reaches_paid_with_tolerance(): void
    {
        $this->loginAsPlatformAdmin();

        config(['saasshop.amounts.tolerance' => 0.50]);

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'refund_tol_plan_02',
            'name' => '退款容差配置测试套餐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_REFUND_TOL_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' => [
                'refund_summary' => [
                    'count' => 1,
                    'total_amount' => 9.60,
                ],
            ],
        ]);

        $this->post('/admin/platform-orders/' . $order->id . '/mark-partially-refunded')
            ->assertRedirect();

        $order->refresh();
        $this->assertSame('paid', $order->payment_status);
    }
}
