import { array, mixed, number, object, string } from 'yup'

import { phoneNumberRule } from '@/constants/rules/phoneNumberRule'
import { nationalCodeRule } from '@/constants/rules/nationalCodeRule'

const requiredString = (min = 0) => {
  const base = string().required()
  return min > 0 ? base.min(min) : base
}

export const personalInformationSchema = object().shape({
  avatar: mixed().notRequired(),
  avatarId: mixed().notRequired(),
  name: requiredString(3),
  birthday: string().required(),
  nationalCode: nationalCodeRule,
  maritalStatus: string().required(),
  gender: string().required(),
  phone: phoneNumberRule,
  provinceId: mixed().required(),
  cityId: mixed().required(),
  address: requiredString(10),
  postalCode: string()
    .required()
    .matches(/^\d{10}$/, 'کد پستی باید ۱۰ رقم باشد'),
  virtualPhoneNumber: string()
    .required()
    .matches(/^\d{10,15}$/, 'شماره تلفن مجازی نامعتبر است'),
  seminaryCode: requiredString(),
  seminaryServiceCenterCode: requiredString(),
  childrenCount: number().typeError('تعداد فرزند باید عدد باشد').required().min(0).max(20),
})

export const educationalInformationSchema = object().shape({
  educationStatus: string().required(),
  seminaryLevel: string().required(),
  universityLevel: string().required(),
  universityName: requiredString(5),
  fieldOfStudy: requiredString(5),
  activitySummary: requiredString(10),
})

export const professionalInformationSchema = object().shape({
  propagationExperienceYears: number()
    .typeError('سنوات تجربه تبلیغ باید عدد باشد')
    .required()
    .min(0)
    .max(70),
  propagationMethodDescription: requiredString(10),
  specializedTopics: requiredString(10),
  propagationPlatforms: array()
    .of(string())
    .min(1, 'حداقل یک بستر فعالیت را انتخاب کنید')
    .max(6, 'حداکثر ۶ بستر فعالیت قابل انتخاب است')
    .required()
    .test(
      'no-both-none-and-others',
      'نمی‌توانید همزمان "فعالیتی نداشته‌ام" و سایر گزینه‌ها را انتخاب کنید',
      (value) => {
        if (!value) return false
        const hasNone = value.includes('none')
        const hasOthers = value.some((v) => v !== 'none')
        return !(hasNone && hasOthers)
      }
    ),
  onlinePlatformDetails: string().when('propagationPlatforms', {
    is: (val) => Array.isArray(val) && val.includes('online'),
    then: (s) => s.required().min(10),
    otherwise: (s) => s.notRequired().nullable(),
  }),
  otherPlatformDetails: string().when('propagationPlatforms', {
    is: (val) => Array.isArray(val) && val.includes('other'),
    then: (s) => s.required().min(10),
    otherwise: (s) => s.notRequired().nullable(),
  }),
})

const skillSelectWithOtherSchema = (selectField, descField) =>
  object().shape({
    [selectField]: string().required(),
    [descField]: string().when(selectField, {
      is: 'other',
      then: (s) => s.required().min(10),
      otherwise: (s) => s.notRequired().nullable(),
    }),
  })

export const skillAssessmentPart1Schema = skillSelectWithOtherSchema(
  'responseToLowSatisfaction',
  'responseToLowSatisfactionDescription'
)

export const skillAssessmentPart2Schema = skillSelectWithOtherSchema(
  'responseToSessionCancellation',
  'responseToSessionCancellationDescription'
)

export const skillAssessmentPart3Schema = skillSelectWithOtherSchema(
  'responseToAudienceConflict',
  'responseToAudienceConflictDescription'
)

export const skillAssessmentPart4Schema = skillSelectWithOtherSchema(
  'responseToCompetingPropagator',
  'responseToCompetingPropagatorDescription'
)

export const skillAssessmentPart5Schema = object().shape({
  responseToUnqualifiedAdvisors: requiredString(10),
  relevantCertificates: requiredString(10),
  hijabApproach: requiredString(10),
})

export const skillAssessmentPart6Schema = object().shape({
  faithProductionAudio: mixed().required(),
  faithProductionId: mixed().notRequired(),
})

export const skillAssessmentPart7Schema = object().shape({
  leaderMessageVideo: mixed().required(),
  leaderMessageId: mixed().notRequired(),
})
