import { ADDRESS_MODES, ADDRESS_TYPES } from '@/src/constants/data/address';
import { z as zod } from 'zod';

function enumListString(enumValues: readonly string[]): string {
    return enumValues.map(value => `"${value}"`).join(', ');
}

export const changePassword = zod.object({
    oldPassword: zod
        .string()
        .min(1, "Current password is required")
        .min(6, "Current Password too short - should be 6 chars minimum"),

    newPassword: zod
        .string()
        .min(1, "New password is required")
        .min(6, "New Password too short - should be 6 chars minimum"),

    confirmNewPassword: zod
        .string()
        .min(1, "Confirm new password is required")
        .min(6, "Confirm new Password too short - should be 6 chars minimum"),

}).superRefine(({ newPassword, confirmNewPassword }, ctx) => {
    if (newPassword !== confirmNewPassword) {
        ctx.addIssue({
            code: "custom",
            message: "The password and the confirmed password do not match.",
            path: ["confirmNewPassword"],
        });
    }
});
export type ChangePasswordFormInput = zod.infer<typeof changePassword>;

export const updateUserSchema = zod.object({
    email: zod
        .string()
        .min(1, "Email is required")
        .email("Please provide a valid email address"),

    firstName: zod
        .string()
        .min(1, "First name is required")
        .min(3, "First name should be 3 chars minimum"),

    phone: zod
        .string()
        .min(1, "Phone number is required")
        .refine((value) => /^\d+$/.test(value) && value.length >= 9, {
            message: "Phone number should contain only numbers and be at least 9 digits long",
            path: ["phone"],
        }),

    countryCode: zod.string().optional(),

});
export type UpdateUserProfileFormInput = zod.infer<typeof updateUserSchema>;

export const customerAddressSchema = zod.object({
    addressId: zod.string().optional(),

    addressType: zod.enum(ADDRESS_TYPES, {
        error: `Address type must be one of the following: ${enumListString(ADDRESS_TYPES)}`,
    }),

    defaultAddress: zod.boolean().optional(),

    addressMode: zod.enum(ADDRESS_MODES, {
        error: `Address mode must be one of the following: ${enumListString(ADDRESS_MODES)}`,
    }),

    name: zod
        .string()
        .min(1, {
            message: "Name is required",
        }),

    address1: zod
        .string()
        .min(1, {
            message: "Address line 1 is required",
        }),

    address2: zod.string().optional(),

    phoneNumber: zod
        .string()
        .min(1, {
            message: "Phone number is required",
        })
        .regex(/^\d{9,}$/, {
            message: "Phone number must be at least 9 digits",
        }),

    landlineNumber: zod.string().optional(),

    state: zod
        .string()
        .min(1, {
            message: "State is required",
        }),

    country: zod
        .string()
        .min(1, {
            message: "Country is required",
        }),

    city: zod
        .string()
        .min(1, {
            message: "City is required",
        }),

    street: zod.string().optional(),

    zipCode: zod.string().optional(),

    longitude: zod.any().optional(),

    latitude: zod.any().optional(),
})
    .refine(
        (data) => data.longitude !== undefined || data.latitude !== undefined,
        {
            message: "Please select the address",
            path: ["longitude"],
        }
    );

export type CustomerAddressFormInput = zod.infer<typeof customerAddressSchema>;

export const guestCheckoutSchema = zod.object({
    email: zod
        .string()
        .email("Please enter a valid email"),
    phone: zod
        .string()
        .min(8, "Please enter a valid phone number"),
});

export type GuestCheckoutFormInput = zod.infer<
    typeof guestCheckoutSchema
>;