import parsePhoneNumberFromString from 'libphonenumber-js'
import { z } from 'zod'

export const VisitorAddSchema = z
  .object({
    customer_name_ar: z.string().optional(),
    customer_name_en: z.string().optional(),
    /*     company_reg: z.string({ required_error: 'REQUIRED' }).min(6, 'MIN_DIGITS_6'),
     */
    company_reg: z.string().optional(),
    official_number: z
      .string()
      .optional()
      .refine(
        value => {
          if (!value || value.trim() === '' || /^\+\d{1,4}$/.test(value)) {
            // Treat as empty if it's just a country code like +966
            return true
          }
          const phoneNumber = parsePhoneNumberFromString(value)
          return phoneNumber?.isValid()
        },
        { message: 'PHONE_INVALID' }
      ),
    official_email: z.string().email('EMAIL').optional().or(z.literal('')),
    category_id: z.number({ required_error: 'REQUIRED_CATEGORY', invalid_type_error: 'REQUIRED' }),
    /*     contact_name: z.string({ required_error: 'REQUIRED' }).min(2, 'MIN_NAME_2'),
     */ contact_name: z.string().optional(),

    /*     contact_title: z.string({ required_error: 'REQUIRED' }).min(2, 'MIN_NAME_2'),
     */
    contact_title: z.string().optional(),

    contact_number: z
      .string()
      .optional()
      .refine(
        value => {
          if (!value || value.trim() === '' || /^\+\d{1,4}$/.test(value)) {
            // Treat as empty if it's just a country code like +966
            return true
          }
          const phoneNumber = parsePhoneNumberFromString(value)
          return phoneNumber?.isValid()
        },
        { message: 'PHONE_INVALID' }
      ),
    category_other: z.string().optional(),
    contact_email: z.string().optional(),
    interested_in: z.array(z.enum(['freezers', 'products']), { invalid_type_error: 'REQUIRED' }),
    interested_in_options: z.any({ required_error: 'REQUIRED_ICECREAM_OPTION' }),
    district_id: z.number({ required_error: 'REQUIRED_AREA' }),
    city_id: z.number({ required_error: 'REQUIRED_CITY' }),
    geo_location: z.any().optional(),
    lat: z.any().optional(),
    long: z.any().optional(),
    date: z.any().optional(),
    main_supplier: z.number({ required_error: 'REQUIRED_MAIN_SUPPLIER', invalid_type_error: 'REQUIRED' }),
    target_price: z.string({ required_error: 'REQUIRED_TARGET_PRICE' }),
    main_supplier_other: z.any().optional(),
    notes: z.string().optional()
  })
  .refine(
    data => {
      if (data.interested_in.includes('products')) {
        return Array.isArray(data.interested_in_options) && data.interested_in_options.length > 0
      }
      return true
    },
    {
      path: ['interested_in_options'], // مكان الخطأ
      message: 'Ice cream option is required when interested in products'
    }
  )
// .superRefine((data, ctx) => {
//   const ar = (data.customer_name_ar ?? '').trim()
//   const en = (data.customer_name_en ?? '').trim()

//   const isArabicValid = ar.length >= 2
//   const isEnglishValid = en.length >= 2

//   if (!isArabicValid && !isEnglishValid) {
//     ctx.addIssue({
//       path: ['customer_name_ar'],
//       code: z.ZodIssueCode.custom,
//       message: 'REQUIRED_ONE_NAME_MIN_2'
//     })
//     ctx.addIssue({
//       path: ['customer_name_en'],
//       code: z.ZodIssueCode.custom,
//       message: 'REQUIRED_ONE_NAME_MIN_2'
//     })
//   }
// })

export type VisitorAddValues = z.infer<typeof VisitorAddSchema>

export const ContactAddSchema = z.object({
  contact_name: z.string({ required_error: 'required..' }).min(2, 'req..'),
  contact_title: z.string({ required_error: 'required..' }).min(2, 'req..'),
  contact_number: z
    .string()
    .optional()
    .refine(
      value => {
        if (!value || value.trim() === '' || /^\+\d{1,4}$/.test(value)) {
          // Treat as empty if it's just a country code like +966
          return true
        }
        const phoneNumber = parsePhoneNumberFromString(value)
        return phoneNumber?.isValid()
      },
      { message: 'PHONE_INVALID' }
    ),
  others: z.string().optional(),
  contact_email: z.string({ required_error: 'required Email' }).email(),
  intersted_in: z.boolean().optional(),
  ice_cream_options: z.string().optional(),
  city_id: z.number({ required_error: 'required' }),
  district_id: z.string({ required_error: 'required' }),

  area: z.string({ required_error: 'required' })
})

export type ContactAddValues = z.infer<typeof ContactAddSchema>
