import { z } from "zod";

export const loginSchema = z.object({
  email: z
    .string()
    .trim()
    .min(1, "Please enter your email address")
    .email("Please enter a valid email address (e.g. john@example.com)")
    .refine((email) => {
      const [, domain] = email.split("@");

      if (!domain) return false;

      const labels = domain.split(".");

      return labels.every(
        (label) =>
          label.length > 0 && !label.startsWith("-") && !label.endsWith("-"),
      );
    }, "Please enter a valid email address (e.g. john@example.com)"),
  password: z
    .string()
    .min(1, "Password is required")
    .refine(
      (value) =>
        /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{8,}$/.test(value),
      {
        message:
          "Password must be at least 8 characters long and contain at least one uppercase letter, one lowercase letter, one number, and one special character.",
      },
    ),
});

export type LoginSchema = z.infer<typeof loginSchema>;
