<?php

declare(strict_types=1);

namespace Tests\Unit\Services;

use App\Services\AuditService;
use App\Services\NotificationService;
use App\Models\Database;
use Tests\TestCase;

class AuditNotificationServiceTest extends TestCase
{
    private AuditService $auditService;
    private NotificationService $notificationService;
    private int $userId;

    protected function setUp(): void
    {
        parent::setUp();
        $this->auditService = new AuditService();
        $this->notificationService = new NotificationService();
        $this->userId = $this->createTestUser('notif@test.com', 'comercial_manager', 'active');
    }

    public function testAuditLog(): void
    {
        $this->auditService->log($this->userId, 'create', 'lead', 1, null, ['first_name' => 'Test'], '127.0.0.1');

        $logs = $this->auditService->getLogs();
        $this->assertEquals(1, $logs['total']);
        $this->assertEquals('create', $logs['items'][0]['action']);
        $this->assertEquals('lead', $logs['items'][0]['entity_type']);
    }

    public function testAuditLogWithFilters(): void
    {
        $this->auditService->log($this->userId, 'create', 'lead', 1, null, null, '127.0.0.1');
        $this->auditService->log($this->userId, 'update', 'task', 2, null, null, '127.0.0.1');

        $logs = $this->auditService->getLogs(['action' => 'update']);
        $this->assertEquals(1, $logs['total']);

        $logsByEntity = $this->auditService->getLogs(['entity_type' => 'lead']);
        $this->assertEquals(1, $logsByEntity['total']);
    }

    public function testCreateNotification(): void
    {
        $notif = $this->notificationService->createNotification(
            $this->userId, 'tarea_proxima', 'Test', 'Mensaje de prueba', 'task', 1
        );

        $this->assertEquals('tarea_proxima', $notif['type']);
        $this->assertEquals('Test', $notif['title']);
        $this->assertEquals(0, $notif['is_read']);
    }

    public function testGetUserNotifications(): void
    {
        $this->notificationService->createNotification($this->userId, 'actividad', 'N1', 'Msg', null, null);
        $this->notificationService->createNotification($this->userId, 'actividad', 'N2', 'Msg', null, null);

        $result = $this->notificationService->getUserNotifications($this->userId);
        $this->assertEquals(2, $result['total']);
        $this->assertEquals(2, $result['unread']);
    }

    public function testMarkAsRead(): void
    {
        $notif = $this->notificationService->createNotification($this->userId, 'actividad', 'Leer', 'Msg', null, null);

        $this->notificationService->markAsRead($notif['id']);

        $result = $this->notificationService->getUserNotifications($this->userId);
        $this->assertEquals(0, $result['unread']);
    }

    public function testMarkAllAsRead(): void
    {
        $this->notificationService->createNotification($this->userId, 'actividad', 'A', 'Msg', null, null);
        $this->notificationService->createNotification($this->userId, 'actividad', 'B', 'Msg', null, null);

        $this->notificationService->markAllAsRead($this->userId);

        $result = $this->notificationService->getUserNotifications($this->userId);
        $this->assertEquals(0, $result['unread']);
    }

    public function testAuditLogMultipleActions(): void
    {
        $actions = ['create', 'update', 'delete', 'login', 'export'];
        foreach ($actions as $i => $action) {
            $this->auditService->log($this->userId, $action, 'test', $i, null, null, '127.0.0.1');
        }

        $logs = $this->auditService->getLogs();
        $this->assertEquals(5, $logs['total']);
    }

    public function testNotificationWithReference(): void
    {
        $notif = $this->notificationService->createNotification(
            $this->userId, 'nuevo_lead', 'Nuevo lead', 'Descripción', 'lead', 42
        );

        $this->assertEquals('lead', $notif['reference_type']);
        $this->assertEquals(42, $notif['reference_id']);
    }
}
