// swagger.js

const fs = require("fs");
const swaggerAutogen = require("swagger-autogen")();
const outputFile = "./swagger-output.json";
const endpointsFiles = ["./app.js"]; 


const doc = {
  info: {
    title: "VSMart ERP",
    description: "VSmart Backend API Documentation with JWT Authorization",
    version: "1.0.0",
  },

  host: "localhost:8002", 
//   basePath: "/api",       // your route prefix (example: app.use('/api', routes))
  schemes: ["http"],
  securityDefinitions: {
    bearerAuth: {
      type: "apiKey",
      in: "header",
      name: "Authorization",
      description: "Enter JWT token as: Bearer <token>",
    },
  },

//   security: [
//     {
//       bearerAuth: [],
//     },
//   ],
};

swaggerAutogen(outputFile, endpointsFiles, doc).then(() => {

  console.log("Swagger documentation generated successfully!");

  // Read generated swagger JSON
  const swaggerData = JSON.parse(fs.readFileSync(outputFile, "utf8"));

  // Count total APIs
  const totalApis = swaggerData.paths
    ? Object.keys(swaggerData.paths).length
    : 0;

  console.log("Total APIs:", totalApis);

  // Add custom field
  swaggerData.info["x-totalApis"] = totalApis;

  // Save updated swagger file
  fs.writeFileSync(outputFile, JSON.stringify(swaggerData, null, 2));
//   console.log("Swagger file updated with total API count.");
});
