import { registry } from "../../infrastructure/openapi/registry";
import { z } from "zod";
import { ErrorResponseSchema } from "../../infrastructure/openapi/security";
import {
  inviteClientSchema,
  acceptClientInvitationSchema,
  updateClientSchema,
} from "./client.validation";

const ClientResponseSchema = registry.register(
  "ClientResponse",
  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: "/clients/invite",
  summary: "Invite a client (Admin only)",
  tags: ["Clients"],
  security: [{ bearerAuth: [] }],
  request: {
    body: {
      content: {
        "application/json": { schema: inviteClientSchema.shape.body },
      },
    },
  },
  responses: {
    201: {
      description: "Invitation sent successfully",
      content: { "application/json": { schema: ClientResponseSchema } },
    },
    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: "Client or invitation already exists",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

registry.registerPath({
  method: "post",
  path: "/clients/accept-invitation",
  summary: "Accept client invitation and set password (Public)",
  tags: ["Clients"],
  request: {
    body: {
      content: {
        "application/json": { schema: acceptClientInvitationSchema.shape.body },
      },
    },
  },
  responses: {
    201: {
      description: "Account created successfully",
      content: { "application/json": { schema: ClientResponseSchema } },
    },
    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: "/clients/get-all-clients",
  summary: "Get all clients of organization",
  tags: ["Clients"],
  security: [{ bearerAuth: [] }],
  responses: {
    200: {
      description: "Clients fetched successfully",
      content: { "application/json": { schema: ClientResponseSchema } },
    },
    401: {
      description: "Unauthorized",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

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

registry.registerPath({
  method: "patch",
  path: "/clients/update-client/{id}",
  summary: "Update client details",
  tags: ["Clients"],
  security: [{ bearerAuth: [] }],
  request: {
    params: z.object({ id: z.string().describe("Client ID") }),
    body: {
      content: {
        "application/json": { schema: updateClientSchema.shape.body },
      },
    },
  },
  responses: {
    200: {
      description: "Client updated successfully",
      content: { "application/json": { schema: ClientResponseSchema } },
    },
    400: {
      description: "Validation failed",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    401: {
      description: "Unauthorized",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    404: {
      description: "Client not found",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

registry.registerPath({
  method: "delete",
  path: "/clients/delete-client/{id}",
  summary: "Delete a client",
  tags: ["Clients"],
  security: [{ bearerAuth: [] }],
  request: { params: z.object({ id: z.string().describe("Client ID") }) },
  responses: {
    200: {
      description: "Client deleted successfully",
      content: { "application/json": { schema: ClientResponseSchema } },
    },
    401: {
      description: "Unauthorized",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    404: {
      description: "Client not found",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});
