import { GetterTree, MutationTree, ActionTree } from 'vuex'
import {
  CreatorSimple,
  DataColors,
  GET_PROFILE_PICTURE_URL,
  GET_USER_PROFILE_PICTURE_URL,
  STANDOUT_COLOR,
  SubscriptionStatus,
} from '~/types'
import {
  DATE_RANGE_ENDPOINT_URL,
  SIMPLE_CREATORS_ENDPOINT_URL,
} from '~/types/statistics'

export const state = () => ({
  creators: [] as CreatorSimple[],
  dateRange: {
    startDate: new Date(),
    endDate: new Date(),
  },
  axiosBaseUrl: process.env.AXIOS_BASE_URL ?? 'https://backend.creatorhero.app',
})

export type RootState = ReturnType<typeof state>

export const getters: GetterTree<RootState, RootState> = {
  creators: state => state.creators,
  dateRange: state => state.dateRange,
  axiosBaseUrl: state => state.axiosBaseUrl,
  profilePictureUrl: state => GET_PROFILE_PICTURE_URL(state.axiosBaseUrl),
  profilePictureUrlByUser: state => (userId?: number) =>
    GET_USER_PROFILE_PICTURE_URL(state.axiosBaseUrl, userId ?? 0),
}

export const mutations: MutationTree<RootState> = {
  ADD_CREATOR(state: any, creator: CreatorSimple) {
    state.creators.push(creator)
  },
  ADD_CREATORS(state: any, creators: CreatorSimple[]) {
    state.creators.push(...creators)
  },
  SET_CREATORS(state: any, creators: CreatorSimple[]) {
    state.creators = creators
  },
  REMOVE_CREATOR(state: any, creator: CreatorSimple) {
    state.creators.splice(state.creators.indexOf(creator), 1)
  },
  REPLACE_CREATOR(state: any, creator: CreatorSimple) {
    const index = state.creators.findIndex(
      (t: CreatorSimple) => t.id === creator.id,
    )
    state.creators.splice(index, 1, creator)
  },
  SET_DATE_RANGE(state: any, dateRange: { startDate: Date; endDate: Date }) {
    state.dateRange = dateRange
  },
}

export const actions: ActionTree<RootState, RootState> = {
  async fetchCreators({ commit }) {
    const creators = await this.$axios.$get(SIMPLE_CREATORS_ENDPOINT_URL)
    commit('SET_CREATORS', creators)
  },
  /**
   * Only returns creators that have the 'stats' feature.
   */
  async fetchCreatorsAllInclusive({ commit }) {
    const creators = await this.$axios.$get(SIMPLE_CREATORS_ENDPOINT_URL)
    if (!creators) return
    // return { toasts: [] }
    // }
    const creatorsSimple = (creators as CreatorSimple[]).filter(
      creator =>
        creator.features.includes('stats') &&
        !creator.status.some(status =>
          [
            SubscriptionStatus.Activatable,
            SubscriptionStatus.NotActivatable,
          ].includes(status),
        ),
    )

    if (creatorsSimple.length === 0) {
      return {
        toasts: ['No creators on a sufficient plan found.'],
      }
    }

    creatorsSimple.unshift(CreatorSimple.constructTotalCreator())

    const range = await this.$axios.$get(DATE_RANGE_ENDPOINT_URL)
    const dateRange = {
      startDate: new Date(range.from),
      endDate: new Date(range.to),
    }

    commit('SET_CREATORS', creatorsSimple)
    commit('SET_DATE_RANGE', dateRange)
    // return { creatorsSimple, dateRange }
  },
}
