import { create } from "zustand"
import { persist } from "zustand/middleware"
import { Lesson } from "../components/LessonItem"
import { ParsedFile } from "../components/FileUploader"
// import { ParsedLink } from "../components/LinkUploader"

export type FormState = {
  description: string
  duration: number
  targetAudience: string
  hasContentIndex: boolean
  contentIndex: string
  // sources: ParsedLink[]
  isCompleted: boolean
  variables: string[]
  currentStep: string
  title?: string
  technologies?: string[]
}

type Auth = {
  bcToken: string
  rigoToken: string
  userId: string
  user: any
}
export type Syllabus = {
  lessons: Lesson[]
  courseInfo: FormState
  sources: ParsedFile[]
}

type Consumables = {
  [key: string]: number
}

export type UploadedFile = {
  name: string
  text: string
}
type Store = {
  auth: Auth
  formState: FormState
  setAuth: (auth: Auth) => void
  // syllabus: Syllabus
  planToRedirect: string
  setPlanToRedirect: (planToRedirect: string) => void
  uploadedFiles: ParsedFile[]
  setUploadedFiles: (uploadedFiles: ParsedFile[]) => void

  cleanHistory: () => void
  history: Syllabus[]
  undo: () => void
  push: (syllabus: Syllabus) => void
  // setSyllabus: (syllabus: Partial<Syllabus>) => void
  setFormState: (formState: Partial<FormState>) => void
  consumables: Consumables
  setConsumables: (consumables: Partial<Consumables>) => void
  mode: "student" | "teacher"
  setMode: (mode: "student" | "teacher") => void
}

const useStore = create<Store>()(
  persist(
    (set) => ({
      auth: {
        bcToken: "",
        rigoToken: "",
        userId: "",
        user: null,
      },
      planToRedirect: "learnpack-creator",
      setPlanToRedirect: (planToRedirect: string) => set({ planToRedirect }),
      formState: {
        description: "",
        duration: 0,
        targetAudience: "",
        hasContentIndex: false,
        contentIndex: "",
        // sources: [],
        isCompleted: false,
        currentStep: "description",
        variables: [
          "description",
          "duration",
          "hasContentIndex",
          "verifyHuman",
        ],
      },
      setFormState: (formState: Partial<FormState>) =>
        set((state) => ({ formState: { ...state.formState, ...formState } })),

      setUploadedFiles: (uploadedFiles) =>
        set(() => ({
          uploadedFiles: [...uploadedFiles],
        })),
      uploadedFiles: [],
      history: [],
      push: (syllabus: Syllabus) => {
        set((state) => ({
          history: [...state.history, syllabus],
        }))
      },

      undo: () => {
        set((state) => {
          return {
            history: state.history.slice(0, -1),
          }
        })
      },
      cleanHistory: () => {
        set(() => ({ history: [] }))
      },
      consumables: {},
      setConsumables: (consumables: Partial<Consumables>) =>
        set((state) => {
          const sanitized: Consumables = Object.fromEntries(
            Object.entries(consumables).map(([k, v]) => [k, v ?? 0])
          )
          return {
            consumables: {
              ...state.consumables,
              ...sanitized,
            },
          }
        }),

      setAuth: (auth: Auth) => set({ auth }),
      mode: "student",
      setMode: (mode: "student" | "teacher") => set({ mode }),
    }),
    {
      name: "syllabus-storage",
    }
  )
)

export default useStore
