<?php

namespace App\Tests\Entity;

use App\Entity\AppLog;
use App\Entity\User;
use PHPUnit\Framework\TestCase;

class AppLogTest extends TestCase
{
    public function testConstructor(): void
    {
        $log = new AppLog('GET', '/admin/dashboard', 'app_admin_dashboard', 'Consultation du tableau de bord', 'secret');
        $this->assertNull($log->getId());
        $this->assertSame('GET', $log->getMethod());
        $this->assertSame('/admin/dashboard', $log->getUrl());
        $this->assertSame('app_admin_dashboard', $log->getRoute());
        $this->assertSame('Consultation du tableau de bord', $log->getAction());
        $this->assertNull($log->getUser());
        $this->assertNull($log->getIp());
        $this->assertNotEmpty($log->getHmac());
        $this->assertInstanceOf(\DateTimeImmutable::class, $log->getCreatedAt());
    }

    public function testConstructorWithUser(): void
    {
        $user = new User();
        $user->setEmail('admin@test.com');
        $user->setFirstName('Admin');
        $user->setLastName('User');
        $user->setPassword('h');

        $log = new AppLog('POST', '/admin/clients/create', 'app_admin_clients_create', 'Creation client', 'secret', $user, '192.168.1.1');
        $this->assertSame($user, $log->getUser());
        $this->assertSame('192.168.1.1', $log->getIp());
        $this->assertSame('POST', $log->getMethod());
    }

    public function testVerifyHmacValid(): void
    {
        $log = new AppLog('GET', '/admin', 'route', 'action', 'my-secret');
        $this->assertTrue($log->verifyHmac('my-secret'));
    }

    public function testVerifyHmacInvalid(): void
    {
        $log = new AppLog('GET', '/admin', 'route', 'action', 'my-secret');
        $this->assertFalse($log->verifyHmac('wrong-secret'));
    }

    public function testHmacDeterministic(): void
    {
        $log = new AppLog('GET', '/admin', 'route', 'action', 'secret');
        $hmac1 = $log->getHmac();
        $this->assertTrue($log->verifyHmac('secret'));
        $this->assertSame($hmac1, $log->getHmac());
    }

    public function testDifferentDataProducesDifferentHmac(): void
    {
        $log1 = new AppLog('GET', '/admin/a', 'route_a', 'action_a', 'secret');
        $log2 = new AppLog('GET', '/admin/b', 'route_b', 'action_b', 'secret');
        $this->assertNotSame($log1->getHmac(), $log2->getHmac());
    }
}
