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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'show_reconcile_mismatch_hint_01',
            'name' => '对账不一致提示测试套餐',
            '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_SHOW_RECONCILE_MISMATCH_0001',
            'order_type' => 'subscription',
            'status' => 'activated',
            'payment_status' => 'paid',
            'plan_name' => $plan->name,
            'billing_cycle' => 'monthly',
            'period_months' => 1,
            'quantity' => 1,
            'payable_amount' => 10,
            'paid_amount' => 10,
            'placed_at' => now(),
            'paid_at' => now(),
            'meta' => [
                'payment_summary' => [
                    'count' => 1,
                    'total_amount' => 9.99,
                ],
            ],
        ]);

        $this->get('/admin/platform-orders/' . $order->id)
            ->assertOk()
            ->assertSee('对账不一致')
            ->assertSee('对账不一致治理提示')
            ->assertSee('差额：', false)
            ->assertSee('差额：¥-0.01', false)
            ->assertSee('去补回执', false)
            ->assertSee('#add-payment-receipt', false)
            ->assertSee('/admin/platform-orders?reconcile_mismatch=1', false);
    }
}
