<template>
  <div class="service-card" :class="type">
    <div class="service-provider">
      <div class="provider-avatar">
        <img :src="getProviderPhoto()" :alt="getProviderName()">
      </div>
      <span class="provider-name fw-bold" @click="goToProfile">{{ getProviderName() }}</span>
    </div>
    <div class="service-main">
      <div class="service-image">
        <div class="carousel-container">
          <img v-if="service.images && service.images.length > 0"
               :src="service.images[currentImageIndex]"
               :alt="service.name">
          <img v-else
               :src="getExampleImage()"
               :alt="service.name">
          <div v-if="shouldShowControls" class="carousel-controls">
            <button class="carousel-btn prev" @click.stop="prevImage">
              <i class="bi bi-chevron-left fw-bold"></i>
            </button>
            <button class="carousel-btn next" @click.stop="nextImage">
              <i class="bi bi-chevron-right fw-bold"></i>
            </button>
          </div>
          <div v-if="shouldShowControls" class="carousel-indicators">
            <span v-for="(_, index) in getImages()"
                  :key="index"
                  :class="['indicator', { active: index === currentImageIndex }]"
                  @click.stop="currentImageIndex = index">
            </span>
          </div>
        </div>
      </div>
      <div class="service-content" @click="viewDetails" tabindex="0">
        <div class="service-header">
          <h3 class="service-name">{{ service.name }}</h3>
        </div>
        <div class="service-features">
          <slot name="additional-info"></slot>
        </div>
        <span v-if="getMinPriceFormatted() !== null" class="service-price">{{ getFormattedPrice() }}</span>
      </div>
    </div>
  </div>
</template>

<script>
import imagenEjemplo from '@/assets/masia_event.jpg'
import imagenEjemplo2 from '@/assets/banquete.jpg'
import defaultAvatar from '@/assets/account_icon.png'
import AccountService from '@/services/AccountServices'

export default {
  name: 'ServiceCard',
  props: {
    service: {
      type: Object,
      required: true
    },
    type: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      imagenEjemplo,
      imagenEjemplo2,
      defaultAvatar,
      account: null,
      currentImageIndex: 0
    }
  },
  computed: {
    shouldShowControls () {
      return (this.service.images && this.service.images.length > 1) ||
             (!this.service.images && this.getImages().length > 1)
    }
  },
  async created () {
    try {
      if (this.service.account_id) {
        const response = await AccountService.getAccountById(this.service.account_id)
        this.account = response
      }
    } catch (error) {
      console.error('Error al obtener los datos del proveedor:', error)
    }
  },
  methods: {
    getProviderPhoto () {
      return this.account && this.account.photo ? this.account.photo : this.defaultAvatar
    },
    getProviderName () {
      return this.account && this.account.username ? this.account.username : 'Usuario'
    },
    viewDetails () {
      this.$router.push({ name: 'detallesService', params: { id: this.service.id } })
    },
    contactProvider () {
      this.$emit('contact-provider', this.service)
    },
    getMinPrice () {
      if (!this.service.availability_rules || this.service.availability_rules.length === 0) return null
      const prices = this.service.availability_rules
        .map(rule => rule.price)
        .filter(price => typeof price === 'number' && !isNaN(price))
      if (prices.length === 0) return null
      return Math.min(...prices)
    },
    getMinPriceFormatted () {
      const min = this.getMinPrice()
      if (min === null) return null
      return new Intl.NumberFormat('es-ES', { style: 'decimal', minimumFractionDigits: 2, maximumFractionDigits: 2 }).format(min)
    },
    getFormattedPrice () {
      const price = this.getMinPriceFormatted()
      if (price === null) return ''

      switch (this.service.type) {
        case 'espai':
          return `Desde ${price}€/h`
        case 'catering':
          return `Desde ${price}€/pax.`
        case 'mobiliari-decoracio':
          return `Desde ${price}€/u`
        case 'musica':
        case 'fotografia':
          return `Desde ${price}€/h`
        default:
          return `Desde ${price}€`
      }
    },
    goToProfile () {
      if (this.account && this.account.id) {
        this.$router.push({ name: 'profile', params: { id: this.account.id } })
      }
    },
    getImages () {
      return [this.imagenEjemplo, this.imagenEjemplo2]
    },
    getExampleImage () {
      // return this.service.images && this.service.images.length > 0 ? this.service.images : [this.imagenEjemplo, this.imagenEjemplo2]
      return this.getImages()[this.currentImageIndex]
    },
    nextImage () {
      const images = this.getImages()
      if (images.length > 1) {
        this.currentImageIndex = (this.currentImageIndex + 1) % images.length
      }
    },
    prevImage () {
      const images = this.getImages()
      if (images.length > 1) {
        this.currentImageIndex = (this.currentImageIndex - 1 + images.length) % images.length
      }
    }
  }
}
</script>

