<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 goFavorites() {
  if (!auth.user) {
    router.push('/login?redirect=/favorites')
    return
  }
  router.push('/favorites')
}

function goMyLists() {
  if (!auth.user) {
    router.push('/login?redirect=/me')
    return
  }
  router.push('/me')
}

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

</script>

<template>
  <section class="dashboardHero">
    <div class="dashboardHero__copy">
      <div class="dashboardHero__eyebrow">Workspace</div>
      <h1 class="dashboardHero__title">Game Library</h1>
      <p class="dashboardHero__desc">자주 쓰는 게임 템플릿을 빠르게 고르고, 필요하면 바로 커스텀 티어표를 시작할 수 있어요.</p>
    </div>
    <div class="dashboardToolbar">
      <div class="dashboardToolbar__stat">
        <span class="dashboardToolbar__label">Visible Games</span>
        <strong class="dashboardToolbar__value">{{ games.length }}</strong>
      </div>
      <div class="dashboardToolbar__stat">
        <span class="dashboardToolbar__label">Account</span>
        <strong class="dashboardToolbar__value">{{ auth.user ? 'Ready' : 'Guest' }}</strong>
      </div>
      <button class="dashboardToolbar__ghost" @click="goFavorites">{{ auth.user ? '즐겨찾기 보기' : '로그인 후 즐겨찾기' }}</button>
      <button class="dashboardToolbar__ghost" @click="goMyLists">{{ auth.user ? '내 리스트 보기' : '로그인 후 내 리스트' }}</button>
      <button class="customTierBtn" @click="goFreeform">{{ auth.user ? '+ 커스텀 티어표 만들기' : '+ 로그인 후 커스텀 티어표 만들기' }}</button>
    </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">
          <span class="libraryCard__metaDot"></span>
          <span>{{ g.id }}</span>
        </div>
      </div>
    </button>
  </section>
</template>

<style scoped>
.dashboardHero {
  display: flex;
  gap: 18px;
  justify-content: space-between;
  align-items: flex-start;
  flex-wrap: wrap;
  margin-top: 2px;
  margin-bottom: 18px;
  padding: 6px 2px 18px;
}
.dashboardHero__copy {
  display: grid;
  gap: 8px;
  max-width: 720px;
}
.dashboardHero__eyebrow {
  font-size: 11px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: rgba(255, 255, 255, 0.42);
}
.dashboardHero__title {
  margin: 0;
  font-size: 34px;
  letter-spacing: -0.04em;
  color: rgba(255, 255, 255, 0.96);
}
.dashboardHero__desc {
  margin: 0;
  color: rgba(255, 255, 255, 0.58);
  line-height: 1.5;
  max-width: 720px;
}
.dashboardToolbar {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}
.dashboardToolbar__stat {
  display: grid;
  gap: 2px;
  min-width: 112px;
  padding: 12px 14px;
  border-radius: 14px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(255, 255, 255, 0.045);
}
.dashboardToolbar__label {
  font-size: 11px;
  color: rgba(255, 255, 255, 0.48);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}
.dashboardToolbar__value {
  font-size: 18px;
  font-weight: 900;
}
.dashboardToolbar__ghost,
.customTierBtn {
  padding: 12px 14px;
  border-radius: 14px;
  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;
  transition:
    transform 0.16s ease,
    border-color 0.16s ease,
    background 0.16s ease;
}
.dashboardToolbar__ghost:hover,
.customTierBtn:hover {
  transform: translateY(-1px);
}
.customTierBtn {
  background: rgba(77, 127, 233, 0.88);
  border-color: rgba(77, 127, 233, 0.96);
  color: #fff;
}
.libraryGrid {
  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);
}
.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: 13px;
  font-weight: 700;
  font-size: 14px;
  color: rgba(255, 255, 255, 0.4);
}
.libraryCard__body {
  display: grid;
  gap: 8px;
}
.libraryCard__title {
  font-weight: 800;
  letter-spacing: -0.02em;
  font-size: 16px;
}
.libraryCard__meta {
  display: inline-flex;
  gap: 8px;
  align-items: center;
  color: rgba(255, 255, 255, 0.6);
  font-size: 13px;
}
.libraryCard__metaDot {
  width: 10px;
  height: 10px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.9);
}
@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) {
  .dashboardHero {
    align-items: stretch;
  }
  .dashboardToolbar {
    width: 100%;
  }
  .dashboardToolbar__ghost,
  .dashboardToolbar__stat,
  .customTierBtn {
    flex: 1 1 100%;
  }
  .libraryGrid {
    grid-template-columns: 1fr;
  }
}
</style>
