// BookShelf — Fastify Backend Server
import Fastify from "fastify";
import cors from "@fastify/cors";
import fjwt from "@fastify/jwt";
import { initDb, closeDb } from "./db/client.js";
import { authRoutes } from "./routes/auth.js";
import { usersRoutes } from "./routes/users.js";
import { booksRoutes } from "./routes/books.js";
import { reading_statusRoutes } from "./routes/reading_status.js";
import { reading_progressRoutes } from "./routes/reading_progress.js";
import { notesRoutes } from "./routes/notes.js";
import { reviewsRoutes } from "./routes/reviews.js";
import { statsRoutes } from "./routes/stats.js";
import { itemsRoutes } from "./routes/items.js";
import { tagsRoutes } from "./routes/tags.js";
import { note_tagsRoutes } from "./routes/note_tags.js";

const JWT_SECRET = process.env.JWT_SECRET || "change-me-in-production-50a93cd7";

export async function buildApp() {
  const app = Fastify({
    logger: {
      level: process.env.LOG_LEVEL || "info",
      transport: process.env.NODE_ENV !== "production"
        ? { target: "pino-pretty", options: { colorize: true } }
        : undefined,
    },
  });

  // Init database
  await initDb();

  // Plugins
  await app.register(cors, {
    origin: process.env.CORS_ORIGIN || "*",
    methods: ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"],
  });

  await app.register(fjwt, { secret: JWT_SECRET });

  // Routes
  await app.register(authRoutes, { prefix: "/api/auth" });
  await app.register(usersRoutes, { prefix: "/api/users" });
  await app.register(booksRoutes, { prefix: "/api/books" });
  await app.register(reading_statusRoutes, { prefix: "/api/reading_status" });
  await app.register(reading_progressRoutes, { prefix: "/api/reading_progress" });
  await app.register(notesRoutes, { prefix: "/api/notes" });
  await app.register(reviewsRoutes, { prefix: "/api/reviews" });
  await app.register(statsRoutes, { prefix: "/api/stats" });
  await app.register(itemsRoutes, { prefix: "/api/items" });
  await app.register(tagsRoutes, { prefix: "/api/tags" });
  await app.register(note_tagsRoutes, { prefix: "/api/note_tags" });

  // Health check
  app.get("/api/health", async () => ({ status: "ok", timestamp: new Date().toISOString() }));

  // Graceful shutdown
  app.addHook("onClose", async () => {
    closeDb();
  });

  return app;
}

// Start server if called directly
const port = parseInt(process.env.PORT || "3001", 10);
const host = process.env.HOST || "0.0.0.0";

async function main() {
  const app = await buildApp();
  try {
    await app.listen({ port, host });
  } catch (err) {
    app.log.error(err);
    process.exit(1);
  }
}

main();
