import { OpenApiGeneratorV31 } from "@asteasolutions/zod-to-openapi";
import { registry } from "./registry";
import { env } from "../../config/env";
import "./security"; // Ensure security components are registered

let cachedOpenApiDoc: any = null;

export function clearOpenApiCache() {
  cachedOpenApiDoc = null;
}

export function getOpenApiDocument() {
  if (cachedOpenApiDoc) {
    return cachedOpenApiDoc;
  }

  const generator = new OpenApiGeneratorV31(registry.definitions);

  cachedOpenApiDoc = generator.generateDocument({
    openapi: "3.1.0",
    info: {
      title: env.SWAGGER_TITLE,
      version: env.SWAGGER_VERSION,
      description:
        "Production-ready OpenAPI 3.1 specification for SGMS Backend API.",
      contact: {
        name: "SGMS API Support",
        email: "support@sgms.com",
      },
    },
    servers: [
      {
        url: "/api/v1",
        description: "API v1 Base URL",
      },
    ],
    tags: [
      {
        name: "Authentication",
        description:
          "User registration, authentication, password recovery, and active session management.",
      },
      {
        name: "Tenants",
        description:
          "API endpoints for managing agency/tenant accounts and details.",
      },
      {
        name: "Companies",
        description:
          "Manage companies, clients, and branch offices within the system.",
      },
      {
        name: "Guards",
        description:
          "Guard profile onboarding, administrative details, credentials, weapons, training records, and documents.",
      },
      {
        name: "Assignments",
        description:
          "Assign guards to companies, track active assignments, and review historical deployments.",
      },
      {
        name: "Organizations",
        description:
          "Create, update, and manage organizations, including their details, branding, and settings.",
      },
      {
        name: "Departments",
        description:
          "Create, update, and manage departments, assign responsibilities, and organize workflows across the organization.",
      },
      {
        name: "Employees",
        description:
          "Create, update, and manage employee profiles, assign departments and roles, and monitor staff information efficiently.",
      },
      {
        name: "Clients",
        description:
          "Create, update, and manage client profiles, including their contact information, company details, and associated users.",
      },
      {
        name: "Services",
        description:
          "Create, update, and manage services offered by the organization, including service details, pricing, and availability.",
      },
      {
        name: "Services Documents",
        description:
          " create, update documents name for service and manage for services.",
      },

      {
        name: "Tasks",
        description:
          "Create, update, and manage tasks, assign them to employees or departments, and track their progress and completion status.",
      },

      {
        name: "Duties",
        description:
          "Schedule, log, start, and complete duty shifts. Includes self-service check-in/out for guards.",
      },
      {
        name: "System",
        description:
          "System health check and administrative diagnostic endpoints.",
      },
    ],
  });

  return cachedOpenApiDoc;
}
