<template>
  <div class="p-8">
    <div class="flex items-center justify-between mb-6">
      <h1 class="text-3xl font-black text-black">YOUR PLAYLISTS</h1>
      <button @click="openCreate"
        class="bg-noctune-teal border-2 border-black px-4 py-2 font-bold flex items-center gap-2 rounded-lg hover:bg-teal-600 transition-colors cursor-pointer">
        <svg class="w-5 h-5" fill="none" stroke="currentColor" stroke-width="3" viewBox="0 0 24 24"><path d="M12 4v16m8-8H4"/></svg>
        NEW PLAYLIST
      </button>
    </div>

    <div v-if="loading" class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4">
      <div v-for="i in 4" :key="i" class="animate-pulse bg-gray-200 rounded-2xl border-2 border-black h-64" />
    </div>

    <div v-else-if="playlists.length === 0" class="text-center py-20">
      <svg class="w-24 h-24 mx-auto text-gray-300 mb-4" fill="currentColor" viewBox="0 0 24 24"><path d="M3 10h11v2H3v-2zm0-4h11v2H3V6zm0 8h7v2H3v-2zm13-1v8l6-4-6-4z"/></svg>
      <p class="text-gray-500 mb-2 text-lg">No playlists yet</p>
      <p class="text-gray-400 text-sm mb-4">Create your first playlist to start organizing your music</p>
      <button @click="openCreate"
        class="bg-noctune-yellow border-2 border-black px-6 py-3 font-bold rounded-lg hover:bg-yellow-400 transition-colors cursor-pointer">
        CREATE YOUR FIRST PLAYLIST
      </button>
    </div>

    <div v-else class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4">
      <div v-for="pl in playlists" :key="pl._id"
        @click="router.push(`/playlist/${pl._id}`)"
        class="bg-white rounded-2xl border-2 border-black overflow-hidden hover:scale-[1.02] transition-transform cursor-pointer group"
      >
        <div class="aspect-square bg-gray-300 relative overflow-hidden">
          <img v-if="pl.coverUrl" :src="formatUrl(pl.coverUrl)" class="w-full h-full object-cover" />
          <div v-else class="w-full h-full flex items-center justify-center bg-gradient-to-br from-noctune-teal/20 to-noctune-yellow/20">
            <svg class="w-20 h-16 text-gray-400" fill="currentColor" viewBox="0 0 24 24"><path d="M3 10h11v2H3v-2zm0-4h11v2H3V6zm0 8h7v2H3v-2zm13-1v8l6-4-6-4z"/></svg>
          </div>
          <div class="absolute inset-0 bg-black/0 group-hover:bg-black/10 transition-colors" />
        </div>
        <div class="p-3">
          <p class="font-bold text-sm truncate text-black">{{ pl.name }}</p>
          <p class="text-xs text-gray-500">{{ pl.tracks?.length || 0 }} songs</p>
        </div>
      </div>
    </div>

    <CreatePlaylistModal :show="showCreateModal" @close="showCreateModal = false" @created="onPlaylistCreated" />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../stores/authStore.js'
import api from '../lib/api.js'
import { formatUrl } from '../lib/utils.js'
import { useAuthGate } from '../composables/useAuthGate.js'
import CreatePlaylistModal from '../components/CreatePlaylistModal.vue'

const router = useRouter()
const authStore = useAuthStore()
const { requireAuth } = useAuthGate()

const playlists = ref([])
const loading = ref(true)
const showCreateModal = ref(false)

function openCreate() {
  requireAuth({
    redirectTo: '/login',
    onAuthenticated: () => { showCreateModal.value = true }
  })
}

function onPlaylistCreated(playlist) {
  showCreateModal.value = false
  playlists.value.unshift(playlist)
}

onMounted(async () => {
  if (!authStore.isLoggedIn) {
    loading.value = false
    return
  }
  try {
    const { data } = await api.get('/playlists')
    if (data.success) playlists.value = data.data
  } catch {
    // ignore
  } finally {
    loading.value = false
  }
})
</script>
