import Fastify from "fastify";
import cors from "@fastify/cors";
import jwt from "@fastify/jwt";
import multipart from "@fastify/multipart";
import { env } from "./core/env.js";
import { accessSecret } from "./core/config.js";
import { registerHealthRoutes } from "./core/routes/health.js";
import { registerAuthRoutes } from "./core/routes/auth.js";
import { registerDeviceRoutes } from "./core/routes/devices.js";
import { registerMaintenanceRoutes } from "./core/routes/maintenance.js";
import { privateFamilyProvider } from "./modules/privatefamily/family-provider.js";
import { registerFamilyRoutes } from "./modules/privatefamily/routes/families.js";
import { registerInvitationRoutes } from "./modules/privatefamily/routes/invitations.js";
import { registerSocialRoutes } from "./modules/privatefamily/routes/social.js";
import { registerAlbumRoutes } from "./modules/privatefamily/routes/albums.js";
import { registerMediaRoutes } from "./modules/privatefamily/routes/media.js";
import { LocalStorageConnector } from "./modules/privatefamily/storage/LocalStorageConnector.js";
export function buildApp() {
    const app = Fastify({
        logger: {
            level: env.LOG_LEVEL,
            transport: env.NODE_ENV === "development"
                ? { target: "pino-pretty", options: { translateTime: "HH:MM:ss" } }
                : undefined,
        },
        trustProxy: true,
        bodyLimit: 2 * 1024 * 1024,
    });
    void app.register(cors, {
        origin: true,
        credentials: true,
    });
    void app.register(jwt, {
        secret: accessSecret(),
        sign: { expiresIn: env.ACCESS_TOKEN_TTL },
    });
    // Upload direct multipart — photos/vidéos pleine résolution, limite 1 Go.
    void app.register(multipart, {
        limits: { fileSize: 1024 * 1024 * 1024 },
    });
    void app.register(registerHealthRoutes);
    void app.register(registerAuthRoutes, { familyProvider: privateFamilyProvider });
    void app.register(registerFamilyRoutes);
    void app.register(registerInvitationRoutes, { familyProvider: privateFamilyProvider });
    void app.register(registerMediaRoutes, {
        storage: new LocalStorageConnector({ dataDir: env.DATA_DIR }),
    });
    void app.register(registerSocialRoutes);
    void app.register(registerAlbumRoutes);
    void app.register(registerDeviceRoutes);
    void app.register(registerMaintenanceRoutes);
    app.setErrorHandler((error, request, reply) => {
        request.log.error({ err: error }, "unhandled error");
        const err = error;
        const statusCode = err.statusCode ?? 500;
        reply.status(statusCode).send({
            error: {
                code: err.code ?? "internal_error",
                message: statusCode >= 500 ? "Erreur interne" : err.message,
            },
        });
    });
    return app;
}
//# sourceMappingURL=app.js.map