import { create } from 'zustand'

interface PlannerStore {
  // Order maps: productId → pbiId[]
  pbiOrder: Record<string, string[]>
  // Order maps: pbiId → storyId[]
  storyOrder: Record<string, string[]>
  // Priority maps: pbiId → priority
  pbiPriority: Record<string, number>

  initPbis: (productId: string, ids: string[]) => void
  reorderPbis: (productId: string, ids: string[]) => void
  rollbackPbis: (productId: string, ids: string[]) => void
  updatePbiPriority: (pbiId: string, priority: number) => void

  initStories: (pbiId: string, ids: string[]) => void
  reorderStories: (pbiId: string, ids: string[]) => void
  rollbackStories: (pbiId: string, ids: string[]) => void
}

export const usePlannerStore = create<PlannerStore>((set) => ({
  pbiOrder: {},
  storyOrder: {},
  pbiPriority: {},

  initPbis: (productId, ids) =>
    set((state) => ({ pbiOrder: { ...state.pbiOrder, [productId]: ids } })),

  reorderPbis: (productId, ids) =>
    set((state) => ({ pbiOrder: { ...state.pbiOrder, [productId]: ids } })),

  rollbackPbis: (productId, ids) =>
    set((state) => ({ pbiOrder: { ...state.pbiOrder, [productId]: ids } })),

  updatePbiPriority: (pbiId, priority) =>
    set((state) => ({ pbiPriority: { ...state.pbiPriority, [pbiId]: priority } })),

  initStories: (pbiId, ids) =>
    set((state) => ({ storyOrder: { ...state.storyOrder, [pbiId]: ids } })),

  reorderStories: (pbiId, ids) =>
    set((state) => ({ storyOrder: { ...state.storyOrder, [pbiId]: ids } })),

  rollbackStories: (pbiId, ids) =>
    set((state) => ({ storyOrder: { ...state.storyOrder, [pbiId]: ids } })),
}))
