<template>
  <main class="container mx-auto px-6 py-10 space-y-8">
    <header class="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
      <div>
        <h1 class="text-3xl font-semibold text-base-content">Catalogue des composants</h1>
        <p class="text-sm text-gray-500">
          Consultez les catégories disponibles et instanciez des composants à partir de leur squelette canonique.
        </p>
      </div>
      <NuxtLink to="/component-category" class="btn btn-outline btn-sm md:btn-md self-start">
        Gérer les catégories
      </NuxtLink>
    </header>

    <section>
      <div v-if="loadingTypes" class="flex justify-center py-16">
        <span class="loading loading-spinner loading-lg" aria-hidden="true" />
      </div>

      <template v-else>
        <p v-if="!componentTypeList.length" class="text-sm text-gray-500">
          Aucune catégorie de composant n'est disponible pour le moment. Créez des catégories dans la gestion dédiée pour définir
          vos squelettes.
        </p>

        <div v-else class="grid gap-6 lg:grid-cols-2">
          <article
            v-for="type in componentTypeList"
            :key="type.id"
            class="card bg-base-100 border border-base-200 shadow-sm"
          >
            <div class="card-body space-y-4">
              <div class="flex flex-col gap-3 md:flex-row md:items-start md:justify-between">
                <div class="space-y-1">
                  <h2 class="card-title text-xl">{{ type.name }}</h2>
                  <p v-if="type.description" class="text-sm text-gray-500">
                    {{ type.description }}
                  </p>
                </div>

                <button type="button" class="btn btn-primary btn-sm md:btn-md self-start" @click="openCreationModal(type)">
                  Instancier un composant
                </button>
              </div>

              <div class="flex flex-wrap gap-2 text-xs text-gray-500">
                <span class="badge badge-outline">{{ formatStructurePreview(type.structure) }}</span>
                <span v-if="getCategoryCustomFields(type).length" class="badge badge-outline">
                  {{ getCategoryCustomFields(type).length }} champ(s) personnalisés
                </span>
              </div>

              <div v-if="type.structure" class="space-y-3">
                <details class="collapse collapse-arrow bg-base-200/60">
                  <summary class="collapse-title text-sm font-medium">
                    Détails du squelette
                  </summary>
                  <div class="collapse-content space-y-3 text-sm text-base-content/80">
                    <div v-if="getStructureCustomFields(type.structure).length" class="space-y-1">
                      <h3 class="font-semibold text-sm text-base-content">Valeurs par défaut</h3>
                      <ul class="list-disc list-inside space-y-1">
                        <li
                          v-for="field in getStructureCustomFields(type.structure)"
                          :key="field.key || field.name"
                        >
                          <span class="font-medium">{{ field.key || field.name }}</span>
                          <span v-if="field.value !== undefined && field.value !== null"> : {{ field.value }}</span>
                        </li>
                      </ul>
                    </div>

                    <div v-if="getStructurePieces(type.structure).length" class="space-y-1">
                      <h3 class="font-semibold text-sm text-base-content">Pièces imposées</h3>
                      <ul class="list-disc list-inside space-y-1">
                        <li
                          v-for="(piece, index) in getStructurePieces(type.structure)"
                          :key="piece.role || piece.typePieceId || piece.familyCode || index"
                        >
                          {{ resolvePieceLabel(piece) }}
                        </li>
                      </ul>
                    </div>

                    <div v-if="getStructureSubcomponents(type.structure).length" class="space-y-1">
                      <h3 class="font-semibold text-sm text-base-content">Sous-composants</h3>
                      <ul class="list-disc list-inside space-y-1">
                        <li
                          v-for="(subcomponent, index) in getStructureSubcomponents(type.structure)"
                          :key="subcomponent.alias || subcomponent.typeComposantId || subcomponent.familyCode || index"
                        >
                          {{ resolveSubcomponentLabel(subcomponent) }}
                        </li>
                      </ul>
                    </div>

                    <p
                      v-if="!getStructureCustomFields(type.structure).length && !getStructurePieces(type.structure).length && !getStructureSubcomponents(type.structure).length"
                      class="text-xs text-gray-500"
                    >
                      Ce squelette ne définit pas encore de pièces, sous-composants ou valeurs par défaut.
                    </p>
                  </div>
                </details>
              </div>

              <div v-if="getCategoryCustomFields(type).length" class="space-y-2">
                <h3 class="text-sm font-semibold text-base-content">Champs personnalisés de la catégorie</h3>
                <ul class="space-y-1 text-sm text-base-content/80">
                  <li
                    v-for="field in getCategoryCustomFields(type)"
                    :key="field.id || field.name"
                    class="flex flex-wrap gap-2 items-center"
                  >
                    <span class="font-medium">{{ field.name }}</span>
                    <span class="badge badge-outline badge-xs">{{ field.type || 'text' }}</span>
                    <span v-if="field.required" class="badge badge-outline badge-xs badge-error">Obligatoire</span>
                  </li>
                </ul>
              </div>

              <div v-if="getExistingComponents(type).length" class="space-y-2">
                <h3 class="text-sm font-semibold text-base-content">Composants existants</h3>
                <ul class="space-y-1 text-sm text-base-content/80">
                  <li
                    v-for="component in getExistingComponents(type).slice(0, 5)"
                    :key="component.id"
                  >
                    <span class="font-medium">{{ component.name || 'Composant sans nom' }}</span>
                    <span v-if="component.machine?.name" class="text-xs text-gray-500">
                      · Machine : {{ component.machine.name }}
                    </span>
                  </li>
                </ul>
                <p
                  v-if="getExistingComponents(type).length > 5"
                  class="text-xs text-gray-500"
                >
                  + {{ getExistingComponents(type).length - 5 }} composant(s) supplémentaires.
                </p>
              </div>
            </div>
          </article>
        </div>
      </template>
    </section>
  </main>

  <Teleport to="body">
    <div v-if="creationModalOpen" class="modal modal-open">
      <div class="modal-box max-w-3xl space-y-6">
        <div class="space-y-1">
          <h3 class="text-lg font-semibold">
            Instancier un composant
          </h3>
          <p class="text-sm text-gray-500">
            Renseignez les informations du composant instancié à partir du squelette de la catégorie sélectionnée.
          </p>
          <p v-if="selectedType" class="badge badge-outline badge-sm">
            Catégorie : {{ selectedType.name }}
          </p>
        </div>

        <form class="space-y-4" @submit.prevent="submitCreation">
          <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <div class="form-control">
              <label class="label">
                <span class="label-text">Nom du composant</span>
              </label>
              <input
                v-model="creationForm.name"
                type="text"
                class="input input-bordered input-sm md:input-md"
                :disabled="submitting"
                placeholder="Nom affiché après instanciation"
              >
            </div>

            <div class="form-control">
              <label class="label">
                <span class="label-text">Référence</span>
              </label>
              <input
                v-model="creationForm.reference"
                type="text"
                class="input input-bordered input-sm md:input-md"
                :disabled="submitting"
                placeholder="Référence interne ou constructeur"
              >
            </div>

            <div class="form-control">
              <label class="label">
                <span class="label-text">Constructeur</span>
              </label>
              <ConstructeurSelect
                v-model="creationForm.constructeurId"
                class="w-full"
                :disabled="submitting"
                placeholder="Rechercher un constructeur..."
              />
            </div>

            <div class="form-control">
              <label class="label">
                <span class="label-text">Prix</span>
              </label>
              <input
                v-model="creationForm.prix"
                type="number"
                step="0.01"
                min="0"
                class="input input-bordered input-sm md:input-md"
                :disabled="submitting"
                placeholder="Valeur indicative (€)"
              >
            </div>
          </div>
        </form>

        <div class="modal-action">
          <button type="button" class="btn btn-ghost" :disabled="submitting" @click="closeCreationModal">
            Annuler
          </button>
          <button
            type="button"
            class="btn btn-primary"
            :disabled="!canSubmit"
            @click="submitCreation"
          >
            <span v-if="submitting" class="loading loading-spinner loading-sm mr-2"></span>
            Créer le composant
          </button>
        </div>
      </div>
      <button class="modal-backdrop" aria-label="Fermer" @click="closeCreationModal"></button>
    </div>
  </Teleport>
