import type { Operation } from './usePrintQueue'

export interface TemplateVariable {
  name: string
  label?: string
  defaultValue?: string
}

export interface PrintTemplate {
  id: string
  name: string
  description?: string
  operations: Operation[]
  variables: TemplateVariable[]
  createdAt: string
  updatedAt: string
}

// Regex para detectar variables en templates
const VARIABLE_REGEX = /\{\{(\w+)(?::([^:}]+))?(?::([^}]+))?\}\}/g

// Resolver variables en operaciones
export function resolveVariables(
  operations: Operation[],
  values: Record<string, string>
): Operation[] {
  return JSON.parse(JSON.stringify(operations)).map((op: Operation) => {
    for (const [key, val] of Object.entries(op)) {
      if (typeof val === 'string') {
        // Reemplazar {{nombre:label:default}} con el valor proporcionado
        op[key] = val.replace(VARIABLE_REGEX, (_, name) => values[name] ?? '')
      }
    }
    return op
  })
}

export function useTemplates() {
  const templates = useState<PrintTemplate[]>('templates', () => [])
  const loading = useState('templatesLoading', () => false)
  const error = useState<string | null>('templatesError', () => null)

  // Cargar templates del servidor
  async function fetchTemplates(): Promise<void> {
    loading.value = true
    error.value = null
    try {
      const data = await $fetch<PrintTemplate[]>('/api/templates')
      templates.value = data
    } catch (e) {
      console.error('Error fetching templates:', e)
      error.value = 'Error al cargar templates'
    } finally {
      loading.value = false
    }
  }

  // Guardar nuevo template
  async function saveTemplate(
    name: string,
    description: string,
    operations: Operation[]
  ): Promise<PrintTemplate | null> {
    error.value = null
    try {
      const template = await $fetch<PrintTemplate>('/api/templates', {
        method: 'POST',
        body: {
          name,
          description,
          operations
        }
      })
      templates.value = [...templates.value, template]
      return template
    } catch (e) {
      console.error('Error saving template:', e)
      error.value = 'Error al guardar template'
      return null
    }
  }

  // Actualizar template existente
  async function updateTemplate(
    id: string,
    updates: Partial<{ name: string; description: string; operations: Operation[] }>
  ): Promise<PrintTemplate | null> {
    error.value = null
    try {
      const template = await $fetch<PrintTemplate>(`/api/templates/${id}`, {
        method: 'PUT',
        body: updates
      })
      templates.value = templates.value.map(t => t.id === id ? template : t)
      return template
    } catch (e) {
      console.error('Error updating template:', e)
      error.value = 'Error al actualizar template'
      return null
    }
  }

  // Eliminar template
  async function deleteTemplate(id: string): Promise<boolean> {
    error.value = null
    try {
      await $fetch(`/api/templates/${id}`, { method: 'DELETE' })
      templates.value = templates.value.filter(t => t.id !== id)
      return true
    } catch (e) {
      console.error('Error deleting template:', e)
      error.value = 'Error al eliminar template'
      return false
    }
  }

  // Cargar operaciones de un template
  function loadTemplate(id: string): Operation[] | null {
    const template = templates.value.find(t => t.id === id)
    return template ? JSON.parse(JSON.stringify(template.operations)) : null
  }

  // Duplicar template
  async function duplicateTemplate(id: string): Promise<PrintTemplate | null> {
    error.value = null
    try {
      const template = await $fetch<PrintTemplate>(`/api/templates/${id}/duplicate`, {
        method: 'POST'
      })
      templates.value = [...templates.value, template]
      return template
    } catch (e) {
      console.error('Error duplicating template:', e)
      error.value = 'Error al duplicar template'
      return null
    }
  }

  return {
    templates: readonly(templates),
    loading: readonly(loading),
    error: readonly(error),
    fetchTemplates,
    saveTemplate,
    updateTemplate,
    deleteTemplate,
    loadTemplate,
    duplicateTemplate,
    resolveVariables
  }
}
