const fastify = require("fastify")({
  logger: { level: "error" },
});

const path = require("path");
const jwt = require("jsonwebtoken");
const cron = require("node-cron");
const { initHeadless } = require("./dist/shared/headless");
const { initDatabase } = require("./dist/shared/database");
const { loadExtensions } = require("./dist/shared/extensions");
const {refreshTrendingAnime, refreshTopAiringAnime} = require("./dist/api/anime/anime.service");
const {refreshPopularBooks, refreshTrendingBooks} = require("./dist/api/books/books.service");
const { ensureConfigFile } = require("./dist/shared/config");
const dotenv = require("dotenv");

dotenv.config();

const viewsRoutes = require("./dist/views/views.routes");
const animeRoutes = require("./dist/api/anime/anime.routes");
const booksRoutes = require("./dist/api/books/books.routes");
const proxyRoutes = require("./dist/api/proxy/proxy.routes");
const extensionsRoutes = require("./dist/api/extensions/extensions.routes");
const galleryRoutes = require("./dist/api/gallery/gallery.routes");
const userRoutes = require("./dist/api/user/user.routes");
const listRoutes = require("./dist/api/list/list.routes");
const anilistRoute = require("./dist/api/anilist/anilist");
const localRoutes = require("./dist/api/local/local.routes");
const configRoutes = require("./dist/api/config/config.routes");

fastify.addHook("preHandler", async (request) => {
  const auth = request.headers.authorization;
  if (!auth) return;

  try {
    const token = auth.replace("Bearer ", "");
    request.user = jwt.verify(token, process.env.JWT_SECRET);
  } catch (e) {
    return reply.code(401).send({ error: "Invalid token" });
  }
});

fastify.register(require("@fastify/static"), {
  root: path.join(__dirname, "public"),
  prefix: "/public/",
  decorateReply: false,
});

fastify.register(require("@fastify/static"), {
  root: path.join(__dirname, "views"),
  prefix: "/views/",
  decorateReply: false,
});

fastify.register(require("@fastify/static"), {
  root: path.join(__dirname, "src", "scripts"),
  prefix: "/src/scripts/",
  decorateReply: false,
});

fastify.register(viewsRoutes);
fastify.register(animeRoutes, { prefix: "/api" });
fastify.register(booksRoutes, { prefix: "/api" });
fastify.register(proxyRoutes, { prefix: "/api" });
fastify.register(extensionsRoutes, { prefix: "/api" });
fastify.register(galleryRoutes, { prefix: "/api" });
fastify.register(userRoutes, { prefix: "/api" });
fastify.register(anilistRoute, { prefix: "/api" });
fastify.register(listRoutes, { prefix: "/api" });
fastify.register(localRoutes, { prefix: "/api" });
fastify.register(configRoutes, { prefix: "/api" });

const sleep = ms => new Promise(r => setTimeout(r, ms));

const start = async () => {
  try {
    ensureConfigFile()
    initDatabase("anilist");
    initDatabase("favorites");
    initDatabase("cache");
    initDatabase("userdata");
    initDatabase("local_library");

    const refreshAll = async () => {
      await refreshTrendingAnime();
      await sleep(300);
      await refreshTopAiringAnime();
      await sleep(300);
      await refreshTrendingBooks();
      await sleep(300);
      await refreshPopularBooks();
    };

    cron.schedule("*/30 * * * *", async () => {
      try {
        await refreshAll();
        console.log("cache refreshed");
      } catch (e) {
        console.error("refresh failed", e);
      }
    });

    await loadExtensions();
    await initHeadless();
    await refreshAll();

    await fastify.listen({ port: 54322, host: "0.0.0.0" });
    console.log(`Server running at http://localhost:54322`);
  } catch (err) {
    fastify.log.error(err);
    process.exit(1);
  }
};

start();
