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

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

        $plan = Plan::query()->create([
            'code' => 'lead_id_create_order_plan',
            'name' => '下单页lead_id透传测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $lead = PlatformLead::query()->create([
            'name' => 'lead_id线索',
            'mobile' => '13800000005',
            'email' => 'lead5@example.com',
            'company' => 'lead_id公司',
            'source' => 'test',
            'status' => 'new',
            'plan_id' => $plan->id,
            'meta' => ['merchant_id' => $merchant->id],
        ]);

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

        $res = $this->get($url);
        $res->assertOk();

        $res->assertSee('name="lead_id"', false);
        $res->assertSee('value="' . $lead->id . '"', false);
    }
}
