<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.filter((item) => item.id !== 'freeform'))

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="topBar">
    <div class="topBar__copy">
      <h1 class="topBar__title">게임 선택</h1>
      <p class="topBar__desc">관리자 고정 순서가 있으면 먼저 보여주고, 그 외 게임은 최근 생성순으로 정렬됩니다.</p>
    </div>
    <button class="customTierBtn" @click="goFreeform">{{ auth.user ? '커스텀 티어표 만들기' : '로그인 후 커스텀 티어표 만들기' }}</button>
  </section>

  <div v-if="error" class="error">{{ error }}</div>
  <section class="grid">
    <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>
.topBar {
  display: flex;
  gap: 16px;
  justify-content: space-between;
  align-items: flex-start;
  flex-wrap: wrap;
  margin-top: 4px;
}
.topBar__copy {
  display: grid;
  gap: 6px;
}
.topBar__title {
  margin: 0;
  font-size: 30px;
  letter-spacing: -0.03em;
}
.topBar__desc {
  margin: 0;
  opacity: 0.78;
  line-height: 1.5;
}
.customTierBtn {
  padding: 12px 16px;
  border-radius: 14px;
  border: 1px solid rgba(96, 165, 250, 0.28);
  background: linear-gradient(135deg, rgba(96, 165, 250, 0.2), rgba(16, 185, 129, 0.16));
  color: rgba(255, 255, 255, 0.96);
  font-weight: 900;
  cursor: pointer;
}
.grid {
  display: grid;
  grid-template-columns: repeat(3, 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);
}
.thumbWrap {
  width: 100%;
  aspect-ratio: 16 / 9;
  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;
}
.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;
}
@media (max-width: 720px) {
  .topBar {
    align-items: stretch;
  }
  .customTierBtn {
    width: 100%;
  }
  .grid {
    grid-template-columns: 1fr;
  }
}
@media (min-width: 721px) and (max-width: 1100px) {
  .grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}
</style>
