<?php
require_once(dirname(__FILE__) . "/../../php/cache.php");
require_once(dirname(__FILE__) . "/../../php/utility/json.php");
require_once(dirname(__FILE__) . "/../../php/utility/fileutil.php");

eval(FileUtil::getPluginConf('log_history'));

header('Content-Type: application/json');

if (!isset($LogTab_array) || !is_array($LogTab_array)) {
    http_response_code(204);
    exit;
}

class LogHandler
{
    public $hash = 'log_history.dat';
    public $logs = [];
    public $modified = false;
    public $max_entries;
    public $log_count;
    protected $cache;

    static public function load()
    {
        global $LogTab_array;

        $cache = new rCache();
        $handler = new LogHandler($cache);

        if ($cache->get($handler)) {
            $handler->cache = $cache;
        } else {
            $handler->logs = [];
        }

        $handler->max_entries = max(1, intval($LogTab_array['max_entries']));
        $handler->log_count   = max(1, intval($LogTab_array['log_count']));

        return $handler;
    }

    public function __construct(rCache $cache)
    {
        $this->cache = $cache;
    }

    public function saveLog($message, $status, $timestamp = null)
    {
        if (empty($message)) {
            return ['status' => 'error', 'message' => 'No message provided'];
        }

        $this->logs[] = [
            'message' => $message,
            'status' => $status,
            'timestamp' => $timestamp ? intval($timestamp) : time()
        ];

        if (count($this->logs) > $this->max_entries) {
            $this->logs = array_slice($this->logs, -$this->max_entries);
        }

        $this->cache->set($this);

        return ['status' => 'success', 'message' => 'Log saved'];
    }

    public function getLatestLogs(int $count = null): array
    {
        $count = $count !== null ? max(1, $count) : $this->log_count;
        return array_values(array_filter(
            array_slice($this->logs, -$count),
            fn($l) => is_array($l) && isset($l['message'], $l['status'])
        ));
    }

    static public function handleRequest()
    {
        $handler = self::load();

        if (isset($_GET['clear'])) {
            $handler->logs = [];
            $handler->cache->set($handler);
            echo JSON::safeEncode(['status' => 'cleared']);
            return;
        }

        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $msg  = trim($_POST['message'] ?? '');
            $st   = trim($_POST['status'] ?? '');
            $ts   = $_POST['timestamp'] ?? null;
            $resp = $handler->saveLog($msg, $st, $ts);
        } else {
            global $LogTab_array;
            $resp = [
                'logs' => $handler->getLatestLogs(),
                'load_style' => $LogTab_array['load_style'] ?? 'noty'
            ];
        }

        echo JSON::safeEncode($resp);
    }
}

LogHandler::handleRequest();
