<template>
  <div class="card cursor-pointer" @click="$emit('click')">
    <div class="relative">
      <img
        :src="imageSrc"
        :alt="object.title"
        class="w-full h-48 object-cover"
      >
      <div class="absolute top-2 right-2">
        <span class="badge" :class="statusBadgeClass">
          {{ statusLabel }}
        </span>
      </div>
    </div>

    <div class="card-body">
      <h3 class="text-lg font-semibold mb-2">{{ object.title || object.short_name }}</h3>
      <p class="text-gray-600 text-sm mb-3 line-clamp-2">
        {{ object.address || 'Адрес не указан' }}
      </p>

      <div class="flex items-center justify-between">
        <div class="flex items-center space-x-1">
          <span class="text-yellow-500">⭐</span>
          <span class="text-sm font-medium">{{ averageScore }}</span>
        </div>
        <div class="text-right">
          <div class="font-bold text-primary-600">
            {{ formatPrice(object.price) }}
          </div>
          <div class="text-xs text-gray-500">{{ object.price_period || 'за единицу' }}</div>
        </div>
      </div>

      <div class="mt-3 flex items-center text-sm text-gray-500">
        <span class="mr-2">📍</span>
        <span>{{ object.address || 'Адрес не указан' }}</span>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import type { ObjectShortResponse } from '~/types/objects'

interface Props {
  object: ObjectShortResponse
}

const props = defineProps<Props>()
defineEmits<{ click: [] }>()

const imageSrc = computed(() => {
  return '/images/placeholder.jpg'
})

const averageScore = computed(() => {
  return props.object.tourist_average_score || props.object.entrepreneur_average_score || '—'
})

const statusLabel = computed(() => {
  const labels: Record<string, string> = {
    active: 'Активен',
    draft: 'Черновик',
    moderation: 'На модерации',
    inactive: 'Неактивен',
    rejected: 'Отклонён'
  }
  return labels[props.object.status] || props.object.status
})

const statusBadgeClass = computed(() => {
  const classes: Record<string, string> = {
    active: 'badge-success',
    draft: 'badge-secondary',
    moderation: 'badge-warning',
    inactive: 'badge-secondary',
    rejected: 'badge-error'
  }
  return classes[props.object.status] || 'badge-secondary'
})

const formatPrice = (price: number | undefined) => {
  if (!price && price !== 0) return '—'
  return new Intl.NumberFormat('ru-RU', {
    style: 'currency',
    currency: 'RUB',
    minimumFractionDigits: 0
  }).format(price)
}
</script>
