---
import Icon from "@/components/misc/Icon.astro";
import BangumiSection from "@/components/pages/bangumi/BangumiSection.astro";
import TabNav from "@/components/pages/bangumi/TabNav.astro";
import { siteConfig } from "@/config";
import I18nKey from "@/i18n/i18nKey";
import { i18n } from "@/i18n/translation";
import MainGridLayout from "@/layouts/MainGridLayout.astro";
import type {
	UserSubjectCollection,
	UserSubjectCollectionResponse,
} from "@/types/bangumi";
import { formatDateI18nWithTime } from "@/utils/date-utils";

//参考：霞葉 https://kasuha.com/posts/fuwari-enhance-ep2/

// 检查页面是否启用
if (!siteConfig.pages.bangumi) {
	return Astro.redirect("/404/");
}

// 获取构建时间
const buildTime = formatDateI18nWithTime(new Date());

////////////// Bangumi 配置 ////////////////////////
const bangumiConfig = {
	username: siteConfig.bangumi?.userId, // 在这里配置你的Bangumi用户名
	apiUrl: "https://api.bgm.tv",
	categories: {
		book: true,
		anime: true,
		music: true,
		game: true,
		real: false,
	},
	// 数据获取设置
	pagination: {
		limit: 50, // 每页获取数量
		delay: 50, // 请求间隔毫秒数，避免频率限制
		maxTotal: 1000, // 最大获取总数，防止无限循环（0=无限制）
	},
};
//////////////////////////////////////////////////

// 分类映射
const categoryMap = {
	book: { id: "book", name: i18n(I18nKey.bangumiCategoryBook), subjectType: 1 },
	anime: {
		id: "anime",
		name: i18n(I18nKey.bangumiCategoryAnime),
		subjectType: 2,
	},
	music: {
		id: "music",
		name: i18n(I18nKey.bangumiCategoryMusic),
		subjectType: 3,
	},
	game: { id: "game", name: i18n(I18nKey.bangumiCategoryGame), subjectType: 4 },
	real: { id: "real", name: i18n(I18nKey.bangumiCategoryReal), subjectType: 6 },
};

// 获取Bangumi数据的函数 - 支持分页获取所有数据
async function fetchBangumiData(username: string, subjectType: number) {
	try {
		const { limit, delay, maxTotal } = bangumiConfig.pagination;
		let offset = 0;
		let allData: UserSubjectCollection[] = [];
		let hasMore = true;

		// 开发模式下只获取一页数据，加快调试速度
		const isDev = import.meta.env.DEV;
		const maxPages = isDev ? 1 : 0; // 0 表示不限制

		console.log(
			`[Bangumi] ${isDev ? "🔧 开发模式" : "🌐 生产模式"} - 开始获取用户 ${username} 的 subjectType ${subjectType} 数据...`,
		);

		while (hasMore) {
			// 开发模式限制：只获取一页
			if (isDev && maxPages > 0 && allData.length >= limit * maxPages) {
				console.log(`[Bangumi] 开发模式：已获取 ${maxPages} 页数据，停止获取`);
				break;
			}

			// 检查是否超过最大获取限制
			if (maxTotal > 0 && allData.length >= maxTotal) {
				console.log(`[Bangumi] 已达到最大获取限制 ${maxTotal}，停止获取`);
				break;
			}

			const url = `${bangumiConfig.apiUrl}/v0/users/${username}/collections?subject_type=${subjectType}&limit=${limit}&offset=${offset}`;

			console.log(`[Bangumi] 正在获取数据: ${url} (已获取: ${allData.length})`);

			const response = await fetch(url, {
				headers: {
					"User-Agent": "YuuOuRou Blog",
					Accept: "application/json",
				},
			});

			if (!response.ok) {
				console.warn(
					`[Bangumi] 无法获取数据 (状态码: ${response.status}):`,
					url,
				);
				break;
			}

			const data = (await response.json()) as UserSubjectCollectionResponse;
			const currentBatch = data.data || [];

			if (currentBatch.length > 0) {
				allData = allData.concat(currentBatch);
				offset += limit;

				// 如果本次获取的数据少于limit，说明已经是最后一页
				if (currentBatch.length < limit) {
					hasMore = false;
				}
			} else {
				hasMore = false;
			}

			// 添加延迟避免请求过于频繁
			if (hasMore) {
				await new Promise((resolve) => setTimeout(resolve, delay));
			}
		}

		console.log(`[Bangumi] 总共获取到 ${allData.length} 条数据`);
		return allData;
	} catch (error) {
		console.error("[Bangumi] 获取数据时出错:", error);
		return [];
	}
}

