import { z } from "zod";

const nameRegex = /^[A-Za-zÀ-ÖØ-öø-ÿ\u0900-\u097F\u0A80-\u0AFF\s.'-]+$/;
const referralCodeRegex = /^[A-Za-z0-9_-]{4,30}$/;

const onlyDigits = (value: unknown) =>
  typeof value === "string" ? value.replace(/\D/g, "") : value;

const emptyToUndefined = (value: unknown) => {
  if (typeof value !== "string") return value;

  const trimmed = value.trim();

  return trimmed === "" ? undefined : trimmed;
};

export const phoneSchema = z.preprocess(
  onlyDigits,
  z
    .string()
    .length(10, "Enter a valid 10-digit mobile number.")
    .regex(/^[6-9]\d{9}$/, "Mobile number must start with 6, 7, 8, or 9."),
);

export const loginSchema = z.object({
  phone: phoneSchema,
  password: z.string().min(1, "Please enter your password."),
  product_id: z.preprocess(
    emptyToUndefined,
    z.coerce.number().refine((value) => [1, 2, 3].includes(value), {
      message: "Please select a valid product.",
    }).optional(),
  ),
});

export const verifyOtpSchema = z.object({
  phone: phoneSchema,
  otp: z.preprocess(
    onlyDigits,
    z
      .string()
      .length(6, "Enter the 6-digit OTP.")
      .regex(/^\d{6}$/, "OTP must contain only numbers."),
  ),
});

export const registerSchema = z
  .object({
    full_name: z
      .string()
      .trim()
      .min(2, "Please enter your full name.")
      .max(120, "Full name must not exceed 120 characters.")
      .regex(nameRegex, "Please enter a valid full name."),

    email: z
      .string()
      .trim()
      .toLowerCase()
      .email("Please enter a valid email address.")
      .max(160, "Email must not exceed 160 characters."),

    phone: phoneSchema,

    company_name: z
      .string()
      .trim()
      .min(2, "Please enter your company or society name.")
      .max(160, "Company or society name must not exceed 160 characters."),

    society_type: z.preprocess(
      emptyToUndefined,
      z.string().trim().max(80, "Society type must not exceed 80 characters.").optional(),
    ),

    has_referral: z.enum(["yes", "no"]).optional(),

    referral_code: z.preprocess(
      emptyToUndefined,
      z
        .string()
        .trim()
        .regex(referralCodeRegex, "Please enter a valid referral code.")
        .optional(),
    ),

    company_address: z
      .string()
      .trim()
      .min(5, "Please enter your company address.")
      .max(300, "Address must not exceed 300 characters."),

    city: z
      .string()
      .trim()
      .min(2, "Please enter your city.")
      .max(80, "City must not exceed 80 characters.")
      .regex(nameRegex, "Please enter a valid city."),

    state: z
      .string()
      .trim()
      .min(2, "Please enter your state.")
      .max(80, "State must not exceed 80 characters.")
      .regex(nameRegex, "Please enter a valid state."),
  })
  .refine((values) => values.has_referral !== "yes" || Boolean(values.referral_code), {
    path: ["referral_code"],
    message: "Please enter your referral code.",
  });

export type LoginValues = z.infer<typeof loginSchema>;
export type RegisterValues = z.infer<typeof registerSchema>;
export type VerifyOtpValues = z.infer<typeof verifyOtpSchema>;
