<?php

namespace Tests\Feature;

use App\Models\Admin;
use App\Models\Merchant;
use App\Models\Plan;
use App\Models\PlatformOrder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AdminDashboardRecentPlatformOrdersFailedHintsShouldShowReasonTooLongHintWhenNoSameReasonLinkTest 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_recent_platform_orders_failed_hints_should_show_reason_too_long_hint_when_no_same_reason_link(): void
    {
        $this->loginAsPlatformAdmin();

        $merchantId = (int) Merchant::query()->value('id');
        $platformAdminId = (int) Admin::query()->where('email', 'platform.admin@demo.local')->value('id');

        $plan = Plan::query()->create([
            'code' => 'dash_failed_reason_too_long_plan',
            'name' => '仪表盘原因过长提示测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        // 使用超出 config 阈值的长度（默认 200），确保命中“原因过长”分支。
        $longReason = str_repeat('A', 220);

        PlatformOrder::query()->create([
            'merchant_id' => $merchantId,
            'plan_id' => $plan->id,
            'site_subscription_id' => null,
            'created_by_admin_id' => $platformAdminId ?: null,
            'order_no' => 'PO_DASH_SYNC_FAIL_LONG_REASON_0001',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'paid',
            'payable_amount' => 10,
            'paid_amount' => 10,
            'meta' => [
                'subscription_activation_error' => [
                    'message' => $longReason,
                ],
            ],
        ]);

        PlatformOrder::query()->create([
            'merchant_id' => $merchantId,
            'plan_id' => $plan->id,
            'site_subscription_id' => null,
            'created_by_admin_id' => $platformAdminId ?: null,
            'order_no' => 'PO_DASH_BMPA_FAIL_LONG_REASON_0001',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'unpaid',
            'payable_amount' => 10,
            'paid_amount' => 0,
            'meta' => [
                'batch_mark_paid_and_activate_error' => [
                    'message' => $longReason,
                ],
            ],
        ]);

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

        // 原因过长时不生成同原因集合链接，但应提示“原因过长”避免运营以为缺入口。
        $res->assertSee('原因过长', false);

        // 不应出现同原因集合链接（限定在失败提示块 scope 内，避免页面其它区域出现同名文案造成误判）
        // 说明：页面上其他位置可能也会出现“同原因集合”，因此这里锁定到 recent-order-sync-failed-hint 这段。
        $content = (string) $res->getContent();
        $startPos = strpos($content, 'data-role="recent-order-sync-failed-hint"');
        $this->assertNotFalse($startPos);
        $snippet = substr($content, $startPos, 800);
        $this->assertStringContainsString('原因过长', $snippet);
        $this->assertStringNotContainsString('同原因集合', $snippet);
    }
}
