<template>
  <div class="card bg-base-100 shadow-sm">
    <div class="card-body space-y-4">
      <DocumentPreviewModal
        :document="previewDocument"
        :visible="previewVisible"
        :documents="previewDocumentList"
        @close="closePreview"
      />

      <div class="flex items-center justify-between">
        <div>
          <h2 class="card-title">Produits associés</h2>
          <p class="text-xs text-gray-500">
            Produits sélectionnés directement pour cette machine.
          </p>
        </div>
        <span class="badge badge-outline" v-if="products.length">
          {{ products.length }} produit{{ products.length > 1 ? 's' : '' }}
        </span>
      </div>

      <div v-if="products.length" class="space-y-3">
        <div
          v-for="product in products"
          :key="product.id || product.linkId || product.name"
          class="rounded border p-3 text-sm space-y-2"
          :class="product.pendingEntity ? 'border-error bg-error/10' : 'border-base-200 bg-base-200/60'"
        >
          <div class="flex items-center justify-between flex-wrap gap-2">
            <p class="font-semibold" :class="product.pendingEntity ? 'text-error' : 'text-base-content'">
              <NuxtLink
                v-if="!isEditMode && !product.pendingEntity && product.id"
                :to="machineId
                  ? { path: `/product/${product.id}`, query: { from: 'machine', machineId } }
                  : `/product/${product.id}`"
                class="hover:underline hover:text-primary transition-colors"
              >
                {{ product.name }}
              </NuxtLink>
              <span v-else>{{ product.name }}</span>
            </p>
            <div class="flex items-center gap-2">
              <button
                v-if="product.pendingEntity"
                type="button"
                class="badge badge-error badge-sm cursor-pointer hover:badge-outline transition-colors"
                title="Cliquer pour associer un item"
                @click="$emit('fill-entity', (product.linkId || product.id) as string, product.modelTypeId as string)"
              >
                À remplir
              </button>
              <span v-if="product.groupLabel" class="badge badge-ghost badge-sm">
                {{ product.groupLabel }}
              </span>
              <button
                v-if="isEditMode"
                type="button"
                class="btn btn-ghost btn-xs text-error"
                title="Supprimer ce produit"
                @click="$emit('remove-product', (product.linkId || product.id) as string)"
              >
                Supprimer
              </button>
            </div>
          </div>
          <p v-if="product.reference" class="text-xs text-base-content/70">
            <span class="font-medium">Référence :</span>
            <span class="ml-1">{{ product.reference }}</span>
          </p>
          <p v-if="product.supplierLabel" class="text-xs text-base-content/70">
            <span class="font-medium">Fournisseurs :</span>
            <span class="ml-1">{{ product.supplierLabel }}</span>
          </p>
          <p v-if="product.priceLabel" class="text-xs text-base-content/70">
            <span class="font-medium">Prix indicatif :</span>
            <span class="ml-1">{{ product.priceLabel }}</span>
          </p>

          <!-- Documents liés au produit -->
          <div v-if="product.documents?.length" class="mt-2 space-y-1">
            <p class="text-xs font-medium text-base-content/70">Documents :</p>
            <div
              v-for="doc in product.documents"
              :key="doc.id || doc.name"
              class="flex items-center justify-between gap-3 rounded border border-base-200 bg-base-100 px-3 py-2"
            >
              <div class="flex items-center gap-3 text-xs">
                <div
                  class="flex-shrink-0 overflow-hidden rounded-md border border-base-200 bg-base-200/70 flex items-center justify-center h-8 w-6"
                >
                  <component
                    :is="documentIcon(doc).component"
                    class="h-4 w-4"
                    :class="documentIcon(doc).colorClass"
                    aria-hidden="true"
                  />
                </div>
                <div>
                  <div class="font-medium text-base-content">{{ doc.name }}</div>
                  <div class="text-xs text-base-content/60">
                    {{ doc.mimeType || 'Inconnu' }} • {{ formatSize(doc.size) }}
                  </div>
                </div>
              </div>
              <div class="flex items-center gap-2 text-xs">
                <button
                  type="button"
                  class="btn btn-ghost btn-xs"
                  :disabled="!canPreviewDocument(doc)"
                  :title="canPreviewDocument(doc) ? 'Consulter le document' : 'Aucun aperçu disponible pour ce type'"
                  @click="openPreview(doc, product.documents || [])"
                >
                  Consulter
                </button>
                <button
                  type="button"
                  class="btn btn-ghost btn-xs"
                  @click="downloadDocument(doc)"
                >
                  Télécharger
                </button>
              </div>
            </div>
          </div>
        </div>
      </div>
      <p v-else class="text-xs text-gray-500">
        Aucun produit n'a été associé directement à cette machine.
      </p>

      <button
        v-if="isEditMode"
        type="button"
        class="btn btn-sm md:btn-md btn-primary"
        @click="$emit('add-product')"
      >
        Ajouter un produit
      </button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import DocumentPreviewModal from '~/components/DocumentPreviewModal.vue'
import { canPreviewDocument } from '~/utils/documentPreview'
import {
  formatSize,
  documentIcon,
  downloadDocument,
} from '~/shared/utils/documentDisplayUtils'

const route = useRoute()
const machineId = computed(() => route.params.id as string | undefined)

defineProps<{
  products: Array<{
    id?: string | null
    linkId?: string | null
    name?: string
    reference?: string | null
    supplierLabel?: string | null
    priceLabel?: string | null
    groupLabel?: string
    pendingEntity?: boolean
    modelTypeId?: string | null
    modelType?: string | null
    documents?: Array<{
      id?: string
      name?: string
      mimeType?: string
      size?: number
      fileUrl?: string
      downloadUrl?: string
    }>
  }>
  isEditMode: boolean
}>()

defineEmits<{
  'add-product': []
  'remove-product': [linkId: string]
  'fill-entity': [linkId: string, modelTypeId: string]
}>()

const previewDocument = ref<any>(null)
const previewVisible = ref(false)
const previewDocumentList = ref<any[]>([])

const openPreview = (doc: any, docs: any[]) => {
  previewDocument.value = doc
  previewDocumentList.value = docs
  previewVisible.value = true
}

const closePreview = () => {
  previewVisible.value = false
  previewDocument.value = null
}
</script>
