import { billingAddress } from '@/constants/customer';
import { z as zod } from 'zod';


export const checkoutSchema = zod.object({
    shippingId: zod.string().optional(),
    billingId: zod.string().regex(/^[0-9a-fA-F]{24}$/, "Billing address is required."),
    billingMethod: zod.string().optional(),
    pickupStoreId: zod.string().optional(),
    stateId: zod.string().optional(),
    cityId: zod.string().optional(),
    orderComments: zod.string().optional(),
    additionalDetails: zod.any().optional(),
    paymentMethodId: zod.string().regex(/^[0-9a-fA-F]{24}$/, "Please choose your payment method"),
}).superRefine((data, ctx) => {
    if (!data.pickupStoreId && !data.shippingId) {
        ctx.addIssue({
            code: 'custom',
            message: 'Please choose shipping address',
            path: ['shippingId'],
        });
    }

    if (data.billingMethod === billingAddress.continueWithBillingAddress && data.billingId == '') {
        ctx.addIssue({
            code: 'custom',
            message: 'Please choose billing address',
            path: ['billingId'],
        });
    }
});
export type CheckoutFormInput = zod.infer<typeof checkoutSchema>;

export const orderCancelSchema = zod.object({
    orderId: zod
        .string()
        .regex(/^[0-9a-fA-F]{24}$/, {
            message: "Shipping address is required. Please select a shipping address",
        }),

    cancelReason: zod
        .string()
        .min(1, "Cancel reason is required")
        .min(9, {
            message: "Cancel reason should be at least 9 characters long",
        }),
});
export type OrderCancelFormInput = zod.infer<typeof orderCancelSchema>;

export const returnOrderProductsSchema = zod.object({
    orderProducts: zod
        .array(
            zod.object({
                orderProductId: zod
                    .string()
                    .min(1, "Order Product ID is required"),

                variantId: zod
                    .string()
                    .min(1, "Variant ID is required"),
            })
        )
        .min(1, "At least one order product must be selected"),

    returnReason: zod
        .string()
        .min(1, "Return reason is required"),
});

export type ReturnOrderProductsFormInput = zod.infer<typeof returnOrderProductsSchema>;