import { registry } from "../../infrastructure/openapi/registry";
import { z } from "zod";
import { ErrorResponseSchema } from "../../infrastructure/openapi/security";
import {
  createOrganizationSchema,
  updateOrganizationSchema,
} from "./organization.validation";

const OrganizationResponseSchema = registry.register(
  "OrganizationResponse",
  z.object({
    success: z.boolean().default(true),
    statusCode: z.number().default(201),
    message: z.string(),
    data: z.any().optional(),
  })
);

registry.registerPath({
  method: "post",
  path: "/organizations/create-organization",
  summary: "Create a new organization",
  tags: ["Organizations"],
  security: [{ bearerAuth: [] }],

  request: {
    body: {
      content: {
        "multipart/form-data": {
          schema: createOrganizationSchema.shape.body,
        },
      },
    },
  },

  responses: {
    201: {
      description: "Organization created successfully",
      content: {
        "application/json": {
          schema: OrganizationResponseSchema,
        },
      },
    },
    400: {
      description: "Validation failed",
      content: {
        "application/json": {
          schema: ErrorResponseSchema,
        },
      },
    },
    401: {
      description: "Unauthorized",
      content: {
        "application/json": {
          schema: ErrorResponseSchema,
        },
      },
    },
  },
});

registry.registerPath({
  method: "put",
  path: "/organizations/update-organization",
  summary: "Update organization",
  tags: ["Organizations"],
  security: [{ bearerAuth: [] }],

  request: {
    body: {
      content: {
        "multipart/form-data": {
          schema: updateOrganizationSchema.shape.body,
        },
      },
    },
  },

  responses: {
    200: {
      description: "Organization updated successfully",
      content: {
        "application/json": {
          schema: OrganizationResponseSchema,
        },
      },
    },
    400: {
      description: "Validation failed",
      content: {
        "application/json": {
          schema: ErrorResponseSchema,
        },
      },
    },
    401: {
      description: "Unauthorized",
      content: {
        "application/json": {
          schema: ErrorResponseSchema,
        },
      },
    },
    404: {
      description: "Organization not found",
      content: {
        "application/json": {
          schema: ErrorResponseSchema,
        },
      },
    },
  },
});

registry.registerPath({
  method: "get",
  path: "/organizations/get-organization",
  summary: "Get logged-in user's organization",
  tags: ["Organizations"],
  security: [{ bearerAuth: [] }],

  responses: {
    200: {
      description: "Organization fetched successfully",
      content: {
        "application/json": {
          schema: OrganizationResponseSchema,
        },
      },
    },
    401: {
      description: "Unauthorized",
      content: {
        "application/json": {
          schema: ErrorResponseSchema,
        },
      },
    },
    404: {
      description: "Organization not found",
      content: {
        "application/json": {
          schema: ErrorResponseSchema,
        },
      },
    },
  },
});

registry.registerPath({
  method: "get",
  path: "/organizations/get-all-organizations",
  summary: "Get all organizations (Super Admin only)",
  tags: ["Organizations"],
  security: [{ bearerAuth: [] }],

  responses: {
    200: {
      description: "All organizations fetched successfully",
      content: {
        "application/json": {
          schema: OrganizationResponseSchema,
        },
      },
    },
    401: {
      description: "Unauthorized",
      content: {
        "application/json": {
          schema: ErrorResponseSchema,
        },
      },
    },
    403: {
      description: "Forbidden - only super admin allowed",
      content: {
        "application/json": {
          schema: ErrorResponseSchema,
        },
      },
    },
  },
});

registry.registerPath({
  method: "delete",
  path: "/organizations/{organizationId}",
  summary: "Delete an organization (Super Admin only)",
  tags: ["Organizations"],
  security: [{ bearerAuth: [] }],

  request: {
    params: z.object({
      organizationId: z.string().describe("Organization ID to delete"),
    }),
  },

  responses: {
    200: {
      description: "Organization deleted successfully",
      content: {
        "application/json": {
          schema: OrganizationResponseSchema,
        },
      },
    },
    401: {
      description: "Unauthorized",
      content: {
        "application/json": {
          schema: ErrorResponseSchema,
        },
      },
    },
    403: {
      description: "Forbidden - only super admin allowed",
      content: {
        "application/json": {
          schema: ErrorResponseSchema,
        },
      },
    },
    404: {
      description: "Organization not found",
      content: {
        "application/json": {
          schema: ErrorResponseSchema,
        },
      },
    },
  },
});
