<?php

namespace App\Shared\Logging;

use App\Shared\Logging\AppLogRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: AppLogRepository::class)]
#[ORM\Table(name: 'app_log')]
#[ORM\Index(columns: ['level', 'created_at'], name: 'idx_app_log_level')]
#[ORM\Index(columns: ['created_at'], name: 'idx_app_log_created')]
class AppLog
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;

    #[ORM\Column(type: 'string', length: 16)]
    private string $level;

    #[ORM\Column(type: 'text')]
    private string $message;

    #[ORM\Column(type: 'text', nullable: true)]
    private ?string $context = null;

    #[ORM\Column(type: 'string', length: 32, nullable: true)]
    private ?string $channel = null;

    #[ORM\Column(type: 'string', length: 255, nullable: true)]
    private ?string $path = null;

    #[ORM\Column(name: 'created_at', type: 'integer')]
    private int $createdAt;

    public function __construct(string $level, string $message, ?string $context = null, ?string $channel = null, ?string $path = null)
    {
        $this->level     = $level;
        $this->message   = $message;
        $this->context   = $context;
        $this->channel   = $channel;
        $this->path      = $path;
        $this->createdAt = time();
    }

    public function toArray(): array
    {
        return [
            'id'         => $this->id,
            'level'      => $this->level,
            'message'    => $this->message,
            'context'    => $this->context,
            'channel'    => $this->channel,
            'path'       => $this->path,
            'created_at' => $this->createdAt,
        ];
    }
}
