<script setup>
import { computed, onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { api } from '../lib/api'
import { toApiUrl } from '../lib/runtime'
import { useAuthStore } from '../stores/auth'

const router = useRouter()
const auth = useAuthStore()

const items = ref([])
const error = ref('')
const games = computed(() => items.value)

onMounted(async () => {
  try {
    const data = await api.listGames()
    items.value = data.games || []
  } catch (e) {
    error.value = '백엔드에 연결할 수 없어요. backend 서버가 실행 중인지 확인해주세요.'
  }
})

function goGame(gameId) {
  router.push(`/games/${gameId}`)
}

function goFreeform() {
  if (!auth.user) {
    router.push('/login?redirect=/editor/freeform/new')
    return
  }
  router.push('/editor/freeform/new')
}

function thumbUrl(g) {
  if (!g.thumbnailSrc) return ''
  return toApiUrl(g.thumbnailSrc)
}

</script>

<template>
  <section class="hero">
    <h1 class="hero__title">티어표 메이커</h1>
    <p class="hero__desc">
      게임을 선택하면 새 티어표를 만들거나, 다른 사람들이 올린 티어표를 볼 수 있어요.
    </p>
  </section>

  <div v-if="error" class="error">{{ error }}</div>
  <section class="grid">
    <button class="card card--freeform" @click="goFreeform">
      <div class="thumbWrap thumbWrap--freeform">
        <div class="thumbFallback">+</div>
      </div>
      <div class="card__eyebrow">{{ auth.user ? '템플릿 없이 시작' : '로그인 후 작성 가능' }}</div>
      <div class="card__title">직접 티어표 만들기</div>
    </button>
    <button v-for="g in games" :key="g.id" class="card" @click="goGame(g.id)">
      <div class="thumbWrap">
        <img v-if="thumbUrl(g)" class="thumb" :src="thumbUrl(g)" :alt="g.name" />
        <div v-else class="thumbFallback">{{ g.name[0] }}</div>
      </div>
      <div class="card__title">{{ g.name }}</div>
    </button>
  </section>
</template>

<style scoped>
.hero {
  padding: 18px 2px 14px;
}
.hero__title {
  font-size: 34px;
  letter-spacing: -0.03em;
  margin: 0 0 8px;
}
.hero__desc {
  margin: 0;
  opacity: 0.86;
  line-height: 1.5;
}
.grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  margin-top: 14px;
}
.error {
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid rgba(239, 68, 68, 0.3);
  background: rgba(239, 68, 68, 0.12);
  color: rgba(255, 255, 255, 0.92);
}
.card {
  text-align: left;
  padding: 14px;
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(255, 255, 255, 0.05);
  color: rgba(255, 255, 255, 0.92);
  cursor: pointer;
  display: grid;
  gap: 10px;
}
.card:hover {
  background: rgba(255, 255, 255, 0.07);
  border-color: rgba(255, 255, 255, 0.18);
}
.card--freeform {
  background: linear-gradient(135deg, rgba(96, 165, 250, 0.12), rgba(16, 185, 129, 0.12));
}
.thumbWrap {
  width: 100%;
  height: 140px;
  border-radius: 14px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(0, 0, 0, 0.18);
  overflow: hidden;
  display: grid;
  place-items: center;
}
.thumbWrap--freeform {
  background:
    radial-gradient(circle at top, rgba(96, 165, 250, 0.18), transparent 50%),
    rgba(0, 0, 0, 0.18);
}
.thumb {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.thumbFallback {
  font-weight: 900;
  font-size: 28px;
  opacity: 0.85;
}
.card__title {
  font-weight: 800;
  letter-spacing: -0.02em;
}
.card__eyebrow {
  font-size: 12px;
  font-weight: 800;
  opacity: 0.7;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
@media (max-width: 720px) {
  .grid {
    grid-template-columns: 1fr;
  }
}
</style>