</template>

<script setup lang="ts">
import { computed, onMounted, reactive, ref } from 'vue'
import ConstructeurSelect from '~/components/ConstructeurSelect.vue'
import { useComponentTypes } from '~/composables/useComponentTypes'
import { useComposants } from '~/composables/useComposants'
import { useToast } from '~/composables/useToast'
import { formatStructurePreview, sanitizeDefinitionOverrides } from '~/shared/modelUtils'
import type { ComponentModelStructure } from '~/shared/types/inventory'
import type { ModelType } from '~/services/modelTypes'

interface ComponentCatalogType extends ModelType {
  structure: ComponentModelStructure | null
  customFields?: Array<Record<string, any>>
  components?: Array<Record<string, any>>
}

const { componentTypes, loadComponentTypes, loadingComponentTypes } = useComponentTypes()
const { createComposant } = useComposants()
const toast = useToast()

const creationModalOpen = ref(false)
const selectedType = ref<ComponentCatalogType | null>(null)
const submitting = ref(false)
const creationForm = reactive({
  name: '' as string,
  reference: '' as string,
  constructeurId: null as string | null,
  prix: '' as string,
})

const loadingTypes = computed(() => loadingComponentTypes.value)
const componentTypeList = computed<ComponentCatalogType[]>(() =>
  (componentTypes.value || [])
    .filter((item: any) => item?.category === 'COMPONENT') as ComponentCatalogType[],
)