// 获取所有启用分类的数据
const bangumiData: Record<string, UserSubjectCollection[]> = {};
const tabs: Array<{ id: string; name: string; count: number }> = [];

// 检查是否已配置 Bangumi 用户ID
const isUserIdConfigured =
	bangumiConfig.username &&
	bangumiConfig.username !== "you-user-id" &&
	bangumiConfig.username.trim() !== "";

if (!isUserIdConfigured) {
	console.log("[Bangumi] ⚠️ 未配置 Bangumi 用户ID，跳过数据获取");
} else {
	console.log("[Bangumi] 🌐 从 API 获取数据");
}

for (const [categoryKey, enabled] of Object.entries(bangumiConfig.categories)) {
	// 如果未配置用户ID，跳过数据获取
	if (!isUserIdConfigured) {
		break;
	}
	if (enabled && categoryMap[categoryKey as keyof typeof categoryMap]) {
		const categoryInfo = categoryMap[categoryKey as keyof typeof categoryMap];
		try {
			const data = await fetchBangumiData(
				// biome-ignore lint/style/noNonNullAssertion: Checked by isUserIdConfigured
				bangumiConfig.username!,
				categoryInfo.subjectType,
			);
			bangumiData[categoryKey] = data;
			tabs.push({
				id: categoryKey,
				name: categoryInfo.name,
				count: data.length,
			});
		} catch (error) {
			console.error(`[Bangumi] 获取 ${categoryInfo.name} 数据失败:`, error);
			bangumiData[categoryKey] = [];
			tabs.push({
				id: categoryKey,
				name: categoryInfo.name,
				count: 0,
			});
		}
	}
}

const activeTab = tabs[0]?.id || "anime";
---

<MainGridLayout title={i18n(I18nKey.bangumi)} description={i18n(I18nKey.bangumiSubtitle)}>
  <div class="flex w-full rounded-[var(--radius-large)] overflow-hidden relative min-h-32">
    <div class="card-base z-10 px-9 py-6 relative w-full">
        <!-- 页面标题 -->
        <div class="relative w-full mb-8">
            <div class="mb-6">
                <div class="flex items-center gap-3 mb-3">
                    <div class="h-8 w-8 rounded-lg bg-[var(--primary)] flex items-center justify-center text-white dark:text-black/70">
                        <Icon icon="material-symbols:movie" class="text-[1.5rem]"></Icon>
                    </div>
                    <h1 class="text-3xl font-bold text-neutral-900 dark:text-neutral-100">
                        {i18n(I18nKey.bangumi)}
                    </h1>
                </div>
                <p class="text-base text-neutral-600 dark:text-neutral-400 leading-relaxed">
                    {i18n(I18nKey.bangumiSubtitle)}
                </p>
                <p class="text-xs text-neutral-500 dark:text-neutral-500 mt-2">
                    {i18n(I18nKey.bangumiLastUpdated)} {buildTime}
                </p>
            </div>

      <!-- Tab 导航和内容 -->
      {tabs.length > 0 ? (
        <>
          <TabNav tabs={tabs} activeTab={activeTab} />

          <!-- 内容区域 -->
          {tabs.map((tab) => (
                      <BangumiSection
            sectionId={tab.id}
            items={bangumiData[tab.id] || []}
            isActive={tab.id === activeTab}
            itemsPerPage={6}
          />
          ))}
        </>
      ) : (
        <div class="text-center py-16">
          <div class="inline-flex items-center justify-center w-16 h-16 bg-[var(--btn-regular-bg)] rounded-full mb-6 border border-[var(--line-divider)]">
            <Icon icon="material-symbols:settings" class="text-[2rem] text-[var(--btn-content)]" />
          </div>
          <h2 class="text-xl font-semibold text-black/80 dark:text-white/80 mb-3">
            {isUserIdConfigured ? i18n(I18nKey.bangumiEmpty) : "未配置 Bangumi 用户ID"}
          </h2>
          <p class="text-black/60 dark:text-white/60 mb-4 max-w-md mx-auto">
            {isUserIdConfigured ? i18n(I18nKey.bangumiEmptyReason) : "请在 src/config/siteConfig.ts 中配置你的 Bangumi 用户ID"}
          </p>
        </div>
      )}
    </div>
  </div>
</MainGridLayout>

<style>
  /* 自定义样式 */
  .line-clamp-2 {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
</style>