import Link from "next/link";
import { headers } from "next/headers";

export default async function BoardsPage() {
  // 서버에서 상대경로 fetch가 실패하므로 요청 헤더로 절대 URL을 구성합니다.
  const h = await headers();
  const host = h.get("host") ?? "localhost:3000";
  const proto = h.get("x-forwarded-proto") ?? "http";
  const base = process.env.NEXT_PUBLIC_BASE_URL || `${proto}://${host}`;
  const url = new URL("/api/boards", base).toString();
  const res = await fetch(url, { cache: "no-store" });
  const { boards } = await res.json();
  return (
    <div>
      <h1>게시판</h1>
      <ul style={{ display: "flex", flexDirection: "column", gap: 6 }}>
        {boards?.map((b: any) => (
          <li key={b.id}><Link href={`/boards/${b.slug}`}>{b.name}</Link></li>
        ))}
      </ul>
    </div>
  );
}


