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

        // 清理 seed，避免列表干扰断言。
        PlatformOrder::query()->delete();

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

        $plan = Plan::query()->create([
            'code' => 'po_index_mark_activated_reconcile_mismatch_test',
            'name' => '平台订单列表行级仅标记生效（对账不一致禁用）测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        // 已支付 + 待处理，但对账不一致（paid=10，回执=9）
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_INDEX_MARK_ACTIVATED_RECON_MISMATCH_0001',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            '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(),
            'meta' => [
                'payment_summary' => [
                    'total_amount' => 9,
                    'count' => 1,
                ],
            ],
        ]);

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

        $res->assertSee('PO_INDEX_MARK_ACTIVATED_RECON_MISMATCH_0001');

        // 行级“仅标记为已生效”按钮必须 disabled
        $res->assertSee('action="/admin/platform-orders/', false);
        $res->assertSee('/mark-activated', false);
        $res->assertSee('disabled', false);

        // 并给出最短治理入口：去补回执
        $res->assertSee('#add-payment-receipt', false);
    }
}
