<?php
declare(strict_types=1);

/**
 * Logger — writes to DB and optional file
 */
class Logger
{
    private int $jobId;
    private string $logFile;

    public function __construct(int $jobId)
    {
        $this->jobId   = $jobId;
        $this->logFile = LOGS_DIR . "/job_{$jobId}.log";
    }

    public function info(string $msg): void  { $this->write('info',  $msg); }
    public function success(string $msg): void { $this->write('success', $msg); }
    public function warning(string $msg): void { $this->write('warning', $msg); }
    public function error(string $msg): void { $this->write('error', $msg); }

    private function write(string $level, string $msg): void
    {
        Db::addLog($this->jobId, $level, $msg);
        $line = sprintf("[%s] [%s] %s\n", date('Y-m-d H:i:s'), strtoupper($level), $msg);
        file_put_contents($this->logFile, $line, FILE_APPEND | LOCK_EX);
    }
}
