<?php

namespace App\Tests\Entity;

use App\Entity\Account;
use App\Entity\AuditLog;
use PHPUnit\Framework\TestCase;

class AuditLogTest extends TestCase
{
    public function testConstructorAndGetters()
    {
        $account = new Account();
        $account->setEmail('test@example.com'); // Real account with email set
        $type = 'Login';
        $message = 'User logged in';
        $path = '/login';
        $userAgent = 'TestBrowser';
        $_ENV['APP_SECRET'] = 'test_secret'; // Required for generateSignature

        $log = new AuditLog($account, $type, $message, $path, $userAgent);
        
        $this->assertSame($account, $log->getAccount());
        $this->assertEquals($type, $log->getType());
        $this->assertEquals($message, $log->getMessage());
        $this->assertEquals($path, $log->getPath());
        $this->assertEquals($userAgent, $log->getUserAgent());
        $this->assertNotNull($log->getActionAt());
        $this->assertNotNull($log->getHashCode());
        
        // Assertions for hashCode - reconstruct it for verification
        // Account::getEmail() is called inside generateSignature()
        $expectedData = sprintf(
            '%s|%s|%s|%s|%s|%s',
            $account->getEmail(),
            $type,
            $message,
            $path,
            $log->getActionAt()->format('Y-m-d H:i:s'),
            $_ENV['APP_SECRET'] ?? 'default_secret'
        );
        $expectedHash = hash('sha256', $expectedData);
        $this->assertEquals($expectedHash, $log->getHashCode());

        // Reset APP_SECRET
        unset($_ENV['APP_SECRET']);
    }
}
