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

const router = useRouter()

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 thumbUrl(g) {
  return g.thumbnailSrc ? toApiUrl(g.thumbnailSrc) : ''
}
</script>

<template>
  <section class="pageHead">
    <div class="pageHead__main">
      <div class="pageHead__eyebrow">Workspace</div>
      <h1 class="pageHead__title">Game Library</h1>
      <p class="pageHead__desc">자주 쓰는 게임 템플릿을 빠르게 고르고, 필요하면 바로 커스텀 티어표를 시작할 수 있어요.</p>
    </div>
  </section>

  <div v-if="error" class="error">{{ error }}</div>
  <section class="libraryGrid">
    <button v-for="g in games" :key="g.id" class="libraryCard" @click="goGame(g.id)">
      <div class="libraryCard__thumbWrap">
        <img v-if="thumbUrl(g)" class="libraryCard__thumb" :src="thumbUrl(g)" :alt="g.name" />
        <div v-else class="libraryCard__thumbFallback">대표 썸네일</div>
      </div>
      <div class="libraryCard__body">
        <div class="libraryCard__title">{{ g.name }}</div>
        <div class="libraryCard__meta">{{ g.id }}</div>
      </div>
    </button>
  </section>
</template>

<style scoped>
.libraryGrid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 18px;
}
.error {
  margin: 0 0 16px;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid rgba(239, 68, 68, 0.18);
  background: rgba(239, 68, 68, 0.1);
  color: rgba(255, 255, 255, 0.92);
}
.libraryCard {
  text-align: left;
  padding: 14px;
  border-radius: 22px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  background: rgba(62, 62, 62, 0.82);
  color: rgba(255, 255, 255, 0.92);
  cursor: pointer;
  display: grid;
  gap: 12px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
  transition:
    transform 0.16s ease,
    background 0.16s ease;
}
.libraryCard:hover {
  background: rgba(70, 70, 70, 0.96);
  transform: translateY(-2px);
}
.libraryCard__thumbWrap {
  width: 100%;
  aspect-ratio: 16 / 9;
  border-radius: 14px;
  border: 1px solid rgba(255, 255, 255, 0.06);
  background: #555;
  overflow: hidden;
  display: grid;
  place-items: center;
}
.libraryCard__thumb {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.libraryCard__thumbFallback {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.4);
}
.libraryCard__body {
  display: grid;
}
.libraryCard__title {
  font-weight: 800;
  letter-spacing: -0.02em;
  font-size: 18px;
}
.libraryCard__meta {
  color: rgba(255, 255, 255, 0.6);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
@media (max-width: 1400px) {
  .libraryGrid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}
@media (max-width: 1200px) {
  .libraryGrid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}
@media (max-width: 900px) {
  .libraryGrid {
    grid-template-columns: 1fr;
  }
}
@media (max-width: 720px) {
  .libraryGrid {
    grid-template-columns: 1fr;
  }
}
</style>
