import { registry } from "../../infrastructure/openapi/registry";
import { z } from "zod";
import { ErrorResponseSchema } from "../../infrastructure/openapi/security";
import { createDepartmentSchema, updateDepartmentSchema } from "./department.validation";

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

registry.registerPath({
  method: "post",
  path: "/departments/create-department",
  summary: "Create a department (Admin only)",
  tags: ["Departments"],
  security: [{ bearerAuth: [] }],
  request: {
    body: {
      content: {
        "application/json": { schema: createDepartmentSchema.shape.body },
      },
    },
  },
  responses: {
    201: {
      description: "Department created successfully",
      content: { "application/json": { schema: DepartmentResponseSchema } },
    },
    400: {
      description: "Validation failed",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    401: {
      description: "Unauthorized",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    403: {
      description: "Forbidden",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    409: {
      description: "Department already exists",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

registry.registerPath({
  method: "get",
  path: "/departments/get-all-departments",
  summary: "Get all departments of organization",
  tags: ["Departments"],
  security: [{ bearerAuth: [] }],
  responses: {
    200: {
      description: "Departments fetched successfully",
      content: { "application/json": { schema: DepartmentResponseSchema } },
    },
    401: {
      description: "Unauthorized",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

registry.registerPath({
  method: "get",
  path: "/departments/get-department/{id}",
  summary: "Get a department by ID",
  tags: ["Departments"],
  security: [{ bearerAuth: [] }],
  request: { params: z.object({ id: z.string().describe("Department ID") }) },
  responses: {
    200: {
      description: "Department fetched successfully",
      content: { "application/json": { schema: DepartmentResponseSchema } },
    },
    401: {
      description: "Unauthorized",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    404: {
      description: "Department not found",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

registry.registerPath({
  method: "put",
  path: "/departments/update-department/{id}",
  summary: "Update a department (Admin only)",
  tags: ["Departments"],
  security: [{ bearerAuth: [] }],
  request: {
    params: z.object({ id: z.string().describe("Department ID") }),
    body: {
      content: {
        "application/json": { schema: updateDepartmentSchema.shape.body },
      },
    },
  },
  responses: {
    200: {
      description: "Department updated successfully",
      content: { "application/json": { schema: DepartmentResponseSchema } },
    },
    400: {
      description: "Validation failed",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    401: {
      description: "Unauthorized",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    403: {
      description: "Forbidden",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    404: {
      description: "Department not found",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    409: {
      description: "Department name already exists",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

registry.registerPath({
  method: "delete",
  path: "/departments/delete-department/{id}",
  summary: "Delete a department (Admin only)",
  tags: ["Departments"],
  security: [{ bearerAuth: [] }],
  request: { params: z.object({ id: z.string().describe("Department ID") }) },
  responses: {
    200: {
      description: "Department deleted successfully",
      content: { "application/json": { schema: DepartmentResponseSchema } },
    },
    401: {
      description: "Unauthorized",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    403: {
      description: "Forbidden",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    404: {
      description: "Department not found",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

