import path from "path";
import { appendFile, mkdir } from "fs/promises";

export class Logger{

    static log(message){
        console.log(message)
    }

    static async error(message, error) {
        try {
            const currentDateTime = new Date();
            const date = currentDateTime.toLocaleDateString("fr-FR", {
                day: "2-digit",
                month: "2-digit",
                year: "2-digit"
            }).replace(/\//g, "-");

            const time = currentDateTime.toLocaleTimeString("fr-FR", {
                hour: "2-digit",
                minute: "2-digit",
                second: "2-digit",
                hour12: false
            });

            const logsDir = path.resolve("./logs");

            const fullPath = path.join(logsDir, `${date}.error.log`);

            await mkdir(logsDir, { recursive: true });

            await appendFile(fullPath, `${time} - ${message} -> ${error.message} \n`);
            console.error(error.stackTrace);
            console.error(`An error occured. The incident has been logged in ${fullPath}`)
        } catch (err) {
            console.error("Error writing log:", err);
        }
    }
}