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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'reconcile_mismatch_receipts_fallback',
            'name' => '对账不一致回退回执汇总测试',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        // 不一致：paid_amount=10，但 payment_receipts 合计=9.99（且没有 payment_summary）
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_RECON_FALLBACK_MISMATCH_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_receipts' => [
                    [
                        'type' => 'bank_transfer',
                        'channel' => 'bank',
                        'amount' => 9.99,
                        'paid_at' => now()->toDateTimeString(),
                        'created_at' => now()->toDateTimeString(),
                        'admin_id' => 1,
                    ],
                ],
            ],
        ]);

        // 一致：paid_amount=10，payment_receipts 合计=10（且没有 payment_summary）
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_RECON_FALLBACK_MATCH_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_receipts' => [
                    [
                        'type' => 'bank_transfer',
                        'channel' => 'bank',
                        'amount' => 10.00,
                        'paid_at' => now()->toDateTimeString(),
                        'created_at' => now()->toDateTimeString(),
                        'admin_id' => 1,
                    ],
                ],
            ],
        ]);

        $this->get('/admin/platform-orders?reconcile_mismatch=1')
            ->assertOk()
            ->assertSee('PO_RECON_FALLBACK_MISMATCH_0001')
            ->assertDontSee('PO_RECON_FALLBACK_MATCH_0002');
    }
}
