<template>
  <div class="catering-details">
    <!-- Sección 2: Información del Servicio -->
    <div class="section">
      <h3>Información del Servicio</h3>
      <div class="detail-row" v-if="service.catering && service.catering.tipo_entrega">
        <i class="bi bi-truck"></i>
        <strong>Tipo de entrega:</strong>
        <span>{{ formatServiceIncludeKey(service.catering.tipo_entrega) }}</span>
      </div>
      <div class="detail-row" v-if="service.catering && service.catering.tipo_catering">
        <img :src="iconCatering" class="icon-catering" alt="Icono catering">
        <strong>Tipo de catering:</strong>
        <span>{{ formatServiceIncludeKey(service.catering.tipo_catering) }}</span>
      </div>
      <div class="detail-row">
        <i class="bi bi-people"></i>
        <strong>Comensales:</strong>
        <span>{{ service.catering ? `${service.catering.min_comensales} - ${service.catering.max_comensales} personas` : '-' }}</span>
      </div>
      <div class="detail-row">
        <i class="bi bi-clock"></i>
        <strong>Duración:</strong>
        <span>{{ service.catering ? `${service.catering.duracion} horas` : '-' }}</span>
      </div>
    </div>
    <!-- Sección 1: Detalles del Servicio -->
    <div class="section" v-if="service.catering && (service.catering.menu || service.catering.articulo)">
      <h3>Detalles del Servicio</h3>
      <!-- Menú -->
      <div v-if="service.catering && service.catering.menu">
        <div class="detail-row">
          <i class="bi bi-menu-app"></i>
          <strong>Menú:</strong>
          <span>{{ service.catering.menu.type_menu }}</span>
        </div>
        <div class="detail-row">
          <i class="bi bi-card-text"></i>
          <strong>Nombre:</strong>
          <span>{{ service.catering.menu.nombre }}</span>
        </div>
        <div class="detail-row">
          <i class="bi bi-info-circle"></i>
          <strong>Descripción:</strong>
          <span>{{ service.catering.menu.descripcion }}</span>
        </div>
        <div class="detail-row" v-if="service.catering.menu.platos && service.catering.menu.platos.length">
          <i class="bi bi-list-check"></i>
          <strong>Platos incluidos:</strong>
        </div>
        <div class="platos-list" v-if="service.catering.menu.platos && service.catering.menu.platos.length">
          <span v-for="(plato, idx) in service.catering.menu.platos" :key="idx" class="tag">{{ plato }}</span>
        </div>
        <div class="detail-row" v-if="service.catering && service.catering.alergenos && service.catering.alergenos.length">
          <i class="bi bi-exclamation-triangle"></i>
          <strong>Alérgenos:</strong>
        </div>
        <div class="alergenos-list" v-if="service.catering && service.catering.alergenos && service.catering.alergenos.length">
          <span v-for="(alergeno, idx) in service.catering.alergenos" :key="idx" class="tag warning">{{ alergeno }}</span>
        </div>
        <div class="detail-row" v-if="service.catering.menu.opciones_especiales && service.catering.menu.opciones_especiales.length">
          <i class="bi bi-star"></i>
          <strong>Opciones especiales:</strong>
        </div>
        <div class="opciones-list" v-if="service.catering.menu.opciones_especiales && service.catering.menu.opciones_especiales.length">
          <span v-for="(opcion, idx) in service.catering.menu.opciones_especiales" :key="idx" class="tag special">{{ opcion }}</span>
        </div>
        <div class="detail-row" v-if="service.catering && service.catering.caracteristicas && service.catering.caracteristicas.length">
          <i class="bi bi-stars"></i>
          <strong>Características:</strong>
        </div>
        <div class="caracteristicas-list" v-if="service.catering && service.catering.caracteristicas && service.catering.caracteristicas.length">
          <span v-for="(caracteristica, idx) in service.catering.caracteristicas" :key="idx" class="tagger">{{ caracteristica }}</span>
        </div>
      </div>
      <!-- Artículo -->
      <div v-if="service.catering && service.catering.articulo">
        <div class="detail-row">
          <i class="bi bi-box"></i>
          <strong>Tipo de artículo:</strong>
          <span>{{ service.catering.articulo.type_articulo }}</span>
        </div>
        <div class="detail-row">
          <i class="bi bi-card-text"></i>
          <strong>Nombre:</strong>
          <span>{{ service.catering.articulo.nombre }}</span>
        </div>
        <div class="detail-row">
          <i class="bi bi-info-circle"></i>
          <strong>Descripción:</strong>
          <span>{{ service.catering.articulo.descripcion }}</span>
        </div>
        <div class="detail-row" v-if="service.catering && service.catering.alergenos && service.catering.alergenos.length">
          <i class="bi bi-exclamation-triangle"></i>
          <strong>Alérgenos:</strong>
        </div>
        <div class="alergenos-list" v-if="service.catering && service.catering.alergenos && service.catering.alergenos.length">
          <span v-for="(alergeno, idx) in service.catering.alergenos" :key="idx" class="tag warning">{{ alergeno }}</span>
        </div>
        <div class="detail-row" v-if="service.catering.articulo.opciones_especiales && service.catering.articulo.opciones_especiales.length">
          <i class="bi bi-star"></i>
          <strong>Opciones especiales:</strong>
        </div>
        <div class="opciones-list" v-if="service.catering.articulo.opciones_especiales && service.catering.articulo.opciones_especiales.length">
          <span v-for="(opcion, idx) in service.catering.articulo.opciones_especiales" :key="idx" class="tag special">{{ opcion }}</span>
        </div>
        <div class="detail-row" v-if="service.catering && service.catering.caracteristicas && service.catering.caracteristicas.length">
          <i class="bi bi-check-circle-fill"></i>
          <strong>Características:</strong>
        </div>
        <div class="caracteristicas-list" v-if="service.catering && service.catering.caracteristicas && service.catering.caracteristicas.length">
          <span v-for="(caracteristica, idx) in service.catering.caracteristicas" :key="idx" class="tagger">{{ caracteristica }}</span>
        </div>
      </div>
    </div>
    <!-- Sección 3: Servicios Incluidos --><!-- Sección de Servicios Incluidos -->
    <div class="section" v-if="service && service.service_include">
      <h3>Servicios Incluidos</h3>
      <div class="services-included-list">
        <div v-for="(value, key) in service.service_include" :key="key" class="detail-row">
          <i class="bi bi-check-circle-fill"></i>
          <strong>{{ formatServiceIncludeKey(key) }}:</strong>
          <span>{{ formatServiceValue(value) }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import iconCatering from '@/assets/icon_catering.png'

export default {
  name: 'DetallesCatering',
  props: {
    service: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      iconCatering
    }
  },
  methods: {
    formatServiceIncludeKey (key) {
      // Convertir snake_case o camelCase a palabras con espacios y primera letra mayúscula
      return key
        .replace(/_/g, ' ')
        .replace(/([A-Z])/g, ' $1')
        .replace(/^./, str => str.toUpperCase())
    },
    formatServiceValue (value) {
      if (typeof value === 'boolean') {
        return value ? 'Sí' : 'No'
      }
      // Si es un array, convertir a string separado por comas
      if (Array.isArray(value)) {
        return value.join(', ')
      }
      // Si es un string, eliminar comillas si existen
      if (typeof value === 'string') {
        return value.replace(/^["']|["']$/g, '')
      }
      // Para otros tipos, convertir a string
      return String(value)
    }
  }
}
</script>

<style scoped>
.catering-details {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.section {
  background: #fff;
  border-radius: 12px;
  padding: 1.5rem;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}

.section h3 {
  color: #800020;
  margin: 0 0 1rem 0;
  font-size: 1.2rem;
  font-weight: 600;
}

.detail-row {
  font-size: 1rem;
  color: #333;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.detail-row:last-child {
  margin-bottom: 0;
}

.icon-catering {
  width: 15px;
  height: 15px;
  object-fit: contain;
}

.tag {
  background:rgb(230, 245, 255);
  color: #1976d2;
  border-radius: 6px;
  padding: 0.25rem 0.7rem;
  margin-right: 0.5rem;
  font-size: 0.9em;
}

.caracteristicas-list {
  display: flex;
  flex-wrap: wrap;
  margin-top: 0.1rem;
}

.tagger {
  background: rgb(213, 213, 213);
  color: #2C3E50;
  border-radius: 6px;
  padding: 0.25rem 0.7rem;
  font-size: 0.9em;
  display: inline-block;
  margin-right: 0.5rem;
  margin-bottom: 0.5rem;
}

.tag.warning {
  background: #fff3e0;
  color: #e65100;
}

.tag.special {
  background: #e8f5e9;
  color: #2e7d32;
}

.platos-list, .opciones-list, .alergenos-list, .caracteristicas-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: -0.5rem;
  margin-bottom: 1.25rem;
}

.services-included {
  margin-top: 1rem;
  background: #f8f9fa;
  border-radius: 8px;
  padding: 1rem;
}

.services-included-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.8rem;
}

.services-included-header i {
  color: #28a745;
  font-size: 1.2rem;
}

.services-included-header strong {
  color: #333;
  font-size: 1rem;
}

.services-included-list {
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
  width: 100%;
}

.services-included-list .detail-row {
  background: #f8f9fa;
  border-radius: 6px;
  padding: 0.8rem;
  border: 1px solid #e9ecef;
  margin-bottom: 0;
}

.services-included-list .detail-row i {
  color: #28a745;
}

.service-item {
  padding: 0.5rem 0.8rem;
  background: white;
  border-radius: 6px;
  font-size: 0.95rem;
  color: #666;
  border: 1px solid #e9ecef;
}

.services-included-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 0.8rem;
  width: 100%;
  margin-top: 0.5rem;
}

.service-include-item {
  background: #f8f9fa;
  border-radius: 6px;
  padding: 0.8rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  border: 1px solid #e9ecef;
}

.service-key {
  color: #666;
  font-size: 0.9rem;
  white-space: nowrap;
}

.service-value {
  color: #333;
  font-weight: 500;
  flex: 1;
  min-width: 0;
  word-break: break-word;
}

@media (max-width: 768px) {
  .services-included {
    grid-template-columns: 1fr;
  }
  .services-included-grid {
    grid-template-columns: 1fr;
  }
  .caracteristicas-list {
    gap: 0.4rem;
  }
  .tagger {
    padding: 0.3rem 0.6rem;
    margin: 0.15rem;
  }
}
</style>
