"use client";

import React from "react";
import { ComponentData } from "@/types/screen";
import { componentRegistry, ComponentRenderer } from "../DynamicComponentRenderer";
import { LayoutGrid } from "lucide-react";

// 대시보드 컴포넌트 렌더러
const DashboardRenderer: ComponentRenderer = ({ component, children, isInteractive = false, ...props }) => {
  const config = component.componentConfig || {};
  const { columns = 3, gap = 16, items = [], style = {} } = config;

  console.log("📊 DashboardRenderer 렌더링:", {
    componentId: component.id,
    isInteractive,
    config,
    columns,
    gap,
  });

  return (
    <div
      className="h-full w-full overflow-hidden p-4"
      style={{
        display: "grid",
        gridTemplateColumns: `repeat(${columns}, 1fr)`,
        gap: `${gap}px`,
        width: "100%",
        height: "100%",
        boxSizing: "border-box",
        ...style,
      }}
    >
      {children && React.Children.count(children) > 0
        ? children
        : // 플레이스홀더 그리드 아이템들
          Array.from({ length: columns * 2 }).map((_, index) => (
            <div
              key={index}
              className={`flex min-h-0 items-center justify-center rounded p-2 ${
                isInteractive
                  ? "border border-border bg-white shadow-sm"
                  : "border-2 border-dashed border-input bg-muted"
              }`}
              style={{
                minWidth: 0,
                minHeight: "60px",
                maxWidth: "100%",
              }}
            >
              <div className="text-center">
                <LayoutGrid className={`mx-auto mb-2 h-6 w-6 ${isInteractive ? "text-primary" : "text-muted-foreground/70"}`} />
                <div className={`text-xs ${isInteractive ? "font-medium text-foreground" : "text-muted-foreground/70"}`}>
                  그리드 {index + 1}
                </div>
                {isInteractive && <div className="mt-1 text-xs text-muted-foreground">실제 화면</div>}
              </div>
            </div>
          ))}
    </div>
  );
};

// 레지스트리에 등록
componentRegistry.register("dashboard", DashboardRenderer);

export { DashboardRenderer };
