import { z } from "zod";

const emailSchema = z
  .email("Invalid email address")
  .trim()
  .min(1, "Email is required")
  .transform((email) => email.toLowerCase());

const passwordSchema = z
  .string()
  .min(8, "Password must be at least 8 characters")
  .max(128, "Password cannot exceed 128 characters");

const strongPasswordSchema = z
  .string()
  .min(8, "Password must be at least 8 characters")
  .max(128, "Password cannot exceed 128 characters")
  .regex(
    /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$/,
    "Password must contain at least one uppercase letter, one lowercase letter, and one number",
  );

const otpSchema = z.string().regex(/^\d{6}$/, "OTP must be exactly 6 digits");

const phoneSchema = z
  .string()
  .trim()
  .min(1, "Phone number is required")
  .regex(/^[0-9]{10,15}$/, "Phone number must be 10–15 digits");

const nameSchema = z
  .string()
  .trim()
  .min(2, "Name must be at least 2 characters")
  .max(100, "Name cannot exceed 100 characters");

export const OtpPurposeSchema = z.enum([
  "FORGOT_PASSWORD",
  "EMAIL_VERIFICATION",
]);

/**
 * Register
 */
export const registerSchema = z.object({
  body: z.object({
    name: nameSchema,
    email: emailSchema,
    password: strongPasswordSchema,
    phone: phoneSchema,
  }),
});

/**
 * Login
 */
export const loginSchema = z.object({
  body: z.object({
    email: emailSchema,
    password: passwordSchema,
  }),
});

/**
 * Refresh Token
 */
export const refreshSchema = z.object({
  body: z.object({
    refreshToken: z.string().trim().min(1, "Refresh token is required"),
  }),
});

/**
 * Logout
 */
export const logoutSchema = z.object({
  body: z.object({
    refreshToken: z.string().trim().min(1, "Refresh token is required"),
  }),
});

/**
 * Forgot Password
 */
export const forgotPasswordSchema = z.object({
  body: z.object({
    email: emailSchema,
  }),
});

/**
 * Verify OTP
 */
export const verifyOtpSchema = z.object({
  body: z.object({
    email: emailSchema,
    otp: otpSchema,
    purpose: OtpPurposeSchema.optional(),
  }),
});

/**
 * Reset Password
 */
export const resetPasswordSchema = z.object({
  body: z.object({
    email: emailSchema,
    otp: otpSchema,
    newPassword: strongPasswordSchema,
  }),
});

/**
 * Change Password
 */
export const changePasswordSchema = z.object({
  body: z
    .object({
      currentPassword: passwordSchema,
      newPassword: strongPasswordSchema,
    })
    .refine((data) => data.currentPassword !== data.newPassword, {
      message: "New password must be different from current password",
      path: ["newPassword"],
    }),
});

export const updateProfileSchema = z.object({
  body: z.object({
    name: nameSchema.optional(),
    phone: z.string().trim().regex(/^[0-9]{10,15}$/, "Phone must be 10–15 digits").optional(),
  }),
});

/**
 * Types
 */

export type RegisterInput = z.infer<typeof registerSchema>["body"];
export type LoginInput = z.infer<typeof loginSchema>["body"];
export type RefreshInput = z.infer<typeof refreshSchema>["body"];
export type LogoutInput = z.infer<typeof logoutSchema>["body"];
export type ForgotPasswordInput = z.infer<typeof forgotPasswordSchema>["body"];
export type VerifyOtpInput = z.infer<typeof verifyOtpSchema>["body"];
export type ResetPasswordInput = z.infer<typeof resetPasswordSchema>["body"];
export type ChangePasswordInput = z.infer<typeof changePasswordSchema>["body"];
