import { type Evaluation } from '@/types/evaluations.types'
import React, { createContext, useContext, useState } from 'react'

type EvaluationsDialogType = 'retry' | 'view' | 'approve' | 'edit'

interface EvaluationsContextType {
  open: EvaluationsDialogType | null
  setOpen: (str: EvaluationsDialogType | null) => void
  currentRow: Evaluation | null
  setCurrentRow: React.Dispatch<React.SetStateAction<Evaluation | null>>
}

const EvaluationsContext = createContext<EvaluationsContextType | undefined>(
  undefined
)

interface Props {
  children: React.ReactNode
}

export function EvaluationsProvider({ children }: Props) {
  const [open, setOpen] = useState<EvaluationsDialogType | null>(null)

  const [currentRow, setCurrentRow] = useState<Evaluation | null>(null)

  return (
    <EvaluationsContext.Provider
      value={{
        open,
        setOpen,
        currentRow,
        setCurrentRow,
      }}
    >
      {children}
    </EvaluationsContext.Provider>
  )
}

// eslint-disable-next-line react-refresh/only-export-components
export function useEvaluations() {
  const context = useContext(EvaluationsContext)

  if (!context) {
    throw new Error('useEvaluations must be used within EvaluationsProvider')
  }

  return context
}
