<?php

declare(strict_types=1);

namespace Tests\Unit\Services;

use App\Services\LeadService;
use Tests\TestCase;

class LeadServiceTest extends TestCase
{
    private LeadService $leadService;
    private int $userId;
    private int $eventId;

    protected function setUp(): void
    {
        parent::setUp();
        $this->leadService = new LeadService();
        $this->userId = $this->createTestUser('gestor@test.com', 'comercial_manager', 'active');
        $this->eventId = $this->createTestEvent('Feria Test', $this->userId);
    }

    public function testCreateManualLead(): void
    {
        $lead = $this->leadService->createManual([
            'first_name' => 'Carlos',
            'last_name' => 'González',
            'email' => 'carlos@test.com',
            'phone' => '5551234567',
            'company' => 'AgroMex',
            'position' => 'Gerente',
        ], $this->userId, $this->eventId);

        $this->assertEquals('Carlos', $lead['first_name']);
        $this->assertEquals('González', $lead['last_name']);
        $this->assertEquals('carlos@test.com', $lead['email']);
        $this->assertEquals('manual', $lead['source']);
        $this->assertEquals('nuevo', $lead['status']);
        $this->assertEquals($this->eventId, $lead['event_id']);
        $this->assertEquals($this->userId, $lead['created_by']);
    }

    public function testCreateManualWithTechnicalProfile(): void
    {
        $lead = $this->leadService->createManual([
            'first_name' => 'María',
            'last_name' => 'López',
            'email' => 'maria@test.com',
            'technical_profile' => [
                'animal_type' => 'bovino',
                'herd_size' => '200 cabezas',
                'interest_area' => 'Nutrición',
            ],
        ], $this->userId, $this->eventId);

        $this->assertArrayHasKey('technical_profile', $lead);
        $this->assertNotNull($lead['technical_profile']);
        $this->assertEquals('bovino', $lead['technical_profile']['animal_type']);
    }

    public function testCreateManualWithoutName(): void
    {
        $this->expectException(\RuntimeException::class);

        $this->leadService->createManual([
            'first_name' => '',
            'last_name' => '',
            'email' => 'test@test.com',
        ], $this->userId, $this->eventId);
    }

    public function testCreateManualWithOnlyFirstName(): void
    {
        $this->expectException(\RuntimeException::class);

        $this->leadService->createManual([
            'first_name' => 'Carlos',
            'last_name' => '',
        ], $this->userId, $this->eventId);
    }

    public function testCheckDuplicates(): void
    {
        $this->leadService->createManual([
            'first_name' => 'Duplicado',
            'last_name' => 'Test',
            'email' => 'dup@test.com',
            'phone' => '5559999999',
        ], $this->userId, $this->eventId);

        $dupes = $this->leadService->checkDuplicates('dup@test.com', null);
        $this->assertCount(1, $dupes);
        $this->assertEquals('Duplicado', $dupes[0]['first_name']);

        $dupesByPhone = $this->leadService->checkDuplicates(null, '5559999999');
        $this->assertCount(1, $dupesByPhone);
    }

    public function testCreateManualDuplicateIsMarked(): void
    {
        $first = $this->leadService->createManual([
            'first_name' => 'Original',
            'last_name' => 'Lead',
            'email' => 'original@test.com',
        ], $this->userId, $this->eventId);

        $duplicate = $this->leadService->createManual([
            'first_name' => 'Copia',
            'last_name' => 'Lead',
            'email' => 'original@test.com',
        ], $this->userId, $this->eventId);

        $this->assertEquals(1, $duplicate['is_duplicate']);
        $this->assertEquals($first['id'], $duplicate['duplicate_of']);
    }

    public function testGetLead(): void
    {
        $created = $this->leadService->createManual([
            'first_name' => 'Get',
            'last_name' => 'Lead',
            'email' => 'getlead@test.com',
        ], $this->userId, $this->eventId);

        $lead = $this->leadService->getLead($created['id']);
        $this->assertEquals('getlead@test.com', $lead['email']);
        $this->assertArrayHasKey('technical_profile', $lead);
    }

    public function testGetLeadNotFound(): void
    {
        $this->expectException(\RuntimeException::class);
        $this->leadService->getLead(9999);
    }

    public function testCreateManualWithNoDuplicates(): void
    {
        $dupes = $this->leadService->checkDuplicates(null, null);
        $this->assertCount(0, $dupes);
    }

    public function testCreateManualMinimalData(): void
    {
        $lead = $this->leadService->createManual([
            'first_name' => 'Mínimo',
            'last_name' => 'Lead',
        ], $this->userId, $this->eventId);

        $this->assertEquals('Mínimo', $lead['first_name']);
        $this->assertNull($lead['email']);
        $this->assertNull($lead['phone']);
    }
}
