import { ReactNode } from 'react'

// Configuration for tenant-specific batch suffixes
const tenantBatchConfig = {
  upes: {
    // Configure suffixes for specific batches
    batchConfigs: {
      '2019 - 2020': ['_JAN'],
      '2020 - 2021': ['_JUL', '_JAN'],
      '2021 - 2022': ['_JUL', '_JAN'],
      '2022 - 2023': ['_JUL', '_JAN'],
      '2023 - 2024': ['_JAN', '_JUL'],
      '2024 - 2025': ['_JUL', '_JAN'],
      '2025 - 2026': ['_JAN', '_JUL'],
      '2026 - 2027': ['_JAN'],
      // Add more specific year configurations as needed
    },
  },
  // Add other tenant configurations here as needed
}

export const batchOptions = (() => {
  const baseBatches = Array.from({ length: 12 }, (_, i) => {
    return `${new Date().getFullYear() - i} - ${
      new Date().getFullYear() - i + 1
    }`
  })

  // Check if current tenant has specific batch configuration
  const tenantConfig = tenantBatchConfig[window.location.hostname.split('.')[0]]

  if (tenantConfig) {
    const { batchConfigs } = tenantConfig
    const batchesWithSuffixes = []

    baseBatches.forEach((batch) => {
      // Check if this batch has specific configuration
      const suffixes = batchConfigs[batch]

      if (suffixes) {
        // Add batch with each suffix
        suffixes.forEach((suffix) => {
          batchesWithSuffixes.push(`${batch}${suffix}`)
        })
      } else {
        // Add batch without suffix
        batchesWithSuffixes.push(batch)
      }
    })
    console.log(batchesWithSuffixes, 'suffix')
    return batchesWithSuffixes
  }

  // Return standard batches for other tenants
  return baseBatches
})()

export const getYears = () => {
  const start = 2019
  const end = new Date().getFullYear() + 3
  const values = new Array(end - start).fill(0).map((_, i) => ({
    label: `${start + i}`,
    value: `${start + i}`,
  }))
  return values.reverse()
}

export const examGroupBatches = Array.from({ length: 10 }, (_, i) => {
  return `${2012 + i} - ${2012 + i + 1}`
})
export const currentYear = new Date().getFullYear()

const startYear = 2012

export const yearOptions = Array.from(
  { length: currentYear - startYear + 1 },
  (_, i) => {
    return `${startYear + i}`
  },
)

export const months = [
  'January',
  'February',
  'March',
  'April',
  'May',
  'June',
  'July',
  'August',
  'September',
  'October',
  'November',
  'December',
]

export const religionOptions = [
  'Hinduism',
  'Islam',
  'Christianity',
  'Sikhism',
  'Buddhism',
  'Jainism',
]

export const casteOptions = [
  'OC',
  'BC_A',
  'BC_B',
  'BC_C',
  'BC_D',
  'BC_E',
  'SC',
  'ST',
]

export const nationalityOptions = ['Indian', 'Other']

export const examTimings = [
  '09:00 AM',
  '10:00 AM',
  '11:00 AM',
  '01:00 PM',
  '02:00 PM',
  '03:00 PM',
]

export const USER_TYPES = [
  {
    label: 'Staff User',
    value: 'Staff_User',
  },
  {
    label: 'Faculty User',
    value: 'Faculty_User',
  },
  {
    label: 'Student User',
    value: 'Student_User',
  },
]

export function getRandomColor(name) {
  const firstAlphabet = name.charAt(0).toLowerCase()

  const asciiCode = firstAlphabet.charCodeAt(0)

  const colorNum =
    asciiCode.toString() + asciiCode.toString() + asciiCode.toString()

  var num = Math.round(0xffffff * parseInt(colorNum))
  var r = (num >> 16) & 145
  var g = (num >> 8) & 145
  var b = num & 145

  return {
    color: 'rgb(' + r + ', ' + g + ', ' + b + ', 0.8)',
    character: firstAlphabet?.toUpperCase(),
  }
}

export const ProfileApplicationType = [
  {
    label: 'Square',
    value: 'square',
  },
  {
    label: 'Exams',
    value: 'exams',
  },
  {
    label: 'Payments',
    value: 'payments',
  },
  {
    label: 'EnrollX',
    value: 'enroll_x',
  },
  {
    label: 'Hostels',
    value: 'hostels',
  },
  {
    label: 'CommuteX',
    value: 'commute_x',
  },
  {
    label: 'PeopleX',
    value: 'hrms',
  },
  {
    label: 'AdminX',
    value: 'admin',
  },
  {
    label: 'Dashboards',
    value: 'dashboards',
  },
]

export interface Option {
  label: string | ReactNode
  value: any
}

export interface GenerateLabelValueProps {
  labelKey?: string
  valueKey?: string
  useIndexAsValue?: boolean
  startFromOne?: boolean
  showAll?: boolean
}

export const generateLabelValueOptions = (
  options: any[],
  {
    labelKey,
    valueKey,
    useIndexAsValue,
    startFromOne,
    showAll,
  }: GenerateLabelValueProps = {
    labelKey: null,
    valueKey: null,
    useIndexAsValue: false,
    startFromOne: false,
    showAll: false,
  },
): Option[] => {
  let result
  if (labelKey == null) {
    result = options.map((option, index) => ({
      label: option[0].toUpperCase() + option.slice(1),
      value: useIndexAsValue ? (startFromOne ? index + 1 : index) : option,
    }))
  } else {
    result = options.map((option) => ({
      label: option[labelKey],
      value: option[valueKey],
    }))
  }
  if (showAll) {
    result.unshift({ label: 'All', value: 'all' })
  }
  return result
}
