import { FastifyInstance } from "fastify";
import fp from "fastify-plugin";
import swagger from "@fastify/swagger";
import swaggerUi from "@fastify/swagger-ui";

export default fp(async (server: FastifyInstance) => {
    await server.register(swagger, {
        mode: "dynamic",
        openapi: {
            info: {
                title: "FEDEO Backend API",
                description: "OpenAPI specification for the FEDEO backend",
                version: "1.0.0",
            },
            servers: [{ url: "/" }],
            components: {
                securitySchemes: {
                    bearerAuth: {
                        type: "http",
                        scheme: "bearer",
                        bearerFormat: "JWT"
                    }
                }
            }
        },
    });

    // @ts-ignore
    await server.register(swaggerUi, {
        routePrefix: "/docs",
    });

    // Stable raw spec path
    server.get("/openapi.json", async (_req, reply) => {
        return reply.send(server.swagger());
    });
});