<style scoped>
.service-card {
  background: rgb(246, 246, 246);
  border-radius: 5px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  display: flex;
  flex-direction: column;
  min-width: 400px;
  min-height: 140px;
  max-width: 1100px;
  width: 100%;
  margin: auto;
}

.service-main {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  flex: 1;
}

.service-provider {
  width: 100%;
  padding: 0.5rem 1.5rem;
  display: flex;
  align-items: center;
  gap: 0.8rem;
  background-color: rgba(250, 250, 250, 0.9);
  border-top: 1px solid #eee;
}

.provider-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
  border: 3px solid #800020;
  box-sizing: border-box;
}

.provider-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.provider-name {
  font-size: 0.95rem;
  color: #333;
  font-weight: 500;
  cursor: pointer;
  transition: color 0.2s ease;
}

.provider-name:hover {
  color: #800020;
}

.provider-name.fw-bold {
  font-weight: 700;
}

.service-image {
  flex: 0 0 200px;
  min-width: 350px;
  max-height: 350px;
  width: auto;
  height: auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background: #f5f5f5;
}

.carousel-container {
  position: relative;
  width: 100%;
  height: 100%;
}

.carousel-controls {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  transform: translateY(-50%);
  display: flex;
  justify-content: space-between;
  padding: 0 10px;
  pointer-events: none;
}

.carousel-btn {
  background: transparent;
  border: none;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  pointer-events: auto;
  transition: all 0.3s ease;
  position: relative;
}

.carousel-btn::before {
  content: '';
  position: absolute;
  width: 30px;
  height: 30px;
  background: rgba(255, 255, 255, 0.75);
  border-radius: 50%;
  opacity: 0;
  transition: all 0.3s ease;
}

.carousel-btn:hover::before {
  opacity: 0.75;
}

.carousel-btn i {
  color: #800020;
  font-size: 1.25rem;
  position: relative;
  z-index: 1;
  text-shadow: 0 0 3px rgba(255, 255, 255, 1);
}

.carousel-btn:hover i {
  color: #800020;
}

.carousel-indicators {
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
  pointer-events: none;
}

.indicator {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(0, 0, 0, 1);
  cursor: pointer;
  pointer-events: auto;
  transition: all 0.3s ease;
}

.indicator.active {
  background: #800020;
  border-color: rgba(0, 0, 0, 1);
  transform: scale(1.1);
}

.indicator:hover {
  background: rgba(255, 255, 255, 0.5);
  border-color: rgba(0, 0, 0, 1);
}

.service-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 5px;
}

.service-content {
  flex: 1 1 0%;
  padding: 0rem 1.5rem;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  min-height: 0px;
  min-width: 0;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.service-content:hover {
  background-color: rgb(236, 236, 236);
}

.service-header {
  margin-bottom: 0rem;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
}

.service-name {
  font-size: 1.1rem;
  font-weight: 700;
  color: #800020;
  margin-bottom: 0rem;
  text-align: left;
}

.service-price {
  font-size: 1rem;
  color: #800020;
  font-weight: 600;
  margin-left: auto;
  margin-top: 0rem;
  margin-bottom: 1rem;
  display: block;
  text-align: right;
}

.service-features {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 0.3rem;
}

/* Estilos específicos para cada tipo de servicio */
.service-card.catering {
  border-left: 4px solid #FF6B6B;
}

.service-card.mobiliari-decoracio {
  border-left: 4px solid #4ECDC4;
}

.service-card.musica {
  border-left: 4px solid #FFD93D;
}

.service-card.fotografia {
  border-left: 4px solid #95E1D3;
}

.service-card.espai {
  border-left: 4px solid #6C5CE7;
}

.service-header,
.service-name {
  text-align: left !important;
  align-items: flex-start !important;
  justify-content: flex-start !important;
}

@media (max-width: 900px) {
  .service-card {
    max-width: 100%;
    min-width: 0;
  }
  .service-image {
    flex: 0 0 100px;
    min-width: 100px;
    min-height: 100px;
    width: 100px;
    height: 100px;
  }
  .service-content {
    padding: 0.5rem 0.5rem;
    min-height: 80px;
  }
}

@media (max-width: 700px) {
  .service-card {
    flex-direction: column;
    max-width: 100%;
    min-width: 0;
    min-height: unset;
  }
  .service-main {
    flex-direction: column;
  }
  .service-image {
    width: 100%;
    height: 120px;
    min-width: 0;
    min-height: 0;
    border-radius: 5px;
  }
  .service-image img {
    border-radius: 5px;
  }
  .service-content {
    padding: 0.5rem;
    min-height: unset;
  }
}
</style>
