import { Database } from "@xtablo/shared/types/database.types";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { ClickOutside } from "./ClickOutside";
import { ImageColorPicker } from "./ImageColorPicker";

type Tablo = Database["public"]["Tables"]["tablos"]["Row"];

interface CreateTabloModalProps {
  onClose: () => void;
  onCreate: (tabloData: Pick<Tablo, "name" | "color" | "image">) => void;
}

export const CreateTabloModal = ({ onClose, onCreate }: CreateTabloModalProps) => {
  const { t } = useTranslation(["modals", "common"]);
  const [newTabloName, setNewTabloName] = useState("");
  const [creationMode, setCreationMode] = useState<"image" | "color">("color");
  const [selectedImage, setSelectedImage] = useState(
    "https://images.unsplash.com/photo-1553877522-43269d4ea984?w=400&h=250&fit=crop&crop=center"
  );
  const [selectedColor, setSelectedColor] = useState("bg-blue-500");

  const resetForm = () => {
    setNewTabloName("");
    setCreationMode("color");
    setSelectedImage(
      "https://images.unsplash.com/photo-1553877522-43269d4ea984?w=400&h=250&fit=crop&crop=center"
    );
    setSelectedColor("bg-blue-500");
  };

  const handleClose = () => {
    resetForm();
    onClose();
  };

  const handleCreate = () => {
    if (newTabloName.trim()) {
      const tabloData = {
        name: newTabloName.trim(),
        // Note: status is now computed from etapes and will default to 'todo' until etapes are created
        ...(creationMode === "image"
          ? { image: selectedImage, color: null }
          : { image: null, color: selectedColor }),
      };
      onCreate(tabloData);
      resetForm();
    }
  };

  return (
    <div className="fixed inset-0 bg-black/80 flex items-center justify-center z-50">
      <ClickOutside onClickOutside={handleClose}>
        <div className="bg-white dark:bg-gray-800 rounded-lg shadow-xl p-6 w-full max-w-4xl min-w-96 mx-4">
          <h2 className="text-xl font-bold text-gray-900 dark:text-white mb-4">
            {t("modals:createTablo.title")}
          </h2>

          <div className="space-y-4">
            {/* Name Input */}
            <div>
              <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                {t("modals:createTablo.nameLabel")}
              </label>
              <input
                type="text"
                value={newTabloName}
                onChange={(e) => setNewTabloName(e.target.value)}
                className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:text-white"
                placeholder={t("modals:createTablo.namePlaceholder")}
                autoFocus
              />
            </div>

            <ImageColorPicker
              creationMode={creationMode}
              setCreationMode={setCreationMode}
              selectedColor={selectedColor}
              setSelectedColor={setSelectedColor}
            />
          </div>

          {/* Modal Actions */}
          <div className="flex justify-end space-x-3 mt-6">
            <button
              type="button"
              className="px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 bg-gray-100 dark:bg-gray-700 hover:bg-gray-200 dark:hover:bg-gray-600 rounded-md"
              onClick={handleClose}
            >
              {t("common:buttons.cancel")}
            </button>
            <button
              type="button"
              className="px-4 py-2 text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 rounded-md disabled:opacity-50 disabled:cursor-not-allowed"
              onClick={handleCreate}
              disabled={!newTabloName.trim() || creationMode === "image"}
            >
              {t("common:buttons.create")}
            </button>
          </div>
        </div>
      </ClickOutside>
    </div>
  );
};
