<?php

namespace Tests\Feature;

use App\Models\Merchant;
use App\Models\Plan;
use App\Models\PlatformLead;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Arr;
use Tests\TestCase;

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

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

        $plan = Plan::query()->create([
            'code' => 'lead_context_form_plan',
            'name' => '下单页线索提示测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $lead = PlatformLead::query()->create([
            'name' => '线索提示',
            'mobile' => '',
            'email' => '',
            'company' => '',
            'source' => 'test',
            'status' => 'new',
            'plan_id' => $plan->id,
            'meta' => ['merchant_id' => $merchant->id],
        ]);

        $res = $this->get('/admin/platform-orders/create?' . Arr::query([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'lead_id' => $lead->id,
            'back' => '/admin/platform-leads',
        ]));

        $res->assertOk();
        $res->assertSee('来源线索：#' . $lead->id, false);

        // 入口应指向 platform-orders?lead_id=xxx（可带 back）
        $res->assertSee('/admin/platform-orders?lead_id=' . $lead->id, false);
        $res->assertSee('查看该线索关联订单', false);
    }
}
