<template>
  <div :class="containerClass">
    <div class="border border-base-200 rounded-lg bg-base-100 shadow-sm">
      <div class="flex flex-wrap items-start justify-between gap-3 border-b border-base-200 px-4 py-3">
        <div class="flex-1 min-w-[220px] space-y-2">
          <label class="label">
            <span class="label-text text-xs font-semibold">
              {{ isRoot ? 'Famille de composant racine' : 'Famille de composant' }}
            </span>
          </label>
          <template v-if="!lockType">
            <select
              v-model="node.typeComposantId"
              class="select select-bordered select-sm w-full"
              @change="handleComponentTypeSelect(node)"
            >
              <option value="">
                Sélectionner une famille de composant
              </option>
              <option
                v-for="type in componentTypes"
                :key="type.id"
                :value="type.id"
              >
                {{ formatComponentTypeOption(type) }}
              </option>
            </select>
            <p class="text-[11px] text-gray-500">
              {{ node.typeComposantId ? `Sélection : ${getComponentTypeLabel(node.typeComposantId) || 'Inconnue'}` : 'Aucune famille sélectionnée' }}
            </p>
            <div v-if="!isRoot" class="form-control mt-2">
              <label class="label py-1">
                <span class="label-text text-[11px]">Alias (optionnel)</span>
              </label>
              <input
                v-model="node.alias"
                type="text"
                class="input input-bordered input-xs"
                placeholder="Alias du sous-composant"
              />
            </div>
          </template>
          <template v-else>
            <div class="input input-bordered input-sm bg-base-200 flex items-center">
              {{ lockedTypeDisplay }}
            </div>
          </template>
        </div>
        <button
          v-if="!isRoot"
          type="button"
          class="btn btn-error btn-xs btn-square"
          @click="emit('remove')"
        >
          <IconLucideTrash class="w-4 h-4" aria-hidden="true" />
        </button>
      </div>

      <div class="px-4 py-4 space-y-5">
        <section v-if="isRoot" class="space-y-3">
          <div class="flex items-center justify-between gap-2">
            <h4 :class="headingClass">
              {{ isRoot ? 'Champs personnalisés du composant' : 'Champs personnalisés' }}
            </h4>
            <button type="button" class="btn btn-outline btn-xs" @click="addCustomField">
              <IconLucidePlus class="w-3 h-3 mr-2" aria-hidden="true" />
              Ajouter
            </button>
          </div>
          <p v-if="!(node.customFields?.length)" class="text-xs text-gray-500">
            Aucun champ n'a encore été défini.
          </p>
          <div v-else class="space-y-2">
            <div
              v-for="(field, index) in node.customFields"
              :key="`field-${index}`"
              class="border border-base-200 rounded-md p-3 space-y-2"
            >
              <div class="flex items-start justify-between gap-2">
                <div class="flex-1 space-y-2">
                  <div class="grid grid-cols-1 md:grid-cols-2 gap-2">
                    <input
                      v-model="field.name"
                      type="text"
                      class="input input-bordered input-xs"
                      placeholder="Nom du champ"
                    />
                    <select v-model="field.type" class="select select-bordered select-xs">
                      <option value="text">Texte</option>
                      <option value="number">Nombre</option>
                      <option value="select">Liste</option>
                      <option value="boolean">Oui/Non</option>
                      <option value="date">Date</option>
                    </select>
                  </div>
                  <div class="flex items-center gap-2 text-xs">
                    <input v-model="field.required" type="checkbox" class="checkbox checkbox-xs" />
                    Obligatoire
                  </div>
                  <textarea
                    v-if="field.type === 'select'"
                    v-model="field.optionsText"
                    class="textarea textarea-bordered textarea-xs h-20"
                    placeholder="Option 1&#10;Option 2"
                  ></textarea>
                </div>
                <button
                  type="button"
                  class="btn btn-error btn-xs btn-square"
                  @click="removeCustomField(index)"
                >
                  <IconLucideTrash class="w-4 h-4" aria-hidden="true" />
                </button>
              </div>
            </div>
          </div>
        </section>

        <section v-if="isRoot" class="space-y-3">
          <div class="flex items-center justify-between gap-2">
            <h4 :class="headingClass">
              {{ isRoot ? 'Pièces incluses par défaut' : 'Pièces' }}
            </h4>
            <button type="button" class="btn btn-outline btn-xs" @click="addPiece">
              <IconLucidePlus class="w-3 h-3 mr-2" aria-hidden="true" />
              Ajouter
            </button>
          </div>
          <p v-if="!(node.pieces?.length)" class="text-xs text-gray-500">
            Aucune pièce définie.
          </p>
          <div v-else class="space-y-2">
            <div
              v-for="(piece, index) in node.pieces"
              :key="`piece-${index}`"
              class="border border-base-200 rounded-md p-3 space-y-3"
            >
              <div class="flex items-start justify-between gap-2">
                <div class="flex-1 space-y-3">
                  <div class="form-control">
                    <label class="label"><span class="label-text">Famille de pièce</span></label>
                    <div>
                      <select
                        v-model="piece.typePieceId"
                        class="select select-bordered select-xs"
                        @change="handlePieceTypeSelect(piece)"
                      >
                        <option value="">
                          Sélectionner une famille
                        </option>
                        <option
                          v-for="type in pieceTypes"
                          :key="type.id"
                          :value="type.id"
                        >
                          {{ formatPieceTypeOption(type) }}
                        </option>
                      </select>
                    </div>
                    <p class="mt-1 text-[11px] text-gray-500">
                      {{ piece.typePieceId ? `Sélection : ${getPieceTypeLabel(piece.typePieceId) || 'Inconnue'}` : 'Aucune famille sélectionnée' }}
                    </p>
                  </div>
                </div>
                <button type="button" class="btn btn-error btn-xs btn-square" @click="removePiece(index)">
                  <IconLucideTrash class="w-4 h-4" aria-hidden="true" />
                </button>
              </div>
            </div>
          </div>
        </section>

        <section class="space-y-3">
          <div class="flex items-center justify-between gap-2">
            <h4 :class="headingClass">Sous-composants</h4>
            <button type="button" class="btn btn-outline btn-xs" @click="addSubComponent">
              <IconLucidePlus class="w-3 h-3 mr-2" aria-hidden="true" />
              Ajouter
            </button>
          </div>
          <p v-if="!isRoot" class="text-[11px] text-gray-500">
            Sélectionnez uniquement la famille de ce sous-composant ; il sera configuré via son propre modèle.
          </p>
          <p v-if="!(node.subcomponents?.length)" class="text-xs text-gray-500">
            Aucun sous-composant défini.
          </p>
          <div v-else class="space-y-3">
            <StructureNodeEditor
              v-for="(subComponent, index) in node.subcomponents"
              :key="`sub-${index}`"
              :node="subComponent"
              :depth="depth + 1"
              :component-types="componentTypes"
              :piece-types="pieceTypes"
              @remove="removeSubComponent(index)"
            />
          </div>
        </section>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed, watch } from 'vue'
