"use client";

import type { DashboardWidgetType } from "@capakraken/shared/types";
import { WIDGET_CATALOG } from "./widget-registry.js";

interface AddWidgetModalProps {
  onAdd: (type: DashboardWidgetType) => void;
  onClose: () => void;
}

export function AddWidgetModal({ onAdd, onClose }: AddWidgetModalProps) {
  return (
    <div
      className="fixed inset-0 bg-black/50 z-50 flex items-center justify-center p-4"
      onClick={(e) => {
        if (e.target === e.currentTarget) onClose();
      }}
    >
      <div className="bg-white rounded-xl shadow-2xl w-full max-w-2xl max-h-[80vh] flex flex-col">
        {/* Header */}
        <div className="flex items-center justify-between px-6 py-4 border-b border-gray-200">
          <h2 className="text-lg font-semibold text-gray-900">Add Widget</h2>
          <button
            type="button"
            onClick={onClose}
            className="text-gray-400 hover:text-gray-600 text-2xl leading-none"
          >
            ×
          </button>
        </div>

        {/* Grid of widgets */}
        <div className="flex-1 overflow-y-auto p-6">
          <div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
            {WIDGET_CATALOG.map((def) => (
              <button
                key={def.type}
                type="button"
                onClick={() => {
                  onAdd(def.type);
                  onClose();
                }}
                className="flex items-start gap-4 p-4 border border-gray-200 rounded-xl hover:border-brand-400 hover:bg-brand-50 transition-colors text-left"
              >
                <span className="text-3xl shrink-0">{def.icon}</span>
                <div>
                  <div className="font-semibold text-gray-900 text-sm">{def.label}</div>
                  <div className="text-xs text-gray-500 mt-1">{def.description}</div>
                  <div className="text-xs text-gray-400 mt-1">
                    Default: {def.defaultSize.w}×{def.defaultSize.h} grid units
                  </div>
                </div>
              </button>
            ))}
          </div>
        </div>
      </div>
    </div>
  );
}
