import { NextRequest, NextResponse } from "next/server";
import { LayoutRegistry } from "@/lib/registry/LayoutRegistry";

/**
 * 현재 등록된 레이아웃 목록 조회 (코드 레벨 + DB)
 */
export async function GET(request: NextRequest) {
  try {
    // 코드 레벨에서 등록된 레이아웃들
    const codeLayouts = LayoutRegistry.getAllLayouts().map((layout) => ({
      id: layout.id,
      name: layout.name,
      nameEng: layout.nameEng,
      description: layout.description,
      category: layout.category,
      type: "code", // 코드로 생성된 레이아웃
      isActive: layout.isActive !== false,
      tags: layout.tags || [],
      metadata: layout.metadata,
      zones: layout.defaultZones?.length || 0,
    }));

    // 레지스트리 통계
    const registryInfo = LayoutRegistry.getRegistryInfo();

    return NextResponse.json({
      success: true,
      data: {
        codeLayouts,
        statistics: {
          total: registryInfo.totalLayouts,
          active: registryInfo.activeLayouts,
          categories: registryInfo.categoryCounts,
          types: registryInfo.registeredTypes,
        },
        summary: {
          codeLayoutCount: codeLayouts.length,
          activeCodeLayouts: codeLayouts.filter((l) => l.isActive).length,
        },
      },
    });
  } catch (error) {
    console.error("레이아웃 목록 조회 오류:", error);
    return NextResponse.json(
      {
        success: false,
        message: "레이아웃 목록 조회에 실패했습니다.",
        error: error instanceof Error ? error.message : "Unknown error",
      },
      { status: 500 },
    );
  }
}
