"use client";

import { cn } from "@/lib/utils";
import { ContainerComponent as ContainerComponentType } from "@/types/screen";
import { buildGridClasses, COLUMN_SPAN_VALUES } from "@/lib/constants/columnSpans";

interface ContainerComponentProps {
  component: ContainerComponentType;
  children?: React.ReactNode;
  className?: string;
  onClick?: () => void;
  isSelected?: boolean;
  onMouseDown?: (e: React.MouseEvent) => void;
  isMoving?: boolean;
}

export default function ContainerComponent({
  component,
  children,
  className,
  onClick,
  isSelected,
  onMouseDown,
  isMoving,
}: ContainerComponentProps) {
  // 그리드 클래스 생성
  const gridClasses = component.grid_column_span
    ? buildGridClasses(component.grid_column_span, component.grid_column_start)
    : "";

  // 스타일 객체 생성
  const style: React.CSSProperties = {
    // 🔄 레거시 호환: grid_column_span이 없으면 기존 width 사용
    ...(!component.grid_column_span && {
      gridColumn: `span ${component.size.width}`,
    }),
    minHeight: `${component.size.height}px`,
    ...(component.style as Partial<React.CSSProperties>),
    ...(isMoving && {
      zIndex: 50,
      opacity: 0.8,
      transform: "scale(1.02)",
    }),
  };

  return (
    <div
      className={cn(
        "rounded-lg border-2 border-dashed border-input bg-muted p-4",
        gridClasses, // 🆕 그리드 클래스 추가
        isSelected && "border-primary bg-accent",
        isMoving && "cursor-move",
        className,
      )}
      style={style}
      onClick={onClick}
      onMouseDown={onMouseDown}
    >
      <div className="mb-2 text-xs text-muted-foreground">{component.label || "컨테이너"}</div>
      {children}
    </div>
  );
}
