import { MetadataRoute } from 'next';
import { fetchBoardList } from '@/lib/api/forum/board';

const BASE_URL = 'https://dpot.web.or.kr';

export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
	// 정적 페이지
	const staticPages: MetadataRoute.Sitemap = [
		{ url: BASE_URL, changeFrequency: 'daily', priority: 1.0 },
		{ url: `${BASE_URL}/login`, changeFrequency: 'monthly', priority: 0.3 },
		{ url: `${BASE_URL}/register`, changeFrequency: 'monthly', priority: 0.3 },
		{ url: `${BASE_URL}/docs`, changeFrequency: 'weekly', priority: 0.5 },
		{ url: `${BASE_URL}/support/faq`, changeFrequency: 'monthly', priority: 0.4 },
		{ url: `${BASE_URL}/support/guide`, changeFrequency: 'monthly', priority: 0.4 },
		{ url: `${BASE_URL}/support/contact`, changeFrequency: 'monthly', priority: 0.3 },
	];

	// 동적: 게시판 목록
	let boardPages: MetadataRoute.Sitemap = [];
	try {
		const res = await fetchBoardList();
		if (res.success && res.data?.list) {
			boardPages = res.data.list
				.filter(b => b.isActive)
				.map(b => ({
					url: `${BASE_URL}/board/${b.code}`,
					changeFrequency: 'daily' as const,
					priority: 0.8,
				}));
		}
	} catch {

	}

	return [...staticPages, ...boardPages];
}