<template>
  <div class="home">
    <section class="hero">
      <div class="container">
        <div class="hero__content">
          <h1 class="hero__title">Ял Арба</h1>
          <p class="hero__subtitle">
            Туристическая платформа Республики Башкортостан
          </p>
          <div class="hero__search">
            <div class="hero__search-input">
              <svg width="20" height="20" viewBox="0 0 24 24" fill="none">
                <circle cx="11" cy="11" r="7" stroke="currentColor" stroke-width="2"/>
                <path d="M20 20l-3.5-3.5" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
              </svg>
              <input
                v-model="searchQuery"
                type="text"
                placeholder="Поиск достопримечательностей, маршрутов, мест..."
                @keyup.enter="doSearch"
              />
            </div>
            <button class="btn btn--primary btn--lg" @click="doSearch">
              Найти
            </button>
          </div>
        </div>
      </div>
    </section>

    <section class="section categories">
      <div class="container">
        <h2 class="categories__title">Популярные категории</h2>
        <div class="categories__grid">
          <NuxtLink
            v-for="cat in categories"
            :key="cat.id"
            :to="`/search?category_id=${cat.id}`"
            class="categories__card"
          >
            <div class="categories__icon">
              <component :is="cat.icon" />
            </div>
            <span class="categories__name">{{ cat.name }}</span>
          </NuxtLink>
        </div>
      </div>
    </section>

    <section class="section popular">
      <div class="container">
        <div class="popular__header">
          <h2>Популярные места</h2>
          <NuxtLink to="/search" class="btn btn--secondary btn--sm">
            Все места
          </NuxtLink>
        </div>
        <div v-if="loading" class="popular__grid">
          <div v-for="n in 4" :key="n" class="card">
            <div class="skeleton" style="height: 200px" />
            <div style="padding: 16px">
              <div class="skeleton" style="height: 20px; width: 80%; margin-bottom: 8px" />
              <div class="skeleton" style="height: 16px; width: 60%" />
            </div>
          </div>
        </div>
        <div v-else-if="objects.length" class="popular__grid">
          <NuxtLink
            v-for="obj in objects"
            :key="obj.id"
            :to="`/objects/${obj.id}`"
            class="card object-card"
          >
            <div class="object-card__image">
              <img
                v-if="obj.images?.[0]"
                :src="obj.images[0]"
                :alt="obj.title"
              />
              <div v-else class="object-card__placeholder">
                <svg width="48" height="48" viewBox="0 0 24 24" fill="none">
                  <rect x="3" y="3" width="18" height="18" rx="2" stroke="currentColor" stroke-width="2"/>
                  <circle cx="8.5" cy="8.5" r="1.5" stroke="currentColor" stroke-width="2"/>
                  <path d="M21 15l-5-5L5 21" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
                </svg>
              </div>
            </div>
            <div class="object-card__info">
              <h5 class="object-card__title">{{ obj.title }}</h5>
              <p v-if="obj.short_description" class="small-text object-card__desc">
                {{ obj.short_description }}
              </p>
              <div class="object-card__meta">
                <span v-if="obj.rating" class="object-card__rating">
                  <svg width="16" height="16" viewBox="0 0 24 24" fill="#FF8833">
                    <path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z"/>
                  </svg>
                  {{ obj.rating.toFixed(1) }}
                </span>
                <span v-if="obj.category_name" class="tag">{{ obj.category_name }}</span>
              </div>
            </div>
          </NuxtLink>
        </div>
        <div v-else class="popular__empty">
          <p class="body-text--gray">Пока нет добавленных мест</p>
        </div>
      </div>
    </section>
  </div>
</template>

<script setup lang="ts">
import { CATEGORIES } from '~/utils/constants'

const searchQuery = ref('')
const objectsStore = useObjectsStore()
const { objects, loading } = storeToRefs(objectsStore)

const categories = CATEGORIES

onMounted(() => {
  objectsStore.fetchObjects({ per_page: 4, sort_by: 'rating', sort_order: 'desc' })
})

function doSearch() {
  if (searchQuery.value.trim()) {
    navigateTo(`/search?query=${encodeURIComponent(searchQuery.value.trim())}`)
  }
}

definePageMeta({
  title: 'Ял Арба',
})
</script>

<style scoped>
.hero {
  background: linear-gradient(135deg, var(--color-primary) 0%, #0d4a25 100%);
  color: var(--color-text-white);
  padding: 100px 0 80px;
  text-align: center;
}

.hero__title {
  font-family: var(--font-display);
  font-size: var(--font-size-h1);
  color: var(--color-text-white);
  margin-bottom: 16px;
}

.hero__subtitle {
  font-family: var(--font-body);
  font-size: 20px;
  color: rgba(255, 255, 255, 0.8);
  margin-bottom: 40px;
}

.hero__search {
  display: flex;
  gap: 12px;
  max-width: 640px;
  margin: 0 auto;
}

.hero__search-input {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--color-white);
  border-radius: var(--radius-sm);
  padding: 0 16px;
}

.hero__search-input svg {
  color: var(--color-text-gray);
  flex-shrink: 0;
}

.hero__search-input input {
  flex: 1;
  height: 56px;
  border: none;
  outline: none;
  font-family: var(--font-body);
  font-size: var(--font-size-body);
  color: var(--color-text-black);
  background: transparent;
}

.hero__search-input input::placeholder {
  color: var(--color-text-gray);
}

.categories__title {
  margin-bottom: 32px;
}

.categories__grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 16px;
}

.categories__card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 24px 16px;
  background: var(--color-white);
  border: 1px solid var(--color-stroke);
  border-radius: var(--radius-md);
  text-align: center;
  transition: all var(--transition-fast);
}

.categories__card:hover {
  border-color: var(--color-primary);
  box-shadow: var(--shadow-sm);
}

.categories__icon {
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-light-green);
  border-radius: var(--radius-full);
  color: var(--color-primary);
}

.categories__name {
  font-family: var(--font-body);
  font-size: var(--font-size-small);
  font-weight: var(--font-weight-medium);
  color: var(--color-text-black);
}

.popular__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 32px;
}

.popular__grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 24px;
}

.object-card {
  cursor: pointer;
}

.object-card__image {
  height: 200px;
  overflow: hidden;
}

.object-card__image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.object-card__placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-bg-gray);
  color: var(--color-text-gray);
}

.object-card__info {
  padding: 16px;
}

.object-card__title {
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.object-card__desc {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  margin-bottom: 8px;
}

.object-card__meta {
  display: flex;
  align-items: center;
  gap: 8px;
}

.object-card__rating {
  display: flex;
  align-items: center;
  gap: 4px;
  font-family: var(--font-body);
  font-size: var(--font-size-small);
  font-weight: var(--font-weight-semibold);
  color: var(--color-text-black);
}

.popular__empty {
  text-align: center;
  padding: 60px 0;
}

@media (max-width: 1024px) {
  .popular__grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .categories__grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 744px) {
  .hero {
    padding: 60px 0 48px;
  }

  .hero__search {
    flex-direction: column;
  }

  .hero__search-input input {
    height: 48px;
  }

  .hero__search .btn {
    width: 100%;
  }

  .categories__grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .popular__grid {
    grid-template-columns: 1fr;
  }
}
</style>
