<?php

namespace Tests\Feature;

use App\Models\Merchant;
use App\Models\Plan;
use App\Models\PlatformOrder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Tests\TestCase;

class AdminDashboardBillingWorkbenchShouldIncludeReconcileMismatchAndRefundInconsistentQuickLinksTest 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_dashboard_billing_workbench_should_include_reconcile_mismatch_and_refund_inconsistent_quick_links(): void
    {
        Cache::flush();

        $this->loginAsPlatformAdmin();

        // 清理 seed 订单，避免 seed 口径变化导致该用例不稳定。
        PlatformOrder::query()->delete();

        $merchant = Merchant::query()->firstOrFail();

        $plan = Plan::query()->create([
            'code' => 'dash_governance_mismatch_test',
            'name' => '仪表盘治理入口对账/退款不一致测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        // 1) 对账不一致：paid_amount=10，但 payment_summary.total_amount=9.99
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_DASH_RECON_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_summary' => [
                    'count' => 1,
                    'total_amount' => 9.99,
                    'last_at' => now()->toDateTimeString(),
                    'last_amount' => 9.99,
                    'last_channel' => 'bank',
                ],
            ],
        ]);

        // 2) 退款不一致：状态=refunded，但退款总额不足
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_DASH_REFUND_INCONSISTENT_0001',
            '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,
                    'last_at' => now()->toDateTimeString(),
                    'last_amount' => 9.98,
                    'last_channel' => 'bank',
                ],
            ],
        ]);

        Cache::flush();

        $res = $this->get('/admin');
        $res->assertOk();

        $res->assertSee('对账不一致（1）');
        $res->assertSee('退款不一致（1）');

        $res->assertSee('reconcile_mismatch=1', false);
        $res->assertSee('refund_inconsistent=1', false);

        // 链接应携带 back，并且不应出现 &amp;back=
        $res->assertSee('back=%2Fadmin', false);
        $res->assertDontSee('&amp;back=', false);
    }
}
