import type { NuxtAxiosInstance } from '@nuxtjs/axios'
import {
  DATE_RANGE_ENDPOINT_URL,
  SIMPLE_CREATORS_ENDPOINT_URL,
} from '~/types/statistics'
import { CreatorSimple, DataColors, STANDOUT_COLOR } from '~/types/index'

/**
 * @deprecated Use store instead
 */
export async function listCreatorsSimple($axios: NuxtAxiosInstance) {
  const creators = await $axios.$get(SIMPLE_CREATORS_ENDPOINT_URL)
  if (!creators) {
    return { toasts: [] }
  }
  const creatorsSimple = (creators as CreatorSimple[])
    .filter(
      creator => creator.plan != null && !creator.plan.startsWith('basic'),
    )
    .map((creator: CreatorSimple) => ({
      id: creator.id,
      name: creator.name,
      color: DataColors.getAndOccupyRandomColor(),
      plan: creator.plan,
      status: creator.status,
    }))

  if (creatorsSimple.length === 0) {
    return {
      toasts: ['No creators on a sufficient plan found.'],
    }
  }

  creatorsSimple.unshift({
    id: 0,
    name: 'Total',
    color: STANDOUT_COLOR,
    plan: 'advanced_yearly',
    status: [],
  })

  const range = await $axios.$get(DATE_RANGE_ENDPOINT_URL)
  const dateRange = {
    startDate: new Date(range.from),
    endDate: new Date(range.to),
  }
  return { creatorsSimple, dateRange }
}

export async function fetchDateRange(axios: NuxtAxiosInstance) {
  const range = await axios.$get(DATE_RANGE_ENDPOINT_URL)
  return { startDate: new Date(range.from), endDate: new Date(range.to) }
}
