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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'plan_batch_show_bmpa',
            'name' => '批次页BMPA测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $runId = 'BMPA_BATCH_PAGE_0001';

        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_BATCH_PAGE_BMPA_0001',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'unpaid',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'quantity' => 1,
            'payable_amount' => 10,
            'paid_amount' => 0,
            'placed_at' => now()->subMinutes(10),
            'meta' => [
                'batch_mark_paid_and_activate' => [
                    'at' => now()->toDateTimeString(),
                    'admin_id' => 1,
                    'scope' => 'filtered',
                    'mode' => 'queue',
                    'run_id' => $runId,
                    'last_result' => [
                        'run_id' => $runId,
                        'success' => 1,
                        'failed' => 2,
                        'matched' => 10,
                        'processed' => 3,
                        'top_reasons' => [
                            ['reason' => '模拟失败：订单不是待处理+未支付', 'count' => 2],
                        ],
                        'at' => now()->toDateTimeString(),
                    ],
                ],
            ],
        ]);

        $html = $this->get('/admin/platform-batches/show?type=bmpa&run_id=' . $runId)
            ->assertOk()
            ->getContent();

        $this->assertStringContainsString('BMPA（批量标记支付并生效） 批次详情', $html);
        $this->assertStringContainsString('run_id：<strong>' . $runId . '</strong>', $html);

        // 治理入口：本批次失败（run_id + bmpa_failed_only=1）
        $this->assertStringContainsString('batch_bmpa_run_id=' . $runId, $html);
        $this->assertStringContainsString('bmpa_failed_only=1', $html);

        // 治理入口：按Top原因
        $this->assertStringContainsString('bmpa_error_keyword=', $html);
        $this->assertStringContainsString(urlencode('模拟失败：订单不是待处理+未支付'), $html);

        // 治理入口：本批次可同步重试（batch_bmpa_run_id + syncable_only=1 + sync_status=unsynced）
        $this->assertStringContainsString('syncable_only=1', $html);
        $this->assertStringContainsString('sync_status=unsynced', $html);

        // 治理入口：本批次可再次尝试（可BMPA处理）
        $this->assertStringContainsString('bmpa_processable_only=1', $html);
    }
}
