<script setup>
import { ref, onMounted } from "vue";
import { useRoute } from "vue-router";
import Toast from "primevue/toast";
import { useToast } from "primevue/usetoast";
import { userApi } from "../../api/user";
import { tenantPath } from "../../utils/tenant";

const toast = useToast();
const items = ref([]);
const loading = ref(true);
const route = useRoute();
const tenantRoute = (path) => tenantPath(path, route);

const fetchFavorites = async () => {
  try {
    const res = await userApi.getFavorites();
    items.value = res || [];
  } catch (e) {
    console.error(e);
  } finally {
    loading.value = false;
  }
};

onMounted(fetchFavorites);

const removeItem = async (id) => {
  try {
    await userApi.removeFavorite(id);
    items.value = items.value.filter((i) => i.id !== id);
    toast.add({ severity: "success", summary: "已取消收藏", life: 2000 });
  } catch (e) {
    toast.add({
      severity: "error",
      summary: "操作失败",
      detail: e.message,
      life: 3000,
    });
  }
};
</script>

<template>
  <div
    class="bg-white rounded-xl shadow-sm border border-slate-100 min-h-[600px] p-8"
  >
    <div class="flex items-center justify-between mb-8">
      <h1 class="text-2xl font-bold text-slate-900">我的收藏</h1>
      <div class="flex gap-4">
        <button
          class="text-sm font-bold text-slate-500 hover:text-slate-900 cursor-pointer"
        >
          批量管理
        </button>
      </div>
    </div>

    <!-- Content Grid -->
    <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
      <div
        v-for="item in items"
        :key="item.id"
        class="group relative bg-white border border-slate-200 rounded-xl overflow-hidden hover:shadow-md transition-all hover:border-primary-200 cursor-pointer"
        @click="$router.push(tenantRoute(`/contents/${item.id}`))"
      >
        <!-- Cover -->
        <div class="aspect-video bg-slate-100 relative overflow-hidden">
          <img
            :src="item.cover"
            class="w-full h-full object-cover transition-transform duration-500 group-hover:scale-105"
          />
          <div
            class="absolute bottom-2 left-2 px-1.5 py-0.5 bg-black/60 text-white text-xs rounded flex items-center gap-1"
          >
            <i
              class="pi"
              :class="
                item.type === 'video'
                  ? 'pi-play-circle'
                  : item.type === 'audio'
                    ? 'pi-volume-up'
                    : 'pi-book'
              "
            ></i>
            <span v-if="item.type === 'video'">视频</span>
            <span v-else-if="item.type === 'audio'">音频</span>
            <span v-else>文章</span>
          </div>
        </div>

        <!-- Info -->
        <div class="p-4">
          <h3
            class="font-bold text-slate-900 mb-2 line-clamp-2 group-hover:text-primary-600 transition-colors"
          >
            {{ item.title }}
          </h3>
          <div class="flex items-center gap-2 text-xs text-slate-500 mb-3">
            <img
              :src="
                item.author_avatar ||
                `https://api.dicebear.com/7.x/avataaars/svg?seed=${item.author_id}`
              "
              class="w-5 h-5 rounded-full"
            />
            <span>{{ item.author_name }}</span>
          </div>
          <div
            class="flex items-center justify-between text-xs text-slate-400 border-t border-slate-50 pt-3"
          >
            <span>{{ item.created_at }}</span>
            <button
              @click.stop="removeItem(item.id)"
              class="hover:text-red-500 flex items-center gap-1 transition-colors"
            >
              <i class="pi pi-heart-fill text-red-500"></i> 取消收藏
            </button>
          </div>
        </div>
      </div>
    </div>

    <!-- Empty State -->
    <div v-if="items.length === 0" class="text-center py-20">
      <div
        class="inline-flex items-center justify-center w-16 h-16 rounded-full bg-slate-50 mb-4"
      >
        <i class="pi pi-star text-2xl text-slate-300"></i>
      </div>
      <p class="text-slate-500 text-lg">暂无收藏内容</p>
      <router-link
        :to="tenantRoute('/')"
        class="mt-4 inline-block text-primary-600 font-medium hover:underline"
        >去发现好内容</router-link
      >
    </div>

    <Toast />
  </div>
</template>