const canSubmit = computed(() => Boolean(selectedType.value && !submitting.value))

const getCategoryCustomFields = (type: ComponentCatalogType) => {
  return Array.isArray(type?.customFields) ? type.customFields : []
}

const getStructureCustomFields = (structure: ComponentModelStructure | null) => {
  return Array.isArray(structure?.customFields) ? structure.customFields : []
}

const getStructurePieces = (structure: ComponentModelStructure | null) => {
  return Array.isArray(structure?.pieces) ? structure.pieces : []
}

const getStructureSubcomponents = (structure: ComponentModelStructure | null) => {
  if (Array.isArray(structure?.subcomponents)) {
    return structure.subcomponents
  }
  const legacy = (structure as any)?.subComponents
  return Array.isArray(legacy) ? legacy : []
}

const getExistingComponents = (type: ComponentCatalogType) => {
  return Array.isArray(type?.components) ? type.components : []
}

const resolvePieceLabel = (piece: Record<string, any>) => {
  const parts: string[] = []
  if (piece.role) {
    parts.push(piece.role)
  }
  if (piece.typePiece?.name) {
    parts.push(piece.typePiece.name)
  } else if (piece.typePieceLabel) {
    parts.push(piece.typePieceLabel)
  } else if (piece.familyCode) {
    parts.push(piece.familyCode)
  } else if (piece.typePieceId) {
    parts.push(`#${piece.typePieceId}`)
  }
  return parts.length ? parts.join(' • ') : 'Pièce'
}

const resolveSubcomponentLabel = (node: Record<string, any>) => {
  const parts: string[] = []
  if (node.alias) {
    parts.push(node.alias)
  }
  if (node.typeComposant?.name) {
    parts.push(node.typeComposant.name)
  } else if (node.typeComposantLabel) {
    parts.push(node.typeComposantLabel)
  } else if (node.familyCode) {
    parts.push(node.familyCode)
  } else if (node.typeComposantId) {
    parts.push(`#${node.typeComposantId}`)
  }

  const childCount = Array.isArray(node.subcomponents)
    ? node.subcomponents.length
    : Array.isArray(node.subComponents)
      ? node.subComponents.length
      : 0
  if (childCount) {
    parts.push(`${childCount} sous-composant(s)`)
  }
  return parts.length ? parts.join(' • ') : 'Sous-composant'
}

const clearCreationForm = () => {
  creationForm.name = ''
  creationForm.reference = ''
  creationForm.constructeurId = null
  creationForm.prix = ''
}

const resetCreationFormForType = () => {
  clearCreationForm()
  creationForm.name = selectedType.value?.name || ''
}

const openCreationModal = async (type: ComponentCatalogType) => {
  selectedType.value = type
  resetCreationFormForType()
  creationModalOpen.value = true
}

const closeCreationModal = () => {
  creationModalOpen.value = false
  selectedType.value = null
  clearCreationForm()
}

const submitCreation = async () => {
  if (!selectedType.value) {
    toast.showError('Aucune catégorie sélectionnée.')
    return
  }

  const payload: Record<string, any> = {
    typeComposantId: selectedType.value.id,
  }

  const overrides = sanitizeDefinitionOverrides({
    name: creationForm.name,
    reference: creationForm.reference,
    constructeurId: creationForm.constructeurId,
    prix: creationForm.prix,
  })

  if (overrides) {
    payload.definition = overrides
  }

  submitting.value = true
  try {
    const result = await createComposant(payload)
    if (result.success) {
      await loadComponentTypes()
      closeCreationModal()
    } else if (result.error) {
      toast.showError(result.error)
    }
  } catch (error: any) {
    toast.showError(error?.message || "Erreur lors de la création du composant")
  } finally {
    submitting.value = false
  }
}

onMounted(async () => {
  await loadComponentTypes()
})
</script>