import IconLucidePlus from '~icons/lucide/plus'
import IconLucideTrash from '~icons/lucide/trash'
import type { ComponentModelPiece, ComponentModelStructureNode } from '~/shared/types/inventory'

defineOptions({ name: 'StructureNodeEditor' })

type ModelTypeOption = {
  id: string
  name: string
  code?: string | null
}

type EditableStructureNode = ComponentModelStructureNode & {
  customFields?: any[]
  pieces?: ComponentModelPiece[]
}

const props = withDefaults(defineProps<{
  node: EditableStructureNode
  depth?: number
  componentTypes?: ModelTypeOption[]
  pieceTypes?: ModelTypeOption[]
  isRoot?: boolean
  lockType?: boolean
  lockedTypeLabel?: string
}>(), {
  depth: 0,
  componentTypes: () => [],
  pieceTypes: () => [],
  isRoot: false,
  lockType: false,
  lockedTypeLabel: '',
})

const emit = defineEmits(['remove'])

const componentTypes = computed(() => props.componentTypes ?? [])
const pieceTypes = computed(() => props.pieceTypes ?? [])

const depthClasses = ['', 'ml-4', 'ml-8', 'ml-12', 'ml-16', 'ml-20']
const containerClass = computed(() => {
  const level = Math.max(0, props.depth ?? 0)
  const index = Math.min(level, depthClasses.length - 1)
  return level === 0 ? 'space-y-4' : `${depthClasses[index]} space-y-4`
})

const headingClass = computed(() => (props.isRoot ? 'text-sm font-semibold' : 'text-xs font-semibold'))
const lockedTypeDisplay = computed(() => {
  if (props.lockedTypeLabel) {
    return props.lockedTypeLabel
  }
  return getComponentTypeLabel(props.node?.typeComposantId) || 'Famille non définie'
})

const formatModelTypeOption = (type: ModelTypeOption | undefined | null) => {
  if (!type) return ''
  return type.code ? `${type.name} (${type.code})` : type.name
}

const componentTypeMap = computed(() => {
  const map = new Map<string, ModelTypeOption>()
  componentTypes.value.forEach((type) => {
    if (type && typeof type.id === 'string') {
      map.set(type.id, type)
    }
  })
  return map
})

const pieceTypeMap = computed(() => {
  const map = new Map<string, ModelTypeOption>()
  pieceTypes.value.forEach((type) => {
    if (type && typeof type.id === 'string') {
      map.set(type.id, type)
    }
  })
  return map
})

const getComponentTypeLabel = (id?: string) => {
  if (!id) return ''
  return formatModelTypeOption(componentTypeMap.value.get(id))
}

const getPieceTypeLabel = (id?: string) => {
  if (!id) return ''
  return formatModelTypeOption(pieceTypeMap.value.get(id))
}

const formatComponentTypeOption = (type: ModelTypeOption | undefined | null) =>
  formatModelTypeOption(type)

const formatPieceTypeOption = (type: ModelTypeOption | undefined | null) =>
  formatModelTypeOption(type)

const ensureArray = (key: 'customFields' | 'pieces' | 'subcomponents') => {
  if (!Array.isArray((props.node as any)[key])) {
    if (key === 'subcomponents') {
      props.node.subcomponents = []
    } else {
      (props.node as any)[key] = []
    }
  }
}

