import { z as zod } from 'zod';


export const loginformSchema = zod.object({
    email: zod.string().email({ message: 'Invalid email address' }).trim().toLowerCase().min(5, { message: 'Email address must be at least 5 characters long' }).max(255).refine(value => value.trim() !== '', { message: 'Email address cannot be empty' }),
    password: zod.string().min(6, { message: 'Password must be at least 6 characters long' }),
});

export type LoginFormInput = zod.infer<typeof loginformSchema>;

const requiredMessage = 'This field is required';

export const regiserFormSchema = zod
    .object({
        otpType: zod.enum(["phone", "email"], {
            message: 'Otp type must be either "phone" or "email"',
        }),
        firstName: zod
            .string()
            .min(2, "Name should be minimum 2 characters")
            .refine(
                (value) => /^[A-Za-z\s]+$/.test(value),
                {
                    message:
                        "Name should contain only alphabetic characters and spaces",
                }
            )
            .refine((value) => value.trim() !== "", {
                message: requiredMessage,
            }),

        email: zod
            .string()
            .email({ message: "Invalid email address" })
            .trim()
            .toLowerCase()
            .min(5, {
                message: "Email address must be at least 5 characters long",
            })
            .max(255)
            .refine((value) => value.trim() !== "", {
                message: "Email address cannot be empty",
            }),

        password: zod
            .string()
            .min(6, "Password must be at least 6 characters long")
            .max(100, "Password must not exceed 100 characters")
            .regex(/^\S*$/, "Password cannot contain spaces"),

        confirmPassword: zod
            .string()
            .min(6, "Confirm password must be at least 6 characters long"),

        phone: zod
            .string()
            .trim()
            .min(8, "Phone number must be at least 8 digits long")
            .max(15, "Phone number must not exceed 15 digits")
            .regex(/^\d+$/, "Phone number must contain only numbers"),
        aggreeWithTermsAndCondions: zod.boolean().default(true),
        countryCode: zod.string({
            message: "Country code is required",
        }),
    })
    .refine((data) => data.password === data.confirmPassword, {
        message: "Passwords do not match",
        path: ["confirmPassword"],
    });

export type RegisterFormInput = zod.infer<typeof regiserFormSchema>;

export const guestRegiserFormSchema = zod.object({
    email: zod
        .string({
            message: "Email is required",
        })
        .trim()
        .min(1, "Email is required")
        .toLowerCase()
        .email("Please provide a valid email address"),
    countryCode: zod.string({
        message: "Country code is required",
    }),
    otpType: zod.enum(["phone", "email"], {
        message: 'Otp type must be either "phone" or "email"',
    }),
    phone: zod.string().min(8, { message: 'Phone number must be at least 8 characters long' }).max(15).refine(value => value.trim() !== '', { message: 'Phone number cannot be empty' }).refine(value => /^[0-9]+$/.test(value), { message: 'Phone number must contain only numerals' }),
});
export type GuestRegisterFormInput = zod.infer<typeof guestRegiserFormSchema>;

export const verifyOtpSchema = zod.object({
    otp: zod
        .string()
        .length(4, "OTP must be 4 digits")
        .regex(/^\d+$/, "OTP must contain only numbers"),
    otpType: zod.enum(["phone", "email"], {
        message: 'Otp type must be either "phone" or "email"',
    }),
})

export type VerifyOtpFormInput = zod.infer<typeof verifyOtpSchema>;

export const forgotPasswordRequestFormSchema = zod.object({
    otpType: zod.enum(["phone", "email"], {
        message: 'Otp type must be either "phone" or "email"',
    }),
    email: zod.string().email({ message: 'Invalid email address' }).trim().toLowerCase().min(5, { message: 'Email address must be at least 5 characters long' }).max(255).refine(value => value.trim() !== '', { message: 'Email address cannot be empty' }),
});
export type ForgotPasswordRequestFormInput = zod.infer<typeof forgotPasswordRequestFormSchema>;

export const resetPasswordFormSchema = zod.object({
    //email: zod.string().email({ message: 'Invalid email address' }).trim().toLowerCase().min(5, { message: 'Email address must be at least 5 characters long' }).max(255).refine(value => value.trim() !== '', { message: 'Email address cannot be empty' }),
    // otp: zod.string().min(4, { message: 'Otp must be at least 6 characters long' }).max(6).refine(value => value.trim() !== '', { message: 'Otp address cannot be empty' }),
    password: zod.string().min(6, { message: 'Password must be at least 6 characters long' }),
    confirmPassword: zod.string().min(6, { message: 'Confirm password must be at least 6 characters long' }),
}).superRefine(({ confirmPassword, password }, ctx) => {
    if (confirmPassword !== password) {
        ctx.addIssue({
            code: "custom",
            message: "Password and confirm password must be the same",
            path: ["confirmPassword"]
        });
    }
});

export type ResetPasswordFormInput = zod.infer<typeof resetPasswordFormSchema>;
