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

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

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

        // A：refunded，但只差 0.02（在 tol=0.05 内）=> 不应命中
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_EXP_REFUND_INCONS_TOL_A',
            'order_type' => 'new_purchase',
            'status' => 'activated',
            'payment_status' => 'refunded',
            '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(),
            'refunded_at' => now(),
            'meta' => [
                'refund_summary' => [
                    'count' => 1,
                    'total_amount' => 9.98,
                ],
            ],
        ]);

        // B：refunded，差 0.10（超出 tol=0.05）=> 应命中
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_EXP_REFUND_INCONS_TOL_B',
            'order_type' => 'new_purchase',
            'status' => 'activated',
            'payment_status' => 'refunded',
            '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(),
            'refunded_at' => now(),
            'meta' => [
                'refund_summary' => [
                    'count' => 1,
                    'total_amount' => 9.90,
                ],
            ],
        ]);

        // C：非 refunded，但只多 0.02（在 tol=0.05 内）=> 不应命中
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_EXP_REFUND_INCONS_TOL_C',
            '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' => 10.02,
                ],
            ],
        ]);

        // D：非 refunded，多 0.06（超出 tol=0.05）=> 应命中
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_EXP_REFUND_INCONS_TOL_D',
            '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' => 10.06,
                ],
            ],
        ]);

        $res = $this->get('/admin/platform-orders/export?download=1&refund_inconsistent=1');
        $res->assertOk();

        $content = $res->streamedContent();

        $this->assertStringNotContainsString('PO_EXP_REFUND_INCONS_TOL_A', $content);
        $this->assertStringContainsString('PO_EXP_REFUND_INCONS_TOL_B', $content);
        $this->assertStringNotContainsString('PO_EXP_REFUND_INCONS_TOL_C', $content);
        $this->assertStringContainsString('PO_EXP_REFUND_INCONS_TOL_D', $content);
    }
}
