/**
 * Swagger/OpenAPI Configuration
 * API documentation setup
 * 
 * @module config/swagger
 */

const swaggerJsdoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');

const options = {
  swaggerDefinition: {
    info: {
      title: 'Misayan SaaS API',
      version: '1.0.0',
      description: 'Multi-tenant WhatsApp Business SaaS Platform API Documentation',
      contact: {
        name: 'Misayan Support',
        email: 'support@saas.misayan.cloud',
      },
      license: {
        name: 'Commercial',
      },
    },
    host: process.env.APP_URL ? process.env.APP_URL.replace(/^https?:\/\//, '') : 'localhost:3000',
    basePath: '/',
    schemes: ['http', 'https'],
    securityDefinitions: {
      bearerAuth: {
        type: 'apiKey',
        name: 'Authorization',
        in: 'header',
        description: 'JWT Authorization header using the Bearer scheme. Example: "Bearer {token}"',
      },
      tenantId: {
        type: 'apiKey',
        in: 'header',
        name: 'X-Tenant-ID',
      },
    },
    security: [
      {
        bearerAuth: [],
      },
    ],
  },
  apis: ['./routes/*.js', './controllers/*.js'],
};

const specs = swaggerJsdoc(options);

function setupSwagger(app) {
  app.use(
    '/api-docs',
    swaggerUi.serve,
    swaggerUi.setup(specs, {
      explorer: true,
      customCss: '.swagger-ui .topbar { display: none }',
      customSiteTitle: 'Misayan SaaS API Documentation',
    })
  );
}

module.exports = { setupSwagger, specs };
