import { useQuery } from '@tanstack/react-query'
import {
  BaseCampaign,
  CampaignItem,
  CampaignType,
  EmailCampaign
} from '@komi-app/messaging-sdk'

import { getCampaignSDK } from '../services'
import { useInitProfileId } from './useInitProfileId'
import { Maybe } from 'utils/types'

export function useSmartScheduleCampaign(campaignId: string | null) {
  const profileId = useInitProfileId()

  return useQuery({
    queryKey: ['campaigns', campaignId, 'get'],
    queryFn: () => getSmartScheduleCampaign(campaignId),
    enabled: Boolean(campaignId) && Boolean(profileId)
  })
}

async function getSmartScheduleCampaign(
  campaignId: string | null
): Promise<Maybe<BaseCampaign<EmailCampaign>>> {
  if (!campaignId) {
    return undefined
  }

  return await getCampaignSDK().email.getOne(campaignId)
}
