import { computed, ref, type ComputedRef, type Ref } from 'vue'

import { watchAdminErrorToast } from '../../composables/useAdminToast'
import { useAuthStore } from '../../stores/auth'
import { useAwardsStore } from '../../stores/awards'
import type { HomeDisplayCategory, HomeInteractionModalKind, HomePreviewPhase, HomeSuccessKind } from './homeLandingTypes'
import { useHomeParticipationActions } from './useHomeParticipationActions'
import { useHomeParticipationPresentation } from './useHomeParticipationPresentation'

type AuthStore = ReturnType<typeof useAuthStore>
type AwardsStore = ReturnType<typeof useAwardsStore>

export function useHomeParticipationState(params: {
  store: AwardsStore
  authStore: AuthStore
  routerPush: (path: string) => Promise<unknown>
  twitchUser: ComputedRef<string>
  displayCategories: ComputedRef<HomeDisplayCategory[]>
  archiveYears: ComputedRef<Array<{ year: number }>>
  archiveYear: Ref<number>
  modal: Ref<null | HomeInteractionModalKind>
  previewPhase: Ref<HomePreviewPhase>
  activeCat: Ref<number>
  deleteConfirm: Ref<boolean>
  accountActionError: Ref<string>
  openModal: (kind: HomeInteractionModalKind) => void
  closeAccountModal: () => void
}) {
  const {
    store,
    authStore,
    routerPush,
    twitchUser,
    displayCategories,
    archiveYears,
    archiveYear,
    modal,
    previewPhase,
    activeCat,
    deleteConfirm,
    accountActionError,
    openModal,
    closeAccountModal,
  } = params

  const votes = ref<Record<string, number>>({})
  const savedVoteCount = ref(0)
  const submitted = ref(false)
  const submitting = ref(false)
  const formError = ref('')
  watchAdminErrorToast(formError)
  const successKind = ref<null | HomeSuccessKind>(null)
  const clipCatIdx = ref(0)
  const clipDsgvo = ref(false)
  const clipSubmissionsEnabled = computed(() => store.overview.featureFlags.clipSubmissionsEnabled)
  const clipSubmissionDisabledMessage = computed(() => store.overview.featureFlags.clipSubmissionDisabledMessage)
  const {
    notSubmitted,
    voteCount,
    totalCats,
    canSubmitVote,
    savedVoteActive,
    activeCategory,
    activeCatName,
    pickerTitle,
    pickerSubtitle,
    successTitle,
    successText,
    clipNomOptions,
    catOptions,
    canSubmitClip,
    clipSubmitStyle,
  } = useHomeParticipationPresentation({
    store,
    displayCategories,
    activeCat,
    modal,
    previewPhase,
    votes,
    savedVoteCount,
    submitted,
    successKind,
    clipCatIdx,
    clipDsgvo,
    clipSubmissionsEnabled,
  })
  const {
    initializeHomeInteractions,
    onLogin,
    onLogout,
    onConfirmDelete,
    openAdminPanel,
    setCat,
    pickNominee,
    submitVote,
    submitReminder,
    submitNomination,
    clipDsgvoChange,
    onClipCatChange,
    submitClip,
    onPrimaryPhaseAction,
    onSectionAction,
    onTimelineFinalAction,
    syncPreviewPhaseFromOverview,
  } = useHomeParticipationActions({
    store,
    authStore,
    routerPush,
    twitchUser,
    displayCategories,
    archiveYears,
    archiveYear,
    previewPhase,
    activeCat,
    deleteConfirm,
    accountActionError,
    openModal,
    closeAccountModal,
    votes,
    savedVoteCount,
    submitted,
    submitting,
    formError,
    successKind,
    clipCatIdx,
    clipDsgvo,
    clipSubmissionsEnabled,
    clipSubmissionDisabledMessage,
  })

  return {
    votes,
    savedVoteCount,
    submitted,
    submitting,
    formError,
    successKind,
    clipCatIdx,
    clipDsgvo,
    clipSubmissionsEnabled,
    clipSubmissionDisabledMessage,
    notSubmitted,
    voteCount,
    totalCats,
    canSubmitVote,
    savedVoteActive,
    activeCategory,
    activeCatName,
    pickerTitle,
    pickerSubtitle,
    successTitle,
    successText,
    clipNomOptions,
    catOptions,
    canSubmitClip,
    clipSubmitStyle,
    initializeHomeInteractions,
    onLogin,
    onLogout,
    onConfirmDelete,
    openAdminPanel,
    setCat,
    pickNominee,
    submitVote,
    submitReminder,
    submitNomination,
    clipDsgvoChange,
    onClipCatChange,
    submitClip,
    onPrimaryPhaseAction,
    onSectionAction,
    onTimelineFinalAction,
    syncPreviewPhaseFromOverview,
  }
}
