import { z as zod } from 'zod';

import { ADDRESS_MODES, ADDRESS_TYPES } from '@/constants/customer'


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

export const updateCustomerSchema = 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()
        .min(1, "Country code is required"),
});

export type UpdateCustomerProfileFormInput = zod.infer<typeof updateCustomerSchema>;
export const changePassword = zod
    .object({
        oldPassword: zod
            .string()
            .min(1, "Old password is required")
            .min(6, "Old 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: ["newPassword"],
            });
        }
    });
export type ChangePasswordFormInput = zod.infer<typeof changePassword>;

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

        addressType: zod.enum(ADDRESS_TYPES).refine(
            (value) => !!value,
            {
                message: "Address type is required",
            }
        ),

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

        addressMode: zod.enum(ADDRESS_MODES).refine(
            (value) => !!value,
            {
                message: "Address mode is required",
            }
        ),

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

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

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

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

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

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

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

        stateId: zod
            .string()
            .min(1, "State is required"),

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

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

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

        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>;