import type { UserTablo } from "@xtablo/shared/types/tablos.types";
import { ChevronDown, ChevronRight, ChevronUp } from "lucide-react";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { ProjectCard } from "./ProjectCard";

const DEFAULT_VISIBLE = 6;

export interface ProjectCardListProps {
  tablos: UserTablo[];
  onTabloClick?: (tabloId: string) => void;
  onTabloMenuClick?: (tabloId: string) => void;
  onSeeAllClick?: () => void;
}

export function ProjectCardList({
  tablos,
  onTabloClick,
  onTabloMenuClick,
  onSeeAllClick,
}: ProjectCardListProps) {
  const { t } = useTranslation("pages");
  const [expanded, setExpanded] = useState(false);

  const hasMore = tablos.length > DEFAULT_VISIBLE;
  const visibleTablos = expanded ? tablos : tablos.slice(0, DEFAULT_VISIBLE);

  return (
    <div className="mb-8">
      <div className="flex items-center justify-between mb-6">
        <h2 className="text-2xl font-semibold text-gray-900 dark:text-gray-100">
          {t("tablo.projectList.title")}
        </h2>
        {onSeeAllClick && (
          <button
            className="flex items-center gap-1 text-purple-600 hover:text-purple-500 dark:text-purple-400 dark:hover:text-purple-300 font-medium"
            onClick={onSeeAllClick}
          >
            {t("tablo.projectList.seeAll")}
            <ChevronRight className="w-4 h-4" />
          </button>
        )}
      </div>
      <div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
        {visibleTablos.map((tablo) => (
          <ProjectCard
            key={tablo.id}
            tablo={tablo}
            onClick={onTabloClick}
            onMenuClick={onTabloMenuClick}
          />
        ))}
      </div>
      {hasMore && (
        <div className="flex justify-center mt-6">
          <button
            className="flex items-center gap-1.5 text-purple-600 hover:text-purple-500 dark:text-purple-400 dark:hover:text-purple-300 font-medium text-sm"
            onClick={() => setExpanded((prev) => !prev)}
          >
            {expanded ? (
              <>
                {t("tablo.projectList.showLess")}
                <ChevronUp className="w-4 h-4" />
              </>
            ) : (
              <>
                {t("tablo.projectList.showAll", {
                  count: tablos.length - DEFAULT_VISIBLE,
                })}
                <ChevronDown className="w-4 h-4" />
              </>
            )}
          </button>
        </div>
      )}
    </div>
  );
}
