<?php

namespace Tests\Feature;

use App\Models\PlatformLead;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

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

        $res = $this->get('/admin/platform-leads');
        $res->assertOk();
        $res->assertSee('name="lead_id"', false);
    }

    public function test_index_should_filter_by_lead_id(): void
    {
        $this->loginAsPlatformAdmin();

        $a = PlatformLead::query()->create([
            'name' => '线索A',
            'mobile' => '',
            'email' => '',
            'company' => '',
            'source' => 'test',
            'status' => 'new',
            'plan_id' => null,
            'meta' => ['from' => 'test'],
        ]);

        PlatformLead::query()->create([
            'name' => '线索B',
            'mobile' => '',
            'email' => '',
            'company' => '',
            'source' => 'test',
            'status' => 'new',
            'plan_id' => null,
            'meta' => ['from' => 'test'],
        ]);

        $res = $this->get('/admin/platform-leads?lead_id=' . $a->id);
        $res->assertOk();
        $res->assertSee((string) $a->id);
        $res->assertSee('线索A');
        $res->assertDontSee('线索B');
    }
}
