<?php

declare(strict_types=1);

namespace Tests\Unit\Services;

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

class ExportConfigServiceTest extends TestCase
{
    private ConfigurationService $configService;
    private LeadService $leadService;
    private int $adminId;
    private int $userId;
    private int $eventId;

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

    private function createLead(): int
    {
        $lead = $this->leadService->createManual([
            'first_name' => 'Export', 'last_name' => 'Lead', 'email' => 'export@test.com', 'company' => 'TestCo', 'phone' => '5550001',
        ], $this->userId, $this->eventId);
        return $lead['id'];
    }

    public function testExportLeads(): void
    {
        $this->createLead();

        $filePath = $this->leadService->exportLeads([
            'event_id' => $this->eventId,
        ], $this->userId, 'comercial_manager');

        $this->assertStringEndsWith('.xlsx', $filePath);
        $this->assertFileExists($filePath);

        unlink($filePath);
    }

    public function testExportLeadsEmpty(): void
    {
        $filePath = $this->leadService->exportLeads([
            'event_id' => $this->eventId,
        ], $this->userId, 'comercial_manager');

        $this->assertFileExists($filePath);
        unlink($filePath);
    }

    public function testExportLeadsSuperAdmin(): void
    {
        $this->createLead();
        $this->leadService->createManual([
            'first_name' => 'Admin', 'last_name' => 'Lead', 'email' => 'admin-lead@test.com',
        ], $this->adminId, $this->eventId);

        $filePath = $this->leadService->exportLeads([
            'event_id' => $this->eventId,
        ], $this->adminId, 'super_admin');

        $this->assertFileExists($filePath);
        unlink($filePath);
    }

    public function testGetAllConfigurations(): void
    {
        $configs = $this->configService->getAll();
        $this->assertIsArray($configs);
    }

    public function testUpdateConfiguration(): void
    {
        $this->configService->update('smtp_host', 'smtp.test.com', $this->adminId);

        $configs = $this->configService->getAll();
        $found = false;
        foreach ($configs as $c) {
            if ($c['key'] === 'smtp_host' && $c['value'] === 'smtp.test.com') {
                $found = true;
                break;
            }
        }

        $this->assertTrue($found);
    }

    public function testUpdateConfigurationNewKey(): void
    {
        $this->configService->update('custom_setting', 'custom_value', $this->adminId);

        $configs = $this->configService->getAll();
        $found = false;
        foreach ($configs as $c) {
            if ($c['key'] === 'custom_setting' && $c['value'] === 'custom_value') {
                $found = true;
                break;
            }
        }

        $this->assertTrue($found);
    }

    public function testConfigurationPersistence(): void
    {
        $this->configService->update('test_key', 'first_value', $this->adminId);
        $this->configService->update('test_key', 'second_value', $this->adminId);

        $configs = $this->configService->getAll();
        $found = null;
        foreach ($configs as $c) {
            if ($c['key'] === 'test_key') {
                $found = $c['value'];
                break;
            }
        }

        $this->assertEquals('second_value', $found);
    }
}
