<?php

namespace objects;

use classes\db;
use classes\object_property;
use traits\db_object_t;

class logs_o extends db
{
    use db_object_t;

    public object_property $module;
    public object_property $department;
    public object_property $type;
    public object_property $user_id;
    public object_property $action;
    public object_property $message;

    public function structure(): void
    {
        $this->setTable('logs');
    }

    public function objectChanged(): void
    {
        // Since the logs object is not cached, there is no need to invalidate the cache
    }

    public function add(string $module, string $department, int $type, int $user_id, string $action, string $message): void
    {
        // Add to the cache instead of the database, to make it faster
        redis->add_log($module, $department, $type, $user_id, $action, $message);
    }

    public function getObjectProperties(): void
    {
        $this->module = new object_property($this->table, $this->id, 'module', 'string', true);
        $this->department = new object_property($this->table, $this->id, 'department', 'string', false);
        $this->type = new object_property($this->table, $this->id, 'type', 'int', true);
        $this->user_id = new object_property($this->table, $this->id, 'user_id', 'int', false);
        $this->action = new object_property($this->table, $this->id, 'action', 'string', true);
        $this->message = new object_property($this->table, $this->id, 'message', 'string', false);
    }

    public function syncLogsToDatabase($display_progress = false): void
    {
        function progress($display_progress, $message): void
        {
            if ($display_progress) {
                echo "\n\033[33m$message\033[0m\n";
            }
        }

        function progress_bar($display_progress, int $current, int $total): void
        {
            // Calculate the percentage
            $percentage = ($current / $total) * 100;
            // Round the percentage
            $percentage = round($percentage);
            // Display the progress bar
            if ($display_progress) {
                echo "\r\033[33m" . str_repeat('=', $percentage) . str_repeat(' ', 100 - $percentage) . "\033[0m " . number_format($percentage, 2) . '%';
            }
            // Show the percentage, completed and total
            if ($display_progress) {
                echo "\r\033[33m" . number_format($percentage, 2) . '% (' . $current . '/' . $total . ")\033[0m";
                // If the current is equal to the total, add a new line
                if ($current === $total) {
                    echo "\n";
                }
            }
        }

        global /** @var db $db */
        $db;
        // Get all logs from the cache
        progress($display_progress, 'Counting logs in cache');
        $total = redis->get_log_count();
        progress($display_progress, 'Downloading ' . $total . ' logs from cache');
        $logs = redis->get_logs();
        progress($display_progress, 'Retrieved ' . count($logs) . ' logs from cache');
        $current = 0;
        // Insert the logs into the database
        if ($logs) {
            progress($display_progress, 'Preparing to insert logs into the database');
            $stmt = $db->prepare("INSERT INTO $this->table (module, department, type, user_id, action, message, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
            progress($display_progress, 'Inserting logs into the database');
            foreach ( $logs as $log ) {
                $current++;
                $timestamp = date('Y-m-d H:i:s', $log['timestamp']);
                $stmt->bind_param('ssiiisss', $log['module'], $log['department'], $log['type'], $log['user_id'], $log['action'], $log['message'], $timestamp, $timestamp);
                $stmt->execute();
                // Remove the log from the cache
                redis->delete('log_' . $log['id']);
                progress_bar($display_progress, $current, $total);
            }
            $stmt->close();
        }
    }
}