import { registry } from "../../infrastructure/openapi/registry";
import {
  registerSchema,
  loginSchema,
  refreshSchema,
  logoutSchema,
  forgotPasswordSchema,
  verifyOtpSchema,
  resetPasswordSchema,
  changePasswordSchema,
} from "./auth.validation";
import { z } from "zod";
import { ErrorResponseSchema } from "../../infrastructure/openapi/security";

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

registry.registerPath({
  method: "post",
  path: "/auth/register",
  summary: "Register a new user account",
  tags: ["Authentication"],
  request: {
    body: {
      content: {
        "application/json": {
          schema: registerSchema.shape.body,
        },
      },
    },
  },
  responses: {
    201: {
      description: "User registered successfully",
      content: { "application/json": { schema: AuthResponseSchema } },
    },
    400: {
      description: "Validation failed",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

registry.registerPath({
  method: "post",
  path: "/auth/login",
  summary: "Authenticate user and obtain access and refresh tokens",
  tags: ["Authentication"],
  request: {
    body: {
      content: {
        "application/json": {
          schema: loginSchema.shape.body,
        },
      },
    },
  },
  responses: {
    200: {
      description: "Login successful",
      content: { "application/json": { schema: AuthResponseSchema } },
    },
    400: {
      description: "Invalid email or password",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

registry.registerPath({
  method: "post",
  path: "/auth/logout",
  summary: "Logout user and invalidate refresh token",
  tags: ["Authentication"],
  request: {
    body: {
      content: {
        "application/json": {
          schema: logoutSchema.shape.body,
        },
      },
    },
  },
  responses: {
    200: {
      description: "Logged out successfully",
      content: { "application/json": { schema: AuthResponseSchema } },
    },
    400: {
      description: "Validation failed",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

registry.registerPath({
  method: "post",
  path: "/auth/refresh",
  summary: "Refresh access token using refresh token",
  tags: ["Authentication"],
  request: {
    body: {
      content: {
        "application/json": {
          schema: refreshSchema.shape.body,
        },
      },
    },
  },
  responses: {
    200: {
      description: "Tokens refreshed successfully",
      content: { "application/json": { schema: AuthResponseSchema } },
    },
    401: {
      description: "Invalid or expired refresh token",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

registry.registerPath({
  method: "get",
  path: "/auth/me",
  summary: "Get currently authenticated user profile",
  tags: ["Authentication"],
  security: [{ bearerAuth: [] }],
  responses: {
    200: {
      description: "User profile retrieved successfully",
      content: { "application/json": { schema: AuthResponseSchema } },
    },
    401: {
      description: "Unauthorized or invalid access token",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

registry.registerPath({
  method: "post",
  path: "/auth/forgot-password",
  summary: "Request password reset OTP via email",
  tags: ["Authentication"],
  request: {
    body: {
      content: {
        "application/json": {
          schema: forgotPasswordSchema.shape.body,
        },
      },
    },
  },
  responses: {
    200: {
      description: "Reset instructions sent if email exists",
      content: { "application/json": { schema: AuthResponseSchema } },
    },
    400: {
      description: "Validation failed",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

registry.registerPath({
  method: "post",
  path: "/auth/verify-otp",
  summary: "Verify verification or password reset OTP",
  tags: ["Authentication"],
  request: {
    body: {
      content: {
        "application/json": {
          schema: verifyOtpSchema.shape.body,
        },
      },
    },
  },
  responses: {
    200: {
      description: "OTP verified successfully",
      content: { "application/json": { schema: AuthResponseSchema } },
    },
    400: {
      description: "Invalid or expired OTP",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

registry.registerPath({
  method: "post",
  path: "/auth/reset-password",
  summary: "Reset password using verified OTP and new password",
  tags: ["Authentication"],
  request: {
    body: {
      content: {
        "application/json": {
          schema: resetPasswordSchema.shape.body,
        },
      },
    },
  },
  responses: {
    200: {
      description: "Password reset successfully",
      content: { "application/json": { schema: AuthResponseSchema } },
    },
    400: {
      description: "Validation failed or invalid OTP",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

registry.registerPath({
  method: "post",
  path: "/auth/change-password",
  summary: "Change password for currently authenticated user",
  tags: ["Authentication"],
  security: [{ bearerAuth: [] }],
  request: {
    body: {
      content: {
        "application/json": {
          schema: changePasswordSchema.shape.body,
        },
      },
    },
  },
  responses: {
    200: {
      description: "Password changed successfully",
      content: { "application/json": { schema: AuthResponseSchema } },
    },
    400: {
      description: "Incorrect current password or invalid new password",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    401: {
      description: "Unauthorized",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

registry.registerPath({
  method: "get",
  path: "/auth/sessions",
  summary: "List active login sessions for current user",
  tags: ["Authentication"],
  security: [{ bearerAuth: [] }],
  responses: {
    200: {
      description: "Sessions retrieved successfully",
      content: { "application/json": { schema: AuthResponseSchema } },
    },
    401: {
      description: "Unauthorized",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

registry.registerPath({
  method: "delete",
  path: "/auth/sessions/{id}",
  summary: "Revoke a specific login session by session ID",
  tags: ["Authentication"],
  security: [{ bearerAuth: [] }],
  request: {
    params: z.object({
      id: z.string().openapi({ description: "Session ID to revoke" }),
    }),
  },
  responses: {
    200: {
      description: "Session revoked successfully",
      content: { "application/json": { schema: AuthResponseSchema } },
    },
    401: {
      description: "Unauthorized",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
    404: {
      description: "Session not found",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});

registry.registerPath({
  method: "delete",
  path: "/auth/sessions",
  summary: "Revoke all login sessions except current session",
  tags: ["Authentication"],
  security: [{ bearerAuth: [] }],
  responses: {
    200: {
      description: "All sessions revoked successfully",
      content: { "application/json": { schema: AuthResponseSchema } },
    },
    401: {
      description: "Unauthorized",
      content: { "application/json": { schema: ErrorResponseSchema } },
    },
  },
});
