import { create } from 'zustand'
import { processRepo, activityRepo, gatewayRepo, resourceRepo } from '@/persistence/repositories'
import type { Process, Activity, GatewayConfig, Resource } from '@/domain/types'

interface ProcessState {
  currentProcess: Process | null
  activities: Activity[]
  gateways: GatewayConfig[]
  resources: Resource[]
  isLoading: boolean
  error: string | null

  // Acciones
  loadProcess: (processId: string) => Promise<void>
  setCurrentProcess: (process: Process) => Promise<void>
  updateProcess: (updates: Partial<Process>) => Promise<void>
  updateActivity: (activity: Activity) => Promise<void>
  updateGateway: (gateway: GatewayConfig) => Promise<void>
  addResource: (resource: Resource) => Promise<void>
  updateResource: (resource: Resource) => Promise<void>
  deleteResource: (resourceId: string) => Promise<void>
  reset: () => void
}

export const useProcessStore = create<ProcessState>((set, get) => ({
  currentProcess: null,
  activities: [],
  gateways: [],
  resources: [],
  isLoading: false,
  error: null,

  loadProcess: async (processId) => {
    set({ isLoading: true, error: null })
    try {
      const [process, activities, gateways, resources] = await Promise.all([
        processRepo.getById(processId),
        activityRepo.getByProcess(processId),
        gatewayRepo.getByProcess(processId),
        resourceRepo.getByProcess(processId),
      ])
      if (!process) throw new Error('Proceso no encontrado')
      set({ currentProcess: process, activities, gateways, resources, isLoading: false })
    } catch (err) {
      set({ error: err instanceof Error ? err.message : 'Error desconocido', isLoading: false })
    }
  },

  setCurrentProcess: async (process) => {
    await processRepo.save(process)
    const [activities, gateways, resources] = await Promise.all([
      activityRepo.getByProcess(process.id),
      gatewayRepo.getByProcess(process.id),
      resourceRepo.getByProcess(process.id),
    ])
    set({ currentProcess: process, activities, gateways, resources })
  },

  updateProcess: async (updates) => {
    const current = get().currentProcess
    if (!current) return
    const updated: Process = { ...current, ...updates, updatedAt: Date.now() }
    await processRepo.save(updated)
    set({ currentProcess: updated })
  },

  updateActivity: async (activity) => {
    await activityRepo.save(activity)
    set((state) => ({
      activities: state.activities.map((a) => (a.id === activity.id ? activity : a)),
    }))
  },

  updateGateway: async (gateway) => {
    await gatewayRepo.save(gateway)
    set((state) => ({
      gateways: state.gateways.map((g) => (g.id === gateway.id ? gateway : g)),
    }))
  },

  addResource: async (resource) => {
    await resourceRepo.save(resource)
    set((state) => ({ resources: [...state.resources, resource] }))
  },

  updateResource: async (resource) => {
    await resourceRepo.save(resource)
    set((state) => ({
      resources: state.resources.map((r) => (r.id === resource.id ? resource : r)),
    }))
  },

  deleteResource: async (resourceId) => {
    await resourceRepo.delete(resourceId)
    set((state) => ({ resources: state.resources.filter((r) => r.id !== resourceId) }))
  },

  reset: () => set({ currentProcess: null, activities: [], gateways: [], resources: [], error: null }),
}))
