<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 pièces</h1>
        <p class="text-sm text-gray-500">
          Consultez les catégories disponibles et instanciez des pièces à partir de leur squelette canonique.
        </p>
      </div>
      <NuxtLink to="/piece-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="!pieceTypeList.length" class="text-sm text-gray-500">
          Aucune catégorie de pièce 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 pieceTypeList"
            :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 une pièce
                </button>
              </div>

              <div class="flex flex-wrap gap-2 text-xs text-gray-500">
                <span class="badge badge-outline">{{ formatPieceStructurePreview(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.name"
                        >
                          <span class="font-medium">{{ field.name }}</span>
                          <span v-if="field.value !== undefined && field.value !== null"> : {{ field.value }}</span>
                        </li>
                      </ul>
                    </div>

                    <p
                      v-if="!getStructureCustomFields(type.structure).length"
                      class="text-xs text-gray-500"
                    >
                      Ce squelette ne définit pas encore de valeurs par défaut spécifiques.
                    </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="getExistingPieces(type).length" class="space-y-2">
                <h3 class="text-sm font-semibold text-base-content">Pièces existantes</h3>
                <ul class="space-y-1 text-sm text-base-content/80">
                  <li
                    v-for="piece in getExistingPieces(type).slice(0, 5)"
                    :key="piece.id"
                  >
                    <span class="font-medium">{{ piece.name || 'Pièce sans nom' }}</span>
                    <span v-if="piece.machine?.name" class="text-xs text-gray-500">
                      · Machine : {{ piece.machine.name }}
                    </span>
                  </li>
                </ul>
                <p
                  v-if="getExistingPieces(type).length > 5"
                  class="text-xs text-gray-500"
                >
                  + {{ getExistingPieces(type).length - 5 }} pièce(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 une pièce
          </h3>
          <p class="text-sm text-gray-500">
            Sélectionnez la machine et le requirement cible puis ajustez les informations d'override avant la création.
          </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">Machine cible</span>
              </label>
              <select
                v-model="creationForm.machineId"
                class="select select-bordered select-sm md:select-md"
                :disabled="machinesLoading || submitting"
                required
              >
                <option value="">Sélectionner une machine</option>
                <option
                  v-for="machine in machines"
                  :key="machine.id"
                  :value="machine.id"
                >
                  {{ machine.name }}
                </option>
              </select>
              <p v-if="machinesLoading" class="text-xs text-gray-500 mt-1">
                Chargement des machines...
              </p>
            </div>

            <div class="form-control">
              <label class="label">
                <span class="label-text">Requirement</span>
              </label>
              <select
                v-model="creationForm.requirementId"
                class="select select-bordered select-sm md:select-md"
                :disabled="requirementLoading || !requirementOptions.length || submitting"
                required
              >
                <option value="">Sélectionner un requirement</option>
                <option
                  v-for="requirement in requirementOptions"
                  :key="requirement.id"
                  :value="requirement.id"
                >
                  {{ resolveRequirementLabel(requirement) }}
                </option>
              </select>
              <p v-if="requirementLoading" class="text-xs text-gray-500 mt-1">
                Chargement des requirements de la machine...
              </p>
              <p
                v-else-if="creationForm.machineId && !requirementOptions.length"
                class="text-xs text-error mt-1"
              >
                Cette machine n'a pas de requirement de pièce configuré.
              </p>
            </div>
          </div>

          <div
            v-if="selectedRequirement"
            class="rounded-lg border border-base-200 bg-base-200/60 p-4 text-xs text-base-content/80 space-y-1"
          >
            <div class="flex flex-wrap gap-2 items-center">
              <span class="font-medium text-sm">Requirement sélectionné :</span>
              <span class="badge badge-outline badge-sm">{{ resolveRequirementLabel(selectedRequirement) }}</span>
            </div>
            <p v-if="selectedRequirement.typePiece?.name" class="text-xs">
              Type attendu : {{ selectedRequirement.typePiece.name }}
            </p>
            <p v-if="selectedRequirement.maxCount !== null && selectedRequirement.maxCount !== undefined" class="text-xs">
              Capacité : {{ selectedRequirement.minCount ?? (selectedRequirement.required ? 1 : 0) }} -
              {{ selectedRequirement.maxCount ?? '∞' }} élément(s)
            </p>
          </div>

          <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <div class="form-control">
              <label class="label">
                <span class="label-text">Nom de la pièce</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 la pièce
          </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, watch } from 'vue'
import ConstructeurSelect from '~/components/ConstructeurSelect.vue'
import { usePieceTypes } from '~/composables/usePieceTypes'
import { useMachines } from '~/composables/useMachines'
import { usePieces } from '~/composables/usePieces'
import { useToast } from '~/composables/useToast'
import { useApi } from '~/composables/useApi'
import { formatPieceStructurePreview, sanitizeDefinitionOverrides } from '~/shared/modelUtils'
import type { PieceModelStructure } from '~/shared/types/inventory'
import type { ModelType } from '~/services/modelTypes'

interface PieceCatalogType extends ModelType {
  structure: PieceModelStructure | null
  customFields?: Array<Record<string, any>>
  pieces?: Array<Record<string, any>>
}

const { pieceTypes, loadPieceTypes, loadingPieceTypes } = usePieceTypes()
const { machines, loadMachines, loading: machinesLoadingRef } = useMachines()
const { createPiece } = usePieces()
const toast = useToast()
const { apiCall } = useApi()

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

const machineRequirementCache = reactive<Record<string, { requirements: any[] }>>({})
const lastSuggestedName = ref('')
let requirementRequestToken = 0

const loadingTypes = computed(() => loadingPieceTypes.value)
const pieceTypeList = computed<PieceCatalogType[]>(() => (pieceTypes.value || []) as PieceCatalogType[])
const machinesLoading = computed(() => machinesLoadingRef.value)

const requirementOptions = computed(() => {
  const machineId = creationForm.machineId
  if (!machineId) {
    return []
  }
  const entry = machineRequirementCache[machineId]
  if (!entry) {
    return []
  }
  return Array.isArray(entry.requirements) ? entry.requirements : []
})

const selectedRequirement = computed(() => {
  return requirementOptions.value.find((requirement: any) => requirement.id === creationForm.requirementId) || null
})

const canSubmit = computed(() => Boolean(creationForm.machineId && creationForm.requirementId && !submitting.value && !requirementLoading.value))

const getCategoryCustomFields = (type: PieceCatalogType) => Array.isArray(type?.customFields) ? type.customFields : []
const getStructureCustomFields = (structure: PieceModelStructure | null) => Array.isArray(structure?.customFields) ? structure.customFields : []
const getExistingPieces = (type: PieceCatalogType) => Array.isArray(type?.pieces) ? type.pieces : []

const resolveRequirementLabel = (requirement: any) => {
  if (!requirement) {
    return 'Requirement sans libellé'
  }
  const parts: string[] = []
  if (requirement.label) {
    parts.push(requirement.label)
  }
  if (requirement.typePiece?.name) {
    parts.push(requirement.typePiece.name)
  }
  return parts.join(' • ') || 'Requirement'
}

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

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

const openCreationModal = async (type: PieceCatalogType) => {
  selectedType.value = type
  resetCreationFormForType()
  creationModalOpen.value = true
  if (!machines.value?.length) {
    await loadMachines()
  }
}

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

const ensureMachineRequirements = async (machineId: string) => {
  if (!machineId || machineRequirementCache[machineId]) {
    return
  }

  const requestId = ++requirementRequestToken
  requirementLoading.value = true
  try {
    const result = await apiCall(`/machines/${machineId}`, { method: 'GET' })
    if (result.success) {
      const requirements = result.data?.typeMachine?.pieceRequirements || []
      machineRequirementCache[machineId] = { requirements }
    }
  } finally {
    if (requestId === requirementRequestToken) {
      requirementLoading.value = false
    }
  }
}

watch(
  () => creationForm.machineId,
  async (machineId) => {
    creationForm.requirementId = ''
    if (!machineId) {
      return
    }
    await ensureMachineRequirements(machineId)
  },
)

watch(
  () => creationForm.requirementId,
  (requirementId) => {
    if (!selectedType.value) {
      return
    }
    const requirement = requirementId ? selectedRequirement.value : null
    const suggestion =
      requirement?.label || requirement?.typePiece?.name || selectedType.value?.name || ''

    if (!creationForm.name || creationForm.name === lastSuggestedName.value) {
      creationForm.name = suggestion
    }
    lastSuggestedName.value = suggestion
  },
)

const submitCreation = async () => {
  if (!creationForm.machineId || !creationForm.requirementId) {
    toast.showError('Sélectionnez une machine et un requirement avant de continuer.')
    return
  }
  if (!selectedType.value) {
    toast.showError('Aucune catégorie sélectionnée.')
    return
  }

  const payload: Record<string, any> = {
    machineId: creationForm.machineId,
    typeMachinePieceRequirementId: creationForm.requirementId,
  }

  const requirement = selectedRequirement.value
  if (selectedType.value.id) {
    const requirementTypeId = requirement?.typePieceId || null
    if (!requirementTypeId || requirementTypeId !== selectedType.value.id) {
      payload.typePieceId = 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 createPiece(payload)
    if (result.success) {
      await loadPieceTypes()
      closeCreationModal()
    } else if (result.error) {
      toast.showError(result.error)
    }
  } catch (error: any) {
    toast.showError(error?.message || 'Erreur lors de la création de la pièce')
  } finally {
    submitting.value = false
  }
}

onMounted(async () => {
  await Promise.allSettled([
    loadPieceTypes(),
    loadMachines(),
  ])
})
</script>
