import { registry } from "../../infrastructure/openapi/registry";
import { z } from "zod";
import { ErrorResponseSchema } from "../../infrastructure/openapi/security";

const TaskDocumentResponseSchema = registry.register(
  "TaskDocumentResponse",
  z.object({
    success: z.boolean().default(true),
    statusCode: z.number().default(200),
    message: z.string().optional(),
    data: z.any().optional(),
  }),
);

// ── CLIENT ──────────────────────────────────────────────────

registry.registerPath({
  method: "get",
  path: "/tasks/{taskId}/documents",
  summary: "Get documents uploaded for a task (Client only)",
  tags: ["Task Documents"],
  security: [{ bearerAuth: [] }],
  request: {
    params: z.object({ taskId: z.string().describe("Task ID") }),
  },
  responses: {
    200: {
      description: "Documents fetched successfully",
      content: { "application/json": { schema: TaskDocumentResponseSchema } },
    },
    400: {
      description: "Client profile not found",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    401: {
      description: "Unauthorized",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    404: {
      description: "Task not found",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

registry.registerPath({
  method: "post",
  path: "/tasks/{taskId}/documents",
  summary: "Upload documents for a task (Client only)",
  tags: ["Task Documents"],
  security: [{ bearerAuth: [] }],
  request: {
    params: z.object({ taskId: z.string().describe("Task ID") }),
    body: {
      content: {
        "multipart/form-data": {
          schema: z.object({
            documents: z
              .array(z.string().openapi({ type: "string", format: "binary" }))
              .describe("One or more files to upload"),
          }),
        },
      },
    },
  },
  responses: {
    201: {
      description: "Documents uploaded successfully",
      content: { "application/json": { schema: TaskDocumentResponseSchema } },
    },
    400: {
      description: "No files uploaded or client profile not found",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    401: {
      description: "Unauthorized",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    404: {
      description: "Task not found",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

registry.registerPath({
  method: "delete",
  path: "/tasks/{taskId}/documents/{documentId}",
  summary: "Delete a document from a task (Client only)",
  tags: ["Task Documents"],
  security: [{ bearerAuth: [] }],
  request: {
    params: z.object({
      taskId: z.string().describe("Task ID"),
      documentId: z.string().describe("Document ID"),
    }),
  },
  responses: {
    200: {
      description: "Document deleted successfully",
      content: { "application/json": { schema: TaskDocumentResponseSchema } },
    },
    400: {
      description: "Client profile not found",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    401: {
      description: "Unauthorized",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    404: {
      description: "Task or document not found",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

// ── ADMIN ───────────────────────────────────────────────────

registry.registerPath({
  method: "post",
  path: "/admin/tasks/{taskId}/documents",
  summary: "Upload documents for a task (Admin/Super Admin only)",
  tags: ["Task Documents"],
  security: [{ bearerAuth: [] }],
  request: {
    params: z.object({ taskId: z.string().describe("Task ID") }),
    body: {
      content: {
        "multipart/form-data": {
          schema: z.object({
            documents: z
              .array(z.string().openapi({ type: "string", format: "binary" }))
              .describe("One or more files to upload"),
          }),
        },
      },
    },
  },
  responses: {
    201: {
      description: "Documents uploaded successfully",
      content: { "application/json": { schema: TaskDocumentResponseSchema } },
    },
    400: {
      description:
        "No files uploaded, admin organization not found, or access denied",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    401: {
      description: "Unauthorized",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    404: {
      description: "Task not found",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

registry.registerPath({
  method: "delete",
  path: "/admin/tasks/{taskId}/documents/{documentId}",
  summary: "Delete a document from a task (Admin/Super Admin only)",
  tags: ["Task Documents"],
  security: [{ bearerAuth: [] }],
  request: {
    params: z.object({
      taskId: z.string().describe("Task ID"),
      documentId: z.string().describe("Document ID"),
    }),
  },
  responses: {
    200: {
      description: "Document deleted successfully",
      content: { "application/json": { schema: TaskDocumentResponseSchema } },
    },
    400: {
      description: "Admin organization not found or access denied",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    401: {
      description: "Unauthorized",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    404: {
      description: "Task or document not found",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});
