import { create } from 'zustand'
import { TaskStatus, TrackInfo } from '../types'

interface TaskState {
  taskId: string | null
  status: 'pending' | 'processing' | 'done' | 'error' | null
  progress: number
  message: string
  error: string | null
  tracks: TrackInfo[]
  logs: string[]
  isProcessing: boolean

  setTaskId: (taskId: string | null) => void
  setStatus: (status: 'pending' | 'processing' | 'done' | 'error' | null) => void
  setProgress: (progress: number) => void
  setMessage: (message: string) => void
  setError: (error: string | null) => void
  setTracks: (tracks: TrackInfo[]) => void
  addLog: (log: string) => void
  setIsProcessing: (isProcessing: boolean) => void
  reset: () => void
}

export const useTaskStore = create<TaskState>((set) => ({
  taskId: null,
  status: null,
  progress: 0,
  message: '',
  error: null,
  tracks: [],
  logs: [],
  isProcessing: false,

  setTaskId: (taskId) => set({ taskId }),
  setStatus: (status) => set({ status }),
  setProgress: (progress) => set({ progress }),
  setMessage: (message) => set({ message }),
  setError: (error) => set({ error }),
  setTracks: (tracks) => set({ tracks }),
  addLog: (log) => set((state) => ({ logs: [...state.logs, log] })),
  setIsProcessing: (isProcessing) => set({ isProcessing }),
  reset: () =>
    set({
      taskId: null,
      status: null,
      progress: 0,
      message: '',
      error: null,
      tracks: [],
      logs: [],
      isProcessing: false,
    }),
}))