<?php
declare(strict_types=1);

namespace FreshCloud\Mail\Log;

use Ramsey\Uuid\Uuid;

final readonly class LogEntry
{
    public function __construct(
        public string $id,
        public \DateTimeImmutable $timestamp,
        public LogType $level,
        public string $message,
        public array $context,
        public ?string $user_id,
        public ?string $account_id,
        public ?string $action,
        public string $source
    ) {
    }

    public static function create(
        LogType $level,
        string $message,
        array $context,
        ?string $user_id = null,
        ?string $account_id = null,
        ?string $action = null,
        ?string $source = null,
        ?string $id = null,
        ?\DateTimeImmutable $timestamp = null
    ): self {
        return new self(
            id: $id ?? Uuid::uuid4()->toString(),
            timestamp: $timestamp ?? new \DateTimeImmutable('now', new \DateTimeZone('UTC')),
            level: $level,
            message: mb_substr($message, 0, 8192),
            context: $context,
            user_id: $user_id,
            account_id: $account_id,
            action: $action,
            source: $source ?? 'unknown'
        );
    }
}