import express from 'express';
import {join, dirname} from 'path';
import { fileURLToPath } from 'url';
import {readFileSync} from 'fs';

const app = express();
const __dirname = dirname(fileURLToPath(import.meta.url));

app.use(express.json());
app.use(express.static(join(__dirname, "public")));

app.post("/language", async (req, res) => {
  const requested = req.body?.lang || "fr";
  try {
    res.json(await import(requested+"/index.js"));
  } catch {
    res.json(await import("fr/index.js"));
  }
});

app.get("/message", (req, res) => {
  let data;
  try {
    data = readFileSync("/tmp/message.txt").toString();
  } catch {
    //The dev was angry before leaving the aquarium, messages service must be fixed and maybe the frontend too
    data = "F🐟I🐟S🐟H🐟";
  }
  res.json({"message": data});
});

app.get("/", (_, res) => {
  res.sendFile(join(__dirname, "public", "index.html"));
});

app.listen(8000, () => {
  console.log("FCSC Aquarium is running !");
});
