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

export const UserPermissionAddSchema = z.object({
  name: z.string({ required_error: 'REQUIRED' }).min(4, 'MIN_NAME_4'),
  email: z.string({ required_error: 'REQUIRED' }).email('EMAIL'),
  password: z.string({ required_error: 'REQUIRED' }).min(8, 'MIN_PASSWORD_DIGITS'),
  account_type: z.enum(['developer', 'top_management', 'sales_management', 'sales_executive'], {
    required_error: 'REQUIRED'
  }),
  phone: z.string({ required_error: 'REQUIRED' }).refine(
    value => {
      const phoneNumber = parsePhoneNumberFromString(value)
      return phoneNumber?.isValid()
    },
    { message: 'PHONE_INVALID' }
  ),
  status: z.enum(['active', 'inactive'], { required_error: 'REQUIRED' })
})

export const UserUpdateSchema = z.object({
  name: z.string({ required_error: 'REQUIRED' }).min(4, 'MIN_NAME_4'),
  email: z.string({ required_error: 'REQUIRED' }).email('EMAIL'),
  password: z.string({ required_error: 'REQUIRED' }).min(8, 'MIN_PASSWORD_DIGITS'),
  account_type: z.enum(['developer', 'top_management', 'sales_management', 'sales_executive'], {
    required_error: 'REQUIRED'
  }),
  phone: z.string({ required_error: 'REQUIRED' }).refine(
    value => {
      const phoneNumber = parsePhoneNumberFromString(value, 'SA')
      return phoneNumber?.isValid()
    },
    { message: 'PHONE_INVALID' }
  ),
  status: z.enum(['active', 'inactive'], { required_error: 'REQUIRED' })
})

export type UserPermissionValue = z.infer<typeof UserPermissionAddSchema>

export type UserPermissionUpdateValue = z.infer<typeof UserUpdateSchema>
