import { FLAGS, useFeatureValue } from '@komi-app/flags-sdk'
import { AsqMeItem } from '@komi-app/profiles-sdk'
import { ModuleType, AsqMeModuleType } from '@komi-app/meta-profiles'

export function getAsqMeUrlSegmentForModuleType(moduleType: string): string {
  switch (moduleType) {
    case ModuleType.ASQME_Q_AND_A:
      return 'asqme-q-and-a'
    case ModuleType.ASQME_PAID_DM:
      return 'asqme-paid-dm'
    case ModuleType.ASQME_VIDEO_REQUEST:
      return 'asqme-video-request'
    case ModuleType.ASQME_FILE_REQUEST:
      return 'asqme-file-request'
    default:
      throw new Error(`Invalid asqme module type: ${moduleType}`)
  }
}

export function getAddNewLabelForModuleType(moduleType: string): string {
  switch (moduleType) {
    case ModuleType.ASQME_Q_AND_A:
      return 'Paid Q & A'
    case ModuleType.ASQME_PAID_DM:
      return 'Paid DM'
    case ModuleType.ASQME_VIDEO_REQUEST:
      return 'Custom Video Request'
    case ModuleType.ASQME_FILE_REQUEST:
      return 'Custom File Request'
    default:
      throw new Error(`Invalid asqme module type: ${moduleType}`)
  }
}

export function getAsqMeModuleTypeFromUrl(url: string): AsqMeModuleType {
  const path = url.split('/').pop()
  switch (path) {
    case 'asqme-q-and-a':
      return ModuleType.ASQME_Q_AND_A
    case 'asqme-paid-dm':
      return ModuleType.ASQME_PAID_DM
    case 'asqme-video-request':
      return ModuleType.ASQME_VIDEO_REQUEST
    case 'asqme-file-request':
      return ModuleType.ASQME_FILE_REQUEST
    default:
      throw new Error(`Can't get asqme module type from url: ${path}`)
  }
}

const getDefaultTitleForModuleType = (moduleType: AsqMeModuleType) => {
  switch (moduleType) {
    case ModuleType.ASQME_Q_AND_A:
      return 'Ask me anything'
    case ModuleType.ASQME_PAID_DM:
      return 'Send me a DM'
    case ModuleType.ASQME_VIDEO_REQUEST:
      return 'Request a video'
    case ModuleType.ASQME_FILE_REQUEST:
      return 'Personalized request'
    default:
      throw new Error(`Invalid asqme module type: ${moduleType}`)
  }
}

const getDefaultCallToActionForModuleType = (moduleType: AsqMeModuleType) => {
  switch (moduleType) {
    case ModuleType.ASQME_Q_AND_A:
      return 'Ask me anything'
    case ModuleType.ASQME_PAID_DM:
      return 'DM me'
    case ModuleType.ASQME_VIDEO_REQUEST:
      return 'Request Video'
    case ModuleType.ASQME_FILE_REQUEST:
      return 'Request'
    default:
      throw new Error(`Invalid asqme module type: ${moduleType}`)
  }
}

export function makeDefaultItem(
  moduleType: AsqMeModuleType = ModuleType.ASQME_Q_AND_A
): AsqMeItem {
  return {
    title: getDefaultTitleForModuleType(moduleType),
    callToAction: getDefaultCallToActionForModuleType(moduleType),
    price: undefined,
    currency: 'USD',
    visible: true,
    thumbnail: '',
    handle: ''
  }
}

export function useIsAsqMeFeatureEnabled(moduleType: AsqMeModuleType): boolean {
  const { q_and_a, paid_dm, file_request, video_request } = useFeatureValue<{
    q_and_a?: boolean
    paid_dm?: boolean
    file_request?: boolean
    video_request?: boolean
  }>(FLAGS.ASQME_INTEGRATION, {})

  switch (moduleType) {
    case ModuleType.ASQME_Q_AND_A:
      return !!q_and_a
    case ModuleType.ASQME_PAID_DM:
      return !!paid_dm
    case ModuleType.ASQME_VIDEO_REQUEST:
      return !!video_request
    case ModuleType.ASQME_FILE_REQUEST:
      return !!file_request
  }
}
