<?php

namespace lib\Core;

class Logger
{
  protected string $logPath = APP_LOG_PATH . DIRECTORY_SEPARATOR;

  public function __construct(string $logDir = APP_LOG_PATH)
  {
    $this->logPath = rtrim($logDir, DIRECTORY_SEPARATOR);
    if (!is_dir($this->logPath)) {
      mkdir($this->logPath, APP_LOG_PERMISSION, true);
    }
  }

  public function log(string $message, string $level = APP_LOG_LEVELS['info']): void
  {
    $filename = date('Y-m-d') . '.log';
    $filepath = "{$this->logPath}/{$filename}";
    $time = date('Y-m-d H:i:s');
    $formatted = "[{$time}] [{$level}] {$message}\n";
    file_put_contents($filepath, $formatted, FILE_APPEND);
  }

  public function info(string $message): void
  {
    $this->log($message, APP_LOG_LEVELS['info']);
  }

  public function error(string $message): void
  {
    $this->log($message,  APP_LOG_LEVELS['info']);
  }

  public function warning(string $message): void
  {
    $this->log($message, APP_LOG_LEVELS['warning']);
  }

  public function debug(string $message): void
  {
    if (APP_LOG_LEVELS['debug']) {
      $this->log($message, APP_LOG_LEVELS['debug']);
    }
  }
}
