import { existsSync } from "fs";
import { access, mkdir, writeFile, rm } from "fs/promises";
import path from "path";
import { appVersion } from "./version";

export const dataDirectory = path.join(process.cwd(), "data");
export const uploadDirectory = path.join(process.cwd(), "public", "uploads", "images");

export function adminConfigurationStatus() {
  return process.env.ADMIN_EMAIL && process.env.ADMIN_PASSWORD && process.env.ADMIN_SESSION_SECRET ? "configured" : "missing";
}

export function isDockerEnvironment() {
  return process.env.DOCKER_ENV === "true" || process.env.CONTAINER === "true" || existsSync("/.dockerenv");
}

export async function ensureRuntimeDirectories() {
  await mkdir(dataDirectory, { recursive: true });
  await mkdir(uploadDirectory, { recursive: true });
}

export async function checkStorage() {
  await ensureRuntimeDirectories();
  const probe = path.join(dataDirectory, `.storage-check-${Date.now()}`);
  await writeFile(probe, "ok", "utf8");
  await rm(probe, { force: true });
  await access(uploadDirectory);
  return "ok";
}

export function olympusStatus() {
  return process.env.OLYMPUS_INTAKE_API_URL && process.env.OLYMPUS_INTAKE_API_TOKEN ? "configured" : "not_configured";
}

export function smtpStatus() {
  return "prepared";
}

export { appVersion };
