import { registry } from "../../infrastructure/openapi/registry";
import { z } from "zod";
import { ErrorResponseSchema } from "../../infrastructure/openapi/security";

const NotificationSchema = z.object({
  id: z.string(),
  userId: z.string(),
  channel: z.enum(["EMAIL", "SMS", "PUSH"]),
  type: z.string().nullable().optional(),
  title: z.string(),
  message: z.string(),
  actionUrl: z.string().nullable().optional(),
  actionLabel: z.string().nullable().optional(),
  status: z.enum(["QUEUED", "SENT", "FAILED", "READ"]),
  sentAt: z.string().datetime().nullable().optional(),
  readAt: z.string().datetime().nullable().optional(),
  createdAt: z.string().datetime(),
});

const NotificationResponseSchema = registry.register(
  "NotificationResponse",
  z.object({
    success: z.boolean().default(true),
    statusCode: z.number().default(200),
    message: z.string().optional(),
    data: NotificationSchema.optional(),
  }),
);

const NotificationListResponseSchema = registry.register(
  "NotificationListResponse",
  z.object({
    success: z.boolean().default(true),
    statusCode: z.number().default(200),
    message: z.string().optional(),
    data: z.array(NotificationSchema).optional(),
    meta: z
      .object({
        total: z.number(),
        page: z.number(),
        limit: z.number(),
      })
      .optional(),
  }),
);

const UnreadCountResponseSchema = registry.register(
  "UnreadCountResponse",
  z.object({
    success: z.boolean().default(true),
    statusCode: z.number().default(200),
    message: z.string().optional(),
    data: z.object({ count: z.number() }).optional(),
  }),
);

// ============================================================
// NOTIFICATION ROUTES
// ============================================================

registry.registerPath({
  method: "get",
  path: "/notifications",
  summary: "Get paginated list of notifications for the logged-in user",
  tags: ["Notifications"],
  security: [{ bearerAuth: [] }],
  request: {
    query: z.object({
      page: z.string().optional().describe("Page number, default 1"),
      limit: z
        .string()
        .optional()
        .describe("Items per page, default 10, max 100"),
    }),
  },
  responses: {
    200: {
      description: "Notifications fetched successfully",
      content: {
        "application/json": { schema: NotificationListResponseSchema },
      },
    },
    401: {
      description: "Unauthorized",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

registry.registerPath({
  method: "get",
  path: "/notifications/unread-count",
  summary: "Get the count of unread notifications for the logged-in user",
  tags: ["Notifications"],
  security: [{ bearerAuth: [] }],
  responses: {
    200: {
      description: "Unread count fetched successfully",
      content: {
        "application/json": { schema: UnreadCountResponseSchema },
      },
    },
    401: {
      description: "Unauthorized",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

registry.registerPath({
  method: "patch",
  path: "/notifications/{notificationId}/read",
  summary: "Mark a single notification as read",
  tags: ["Notifications"],
  security: [{ bearerAuth: [] }],
  request: {
    params: z.object({
      notificationId: z.string().describe("Notification ID"),
    }),
  },
  responses: {
    200: {
      description: "Notification marked as read",
      content: {
        "application/json": { schema: NotificationResponseSchema },
      },
    },
    401: {
      description: "Unauthorized",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    404: {
      description: "Notification not found",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

registry.registerPath({
  method: "patch",
  path: "/notifications/read-all",
  summary: "Mark all notifications as read for the logged-in user",
  tags: ["Notifications"],
  security: [{ bearerAuth: [] }],
  responses: {
    200: {
      description: "All notifications marked as read",
      content: {
        "application/json": {
          schema: registry.register(
            "MarkAllReadResponse",
            z.object({
              success: z.boolean().default(true),
              statusCode: z.number().default(200),
              message: z.string().optional(),
              data: z.object({ updated: z.number() }).optional(),
            }),
          ),
        },
      },
    },
    401: {
      description: "Unauthorized",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});
