<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">Main Title</h1>
      <p class="topBar__desc">게임 선택과 커스텀 티어표 진입을 하나의 대시보드처럼 정리했습니다.</p>
    </div>
    <div class="toolbar">
      <button class="toolbar__ghost" @click="goFreeform">Toggle Filter</button>
      <button class="toolbar__select" @click="goFreeform">Select Filter</button>
      <button class="customTierBtn" @click="goFreeform">{{ auth.user ? '+ 커스텀 티어표 만들기' : '+ 로그인 후 커스텀 티어표 만들기' }}</button>
    </div>
  </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: 18px;
  justify-content: space-between;
  align-items: flex-start;
  flex-wrap: wrap;
  margin-top: 2px;
  margin-bottom: 18px;
}
.topBar__copy {
  display: grid;
  gap: 8px;
}
.topBar__title {
  margin: 0;
  font-size: 32px;
  letter-spacing: -0.04em;
  color: rgba(255, 255, 255, 0.96);
}
.topBar__desc {
  margin: 0;
  color: rgba(255, 255, 255, 0.58);
  line-height: 1.5;
}
.toolbar {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}
.toolbar__ghost,
.toolbar__select,
.customTierBtn {
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(255, 255, 255, 0.06);
  color: rgba(255, 255, 255, 0.84);
  font-weight: 700;
  cursor: pointer;
}
.customTierBtn {
  background: rgba(255, 255, 255, 0.08);
}
.grid {
  display: grid;
  grid-template-columns: repeat(3, 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);
}
.card {
  text-align: left;
  padding: 12px;
  border-radius: 14px;
  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: 0 1px 0 rgba(255, 255, 255, 0.04);
}
.card:hover {
  background: rgba(72, 72, 72, 0.92);
}
.thumbWrap {
  width: 100%;
  aspect-ratio: 16 / 9;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.06);
  background: #555;
  overflow: hidden;
  display: grid;
  place-items: center;
}
.thumb {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.thumbFallback {
  font-weight: 700;
  font-size: 14px;
  color: rgba(255, 255, 255, 0.4);
}
.card__title {
  font-weight: 800;
  letter-spacing: -0.02em;
  font-size: 15px;
}
@media (max-width: 1200px) {
  .grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}
@media (max-width: 900px) {
  .grid {
    grid-template-columns: 1fr;
  }
}
@media (max-width: 720px) {
  .topBar {
    align-items: stretch;
  }
  .toolbar {
    width: 100%;
  }
  .toolbar__ghost,
  .toolbar__select,
  .customTierBtn {
    flex: 1 1 100%;
  }
  .grid {
    grid-template-columns: 1fr;
  }
}
</style>
