const fs = require("fs");
const path = require("path");

const logDir = path.join(__dirname, "..", "logs");
const logFile = path.join(logDir, "email-notifications.log");

function ensureLogDir() {
  fs.mkdirSync(logDir, { recursive: true });
}

function write(level, message, meta = {}) {
  ensureLogDir();
  const entry = {
    timestamp: new Date().toISOString(),
    level,
    message,
    ...meta,
  };
  const line = JSON.stringify(entry);
  fs.appendFileSync(logFile, `${line}\n`);

  const printer = level === "error" ? console.error : console.log;
  printer(line);
}

module.exports = {
  info(message, meta) {
    write("info", message, meta);
  },
  error(message, meta) {
    write("error", message, meta);
  },
};
