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

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

        // 1) BAS
        $basRunId = 'BAS_TOP_REASON_LINK_0001';
        $basReason = '模拟失败：订阅同步异常';

        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_BATCH_SHOW_TOP_REASON_BAS_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()->subMinutes(10),
            'paid_at' => now()->subMinutes(9),
            'activated_at' => now()->subMinutes(8),
            'meta' => [
                'batch_activation' => [
                    'run_id' => $basRunId,
                    'last_result' => [
                        'run_id' => $basRunId,
                        'success' => 3,
                        'failed' => 1,
                        'matched' => 10,
                        'processed' => 4,
                        'top_reasons' => [
                            ['reason' => $basReason, 'count' => 1],
                        ],
                        'at' => now()->toDateTimeString(),
                    ],
                ],
            ],
        ]);

        $basHtml = $this->get('/admin/platform-batches/show?type=bas&run_id=' . $basRunId)
            ->assertOk()
            ->getContent();

        $this->assertStringContainsString('data-role="batch-top-reason-link"', $basHtml);
        $this->assertStringContainsString('batch_activation_run_id=' . $basRunId, $basHtml);
        $this->assertStringContainsString('sync_status=failed', $basHtml);
        $this->assertStringContainsString('sync_error_keyword=', $basHtml);
        $this->assertStringContainsString(urlencode($basReason), $basHtml);

        // 2) BMPA
        $bmpaRunId = 'BMPA_TOP_REASON_LINK_0001';
        $bmpaReason = '模拟失败：订单不是待处理+未支付';

        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_BATCH_SHOW_TOP_REASON_BMPA_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()->subMinutes(10),
            'paid_at' => now()->subMinutes(9),
            'activated_at' => now()->subMinutes(8),
            'meta' => [
                'batch_mark_paid_and_activate' => [
                    'run_id' => $bmpaRunId,
                    'last_result' => [
                        'run_id' => $bmpaRunId,
                        'success' => 2,
                        'failed' => 2,
                        'matched' => 10,
                        'processed' => 4,
                        'top_reasons' => [
                            ['reason' => $bmpaReason, 'count' => 2],
                        ],
                        'at' => now()->toDateTimeString(),
                    ],
                ],
            ],
        ]);

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

        $this->assertStringContainsString('data-role="batch-top-reason-link"', $bmpaHtml);
        $this->assertStringContainsString('batch_bmpa_run_id=' . $bmpaRunId, $bmpaHtml);
        $this->assertStringContainsString('bmpa_failed_only=1', $bmpaHtml);
        $this->assertStringContainsString('bmpa_error_keyword=', $bmpaHtml);
        $this->assertStringContainsString(urlencode($bmpaReason), $bmpaHtml);
    }
}
