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

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

const gameId = computed(() => route.params.gameId)

const gameName = ref('')
const tierLists = ref([])
const error = ref('')

function fmt(ts) {
  return new Date(ts).toLocaleString(undefined, {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
  })
}

onMounted(async () => {
  try {
    const [gameRes, listRes] = await Promise.all([api.getGame(gameId.value), api.listPublicTierLists(gameId.value)])
    gameName.value = gameRes.game?.name || gameId.value
    tierLists.value = listRes.tierLists || []
  } catch (e) {
    error.value = '게임 정보를 불러오지 못했어요.'
  }
})

function createNew() {
  if (!auth.user) {
    router.push(`/login?redirect=/editor/${gameId.value}/new`)
    return
  }
  router.push(`/editor/${gameId.value}/new`)
}

function openTierList(id) {
  router.push(`/editor/${gameId.value}/${id}`)
}
</script>

<template>
  <section class="head">
    <div class="head__left">
      <div class="kicker">게임</div>
      <h2 class="title">{{ gameName || gameId }}</h2>
      <p class="desc">새 티어표를 만들거나, 다른 사람들이 올린 티어표를 확인하세요.</p>
    </div>
    <div class="head__right">
      <button class="primary" @click="createNew">{{ auth.user ? '새로운 티어표 만들기' : '로그인 후 새 티어표 만들기' }}</button>
    </div>
  </section>

  <div v-if="error" class="error">{{ error }}</div>
  <section class="panel">
    <div class="panel__title">공개 티어표</div>
    <div v-if="tierLists.length === 0" class="empty">아직 공개 티어표가 없어요.</div>
    <div v-else class="list">
      <button v-for="t in tierLists" :key="t.id" class="row" @click="openTierList(t.id)">
        <div class="row__title">{{ t.title }}</div>
        <div class="row__meta">
          작성자: {{ t.authorName || '알 수 없음' }} · 저장: {{ fmt(t.createdAt || t.updatedAt) }} · 업데이트: {{ fmt(t.updatedAt) }}
        </div>
      </button>
    </div>
  </section>
</template>

<style scoped>
.head {
  display: flex;
  gap: 14px;
  align-items: flex-end;
  justify-content: space-between;
  flex-wrap: wrap;
  padding: 6px 2px 14px;
}
.kicker {
  font-size: 12px;
  opacity: 0.7;
}
.title {
  margin: 4px 0 6px;
  font-size: 26px;
  letter-spacing: -0.02em;
}
.desc {
  margin: 0;
  opacity: 0.84;
}
.primary {
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(96, 165, 250, 0.2);
  color: rgba(255, 255, 255, 0.92);
  cursor: pointer;
  font-weight: 700;
}
.primary:hover {
  background: rgba(96, 165, 250, 0.26);
}
.panel {
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(255, 255, 255, 0.04);
  border-radius: 16px;
  padding: 14px;
}
.error {
  margin: 10px 0 14px;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid rgba(239, 68, 68, 0.3);
  background: rgba(239, 68, 68, 0.12);
}
.panel__title {
  font-weight: 800;
  margin-bottom: 10px;
}
.empty {
  opacity: 0.75;
}
.list {
  display: grid;
  gap: 10px;
}
.row {
  text-align: left;
  padding: 12px;
  border-radius: 14px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(255, 255, 255, 0.03);
  color: rgba(255, 255, 255, 0.92);
  cursor: pointer;
}
.row:hover {
  background: rgba(255, 255, 255, 0.05);
}
.row__title {
  font-weight: 800;
}
.row__meta {
  opacity: 0.78;
  margin-top: 6px;
  font-size: 13px;
}
</style>