const syncComponentType = (component: EditableStructureNode) => {
  if (!component) {
    return
  }
  if (props.lockType && props.isRoot) {
    if (props.lockedTypeLabel) {
      component.typeComposantLabel = props.lockedTypeLabel
      if (!component.alias || component.alias === component.typeComposantLabel) {
        component.alias = props.lockedTypeLabel
      }
    }
    if (component.typeComposantId) {
      const option = componentTypeMap.value.get(component.typeComposantId)
      component.familyCode = option?.code ?? component.familyCode
    }
    return
  }
  const id = typeof component.typeComposantId === 'string'
    ? component.typeComposantId
    : ''

  if (!id) {
    component.typeComposantLabel = ''
    component.familyCode = ''
    return
  }

  const option = componentTypeMap.value.get(id)
  if (!option) {
    component.typeComposantLabel = ''
    component.familyCode = ''
    return
  }

  component.typeComposantLabel = formatModelTypeOption(option)
  component.familyCode = option.code ?? component.familyCode
  if (!component.alias || component.alias === '' || component.alias === lockedTypeDisplay.value) {
    component.alias = option.name || component.typeComposantLabel
  }
}

const updatePieceTypeLabel = (piece: ComponentModelPiece & Record<string, any>) => {
  if (!piece) return

  if (piece.typePieceId) {
    const option = pieceTypeMap.value.get(piece.typePieceId)
    if (option) {
      piece.typePieceLabel = formatPieceTypeOption(option)
      return
    }
  }

  if (piece.typePieceLabel) {
    const normalized = piece.typePieceLabel.trim().toLowerCase()
    if (normalized) {
      const match = pieceTypes.value.find((type) => {
        const formatted = formatPieceTypeOption(type).toLowerCase()
        const name = (type?.name ?? '').toLowerCase()
        const code = (type?.code ?? '').toLowerCase()
        return formatted === normalized || name === normalized || (!!code && code === normalized)
      })
      if (match) {
        piece.typePieceId = match.id
        piece.typePieceLabel = formatPieceTypeOption(match)
        return
      }
    }
  }
}

const syncPieceLabels = (pieces?: any[]) => {
  if (!Array.isArray(pieces)) {
    return
  }
  pieces.forEach((piece) => {
    updatePieceTypeLabel(piece)
  })
}

const handleComponentTypeSelect = (component: any) => {
  syncComponentType(component)
}

const handlePieceTypeSelect = (piece: ComponentModelPiece & Record<string, any>) => {
  if (!piece) {
    return
  }
  const id = typeof piece.typePieceId === 'string' ? piece.typePieceId : ''
  if (!id) {
    piece.typePieceLabel = ''
    return
  }
  const option = pieceTypeMap.value.get(id)
  if (!option) {
    piece.typePieceId = ''
    piece.typePieceLabel = ''
    return
  }
  piece.typePieceLabel = formatPieceTypeOption(option)
}

const addCustomField = () => {
  ensureArray('customFields')
  props.node.customFields.push({
    name: '',
    type: 'text',
    required: false,
    optionsText: '',
    options: [],
  })
}

const removeCustomField = (index: number) => {
  if (!Array.isArray(props.node.customFields)) return
  props.node.customFields.splice(index, 1)
}

const addPiece = () => {
  ensureArray('pieces')
  props.node.pieces.push({
    typePieceId: '',
    typePieceLabel: '',
    reference: '',
  })
}

const removePiece = (index: number) => {
  if (!Array.isArray(props.node.pieces)) return
  props.node.pieces.splice(index, 1)
}

const addSubComponent = () => {
  ensureArray('subcomponents')
  props.node.subcomponents.push({
    typeComposantId: '',
    typeComposantLabel: '',
    modelId: '',
    familyCode: '',
    alias: '',
    subcomponents: [],
  })
}

const removeSubComponent = (index: number) => {
  if (!Array.isArray(props.node.subcomponents)) return
  props.node.subcomponents.splice(index, 1)
}

watch(componentTypes, () => {
  syncComponentType(props.node)
}, { deep: true, immediate: true })

watch(
  () => props.node.typeComposantId,
  () => {
    syncComponentType(props.node)
  },
)

watch(pieceTypes, () => {
  syncPieceLabels(props.node?.pieces)
}, { deep: true, immediate: true })

watch(
  () => props.node.pieces,
  (value) => {
    syncPieceLabels(value)
  },
  { deep: true }
)

watch(
  () => [props.lockedTypeLabel, props.lockType],
  () => {
    if (props.lockType && props.isRoot) {
      const label = props.lockedTypeLabel || lockedTypeDisplay.value
      props.node.typeComposantLabel = label
      if (label && (!props.node.alias || props.node.alias === lockedTypeDisplay.value)) {
        props.node.alias = label
      }
      if (props.node.typeComposantId) {
        const option = componentTypeMap.value.get(props.node.typeComposantId)
        props.node.familyCode = option?.code ?? props.node.familyCode
      }
    }
  },
  { immediate: true }
)
</script>
