import { registry } from "../../infrastructure/openapi/registry";
import { z } from "zod";
import { ErrorResponseSchema } from "../../infrastructure/openapi/security";
import { inviteEmployeeSchema, acceptInvitationSchema } from "./employee.validation";

const EmployeeResponseSchema = registry.register(
  "EmployeeResponse",
  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: "/employees/invite",
  summary: "Invite an employee or manager (Admin only)",
  tags: ["Employees"],
  security: [{ bearerAuth: [] }],
  request: {
    body: {
      content: {
        "application/json": { schema: inviteEmployeeSchema.shape.body },
      },
    },
  },
  responses: {
    201: {
      description: "Invitation sent successfully",
      content: { "application/json": { schema: EmployeeResponseSchema } },
    },
    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: "User or invitation already exists",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

registry.registerPath({
  method: "post",
  path: "/employees/accept-invitation",
  summary: "Accept invitation and set password (Public)",
  tags: ["Employees"],
  request: {
    body: {
      content: {
        "application/json": { schema: acceptInvitationSchema.shape.body },
      },
    },
  },
  responses: {
    201: {
      description: "Account created successfully",
      content: { "application/json": { schema: EmployeeResponseSchema } },
    },
    400: {
      description: "Invalid or expired token",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    409: {
      description: "User already registered",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

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

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

