<template>
  <div>
    <DocumentPreviewModal
      :document="previewDocument"
      :visible="previewVisible"
      :documents="componentDocuments"
      @close="closePreview"
    />
    <main class="container mx-auto px-6 py-10">
    <div v-if="loading" class="flex flex-col items-center gap-4 py-20 text-center">
      <span class="loading loading-spinner loading-lg" aria-hidden="true" />
      <p class="text-sm text-base-content/70">Chargement du composant…</p>
    </div>

    <div v-else-if="!component" class="max-w-xl mx-auto">
      <div class="alert alert-error shadow-lg">
        <div>
          <h2 class="font-semibold text-lg">Composant introuvable</h2>
          <p class="text-sm text-base-content/80">
            Nous n'avons pas pu retrouver le composant demandé. Il a peut-être été supprimé.
          </p>
        </div>
      </div>
      <button type="button" class="btn btn-primary mt-6" @click="$router.back()">
        Retour au catalogue
      </button>
    </div>

    <section v-else class="card border border-base-200 bg-base-100 shadow-sm max-w-5xl mx-auto">
      <div class="card-body space-y-6">
        <header class="flex flex-col gap-2 md:flex-row md:items-center md:justify-between">
          <div>
            <h1 class="text-3xl font-semibold text-base-content">Modifier le composant</h1>
            <p class="text-sm text-base-content/70">
              Mettez à jour les informations du composant et ses champs personnalisés.
            </p>
          </div>
          <button type="button" class="btn btn-ghost btn-sm md:btn-md self-start" @click="$router.back()">
            Retour au catalogue
          </button>
        </header>

        <div class="grid grid-cols-1 gap-4 md:grid-cols-2">
          <div class="form-control">
            <label class="label">
              <span class="label-text">Catégorie de composant</span>
            </label>
            <select
              v-model="selectedTypeId"
              class="select select-bordered select-sm md:select-md"
              disabled
            >
              <option value="">Sélectionner une catégorie</option>
              <option
                v-for="type in componentTypeList"
                :key="type.id"
                :value="type.id"
              >
                {{ type.name }}
              </option>
            </select>
            <p class="text-xs text-base-content/60 mt-1">
              La catégorie d'origine ne peut pas être modifiée depuis cette page.
            </p>
          </div>
        </div>

        <div class="grid grid-cols-1 gap-4 md:grid-cols-2">
          <div class="form-control">
            <label class="label">
              <span class="label-text">Nom du composant</span>
            </label>
            <input
              v-model="editionForm.name"
              type="text"
              class="input input-bordered input-sm md:input-md"
              :disabled="!canEdit || saving"
              placeholder="Nom affiché dans le catalogue"
              required
            >
          </div>
        </div>

        <div class="form-control">
          <label class="label">
            <span class="label-text">Description</span>
          </label>
          <textarea
            v-model="editionForm.description"
            class="textarea textarea-bordered textarea-sm md:textarea-md"
            :disabled="!canEdit || saving"
            placeholder="Description du composant (optionnel)"
            rows="3"
          />
        </div>

        <div class="grid grid-cols-1 gap-4 md:grid-cols-2">
          <div class="form-control">
            <label class="label">
              <span class="label-text">Référence</span>
            </label>
            <input
              v-model="editionForm.reference"
              type="text"
              class="input input-bordered input-sm md:input-md"
              :disabled="!canEdit || saving"
              placeholder="Référence interne ou fournisseur"
            >
          </div>

          <div class="form-control">
            <label class="label">
              <span class="label-text">Fournisseur</span>
            </label>
            <ConstructeurSelect
              v-model="editionForm.constructeurIds"
              class="w-full"
              :disabled="!canEdit || saving"
              placeholder="Rechercher un ou plusieurs fournisseurs..."
              :initial-options="component?.constructeurs || []"
            />
          </div>
        </div>

        <div class="grid grid-cols-1 gap-4 md:grid-cols-2">
          <div class="form-control">
            <label class="label">
              <span class="label-text">Prix indicatif (€)</span>
            </label>
            <input
              v-model="editionForm.prix"
              type="number"
              step="0.01"
              min="0"
              class="input input-bordered input-sm md:input-md"
              :disabled="!canEdit || saving"
              placeholder="Valeur indicatrice"
            >
          </div>
        </div>

        <StructureSkeletonPreview
          v-if="selectedType"
          :structure="selectedTypeStructure"
          :description="selectedType.description || 'Ce squelette définit la structure et les contraintes du composant.'"
          :preview-badge="formatStructurePreview(selectedTypeStructure)"
          variant="component"
          show-empty-state
          :resolve-piece-label="resolvePieceLabel"
          :resolve-product-label="resolveProductLabel"
          :resolve-subcomponent-label="resolveSubcomponentLabel"
        />

        <div
          v-if="structureSelections.hasAny"
          class="space-y-3 rounded-lg border border-base-200 bg-base-200/30 p-4"
        >
          <header class="space-y-1">
            <h2 class="font-semibold text-base-content">Sélections actuelles</h2>
            <p class="text-xs text-base-content/70">
              Voici les pièces, produits et sous-composants réellement choisis pour ce composant.
            </p>
          </header>

          <div class="grid grid-cols-1 gap-4 md:grid-cols-2">
            <div v-if="structureSelections.pieces.length" class="space-y-2">
              <h3 class="font-semibold text-sm text-base-content">Pièces choisies</h3>
              <ul class="list-disc list-inside space-y-1 text-sm">
                <li v-for="entry in structureSelections.pieces" :key="`selected-piece-${entry.path}-${entry.id}`" class="flex items-center gap-2">
                  <span class="font-medium">{{ entry.resolvedName }}</span>
                  <span v-if="(entry.quantity ?? 1) > 1" class="text-sm text-base-content/60">×{{ entry.quantity }}</span>
                  <span class="text-xs text-base-content/70"> — {{ entry.requirementLabel }}</span>
                  <input
                    v-if="canEdit && entry._definition"
                    v-model.number="entry._definition.quantity"
                    type="number"
                    :min="1"
                    step="1"
                    placeholder="Qté"
                    class="input input-bordered input-xs w-16 ml-auto"
                    @input="entry._definition.quantity = Math.max(1, entry._definition.quantity || 1)"
                    @blur="saveSlotQuantity(entry)"
                  />
                </li>
              </ul>
            </div>

            <div v-if="structureSelections.products.length" class="space-y-2">
              <h3 class="font-semibold text-sm text-base-content">Produits choisis</h3>
              <ul class="list-disc list-inside space-y-1 text-sm">
                <li v-for="entry in structureSelections.products" :key="`selected-product-${entry.path}-${entry.id}`">
                  <span class="font-medium">{{ entry.resolvedName }}</span>
                  <span class="text-xs text-base-content/70"> — {{ entry.requirementLabel }}</span>
                </li>
              </ul>
            </div>

            <div v-if="structureSelections.components.length" class="space-y-2">
              <h3 class="font-semibold text-sm text-base-content">Sous-composants choisis</h3>
              <ul class="list-disc list-inside space-y-1 text-sm">
                <li v-for="entry in structureSelections.components" :key="`selected-component-${entry.path}-${entry.id}`">
                  <span class="font-medium">{{ entry.resolvedName }}</span>
                  <span class="text-xs text-base-content/70"> — {{ entry.requirementLabel }}</span>
                </li>
              </ul>
            </div>
          </div>
        </div>

        <div v-if="customFieldInputs.length" class="space-y-4 rounded-lg border border-base-200 bg-base-200/40 p-4">
          <header class="space-y-1">
            <h2 class="font-semibold text-base-content">Champs personnalisés</h2>
            <p class="text-xs text-base-content/70">
              Mettez à jour les valeurs propres à ce composant.
            </p>
          </header>
          <CustomFieldInputGrid :fields="customFieldInputs" :disabled="!canEdit || saving" />
        </div>

        <div class="space-y-4 rounded-lg border border-base-200 bg-base-200/40 p-4">
          <header class="flex flex-col gap-1 md:flex-row md:items-center md:justify-between">
            <div>
              <h2 class="font-semibold text-base-content">Documents</h2>
              <p class="text-xs text-base-content/70">
                Gérez les documents associés à ce composant.
              </p>
            </div>
            <span v-if="selectedFiles.length" class="badge badge-outline">
              {{ selectedFiles.length }} document{{ selectedFiles.length > 1 ? 's' : '' }} prêt{{ selectedFiles.length > 1 ? 's' : '' }} à être ajouté{{ selectedFiles.length > 1 ? 's' : '' }}
            </span>
          </header>
          <div :class="{ 'pointer-events-none opacity-60': !canEdit || saving || uploadingDocuments }">
            <DocumentUpload
              v-model="selectedFiles"
              title="Déposer vos fichiers"
              subtitle="Formats acceptés : PDF, images, documents…"
              @files-added="handleFilesAdded"
            />
          </div>
          <p v-if="uploadingDocuments" class="text-xs text-base-content/70">
            Téléversement des documents en cours…
          </p>
          <p v-else-if="loadingDocuments" class="text-xs text-base-content/70">
            Chargement des documents en cours…
          </p>
          <DocumentListInline
            v-else
            :documents="componentDocuments"
            :can-delete="canEdit"
            :delete-disabled="uploadingDocuments"
            empty-text="Aucun document n'est associé à ce composant pour le moment."
            @preview="openPreview"
            @delete="removeDocument"
          />
        </div>

        <EntityHistorySection
          :entries="history"
          :loading="historyLoading"
          :error="historyError"
          :field-labels="historyFieldLabels"
        />

        <div class="flex flex-col gap-3 md:flex-row md:justify-end">
          <NuxtLink to="/component-catalog" class="btn btn-ghost" :class="{ 'btn-disabled': saving }">
            Annuler
          </NuxtLink>
          <button type="button" class="btn btn-primary" :disabled="!canSubmit" @click="submitEdition">
            <span v-if="saving" class="loading loading-spinner loading-sm mr-2" />
            Enregistrer les modifications
          </button>
        </div>

        <!-- Comments -->
        <div class="mt-4">
          <CommentSection
            entity-type="composant"
            :entity-id="String(route.params.id)"
            :entity-name="component?.name"
            show-resolved
          />
        </div>
      </div>
    </section>
    </main>
  </div>
</template>

<script setup lang="ts">
import { useRoute } from '#imports'
import { useComponentEdit } from '~/composables/useComponentEdit'

const route = useRoute()

const {
  component,
  loading,
  saving,
  selectedFiles,
  uploadingDocuments,
  loadingDocuments,
  componentDocuments,
  previewDocument,
  previewVisible,
  selectedTypeId,
  editionForm,
  customFieldInputs,
  historyFieldLabels,
  canEdit,
  canSubmit,
  componentTypeList,
  selectedType,
  selectedTypeStructure,
  structureSelections,
  history,
  historyLoading,
  historyError,
  openPreview,
  closePreview,
  removeDocument,
  handleFilesAdded,
  submitEdition,
  saveSlotQuantity,
  resolvePieceLabel,
  resolveProductLabel,
  resolveSubcomponentLabel,
  formatStructurePreview,
} = useComponentEdit(String(route.params.id))
</script>
