"use client";

import { Exercise } from "@prisma/client";
import Link from "next/link";
import { Dumbbell } from "lucide-react";

const TYPE_COLORS: Record<string, string> = {
  barbell: "bg-red-900/50 text-red-400",
  dumbbell: "bg-blue-900/50 text-blue-400",
  machine: "bg-purple-900/50 text-purple-400",
  cable: "bg-amber-900/50 text-amber-400",
  bodyweight: "bg-green-900/50 text-green-400",
  cardio: "bg-orange-900/50 text-orange-400",
  kettlebell: "bg-yellow-900/50 text-yellow-400",
  other: "bg-zinc-800 text-zinc-400",
};

export default function ExerciseCard({ exercise }: { exercise: Exercise }) {
  const muscleGroups = JSON.parse(exercise.muscleGroups || "[]") as string[];
  const typeColor = TYPE_COLORS[exercise.type] || TYPE_COLORS.other;

  return (
    <Link href={`/main/exercises/${exercise.id}`}>
      <div className="bg-zinc-900 border border-zinc-800 rounded-lg hover:border-zinc-700 transition p-4 h-full flex flex-col cursor-pointer">
        <div className="flex items-start gap-3 mb-3">
          <div className="bg-zinc-800 p-2 rounded-lg">
            <Dumbbell className="w-5 h-5 text-zinc-400" />
          </div>
          <div className="flex-1 min-w-0">
            <h3 className="font-semibold text-white truncate">
              {exercise.name}
            </h3>
            <div
              className={`inline-block mt-1 px-2 py-0.5 rounded text-xs font-medium ${typeColor}`}
            >
              {exercise.type.charAt(0).toUpperCase() + exercise.type.slice(1)}
            </div>
          </div>
        </div>

        {exercise.description && (
          <p className="text-sm text-zinc-500 mb-3 line-clamp-2">
            {exercise.description}
          </p>
        )}

        {muscleGroups.length > 0 && (
          <div className="flex flex-wrap gap-1.5 mt-auto">
            {muscleGroups.slice(0, 3).map((group) => (
              <span
                key={group}
                className="inline-block px-2 py-0.5 bg-zinc-800 text-zinc-400 rounded text-xs"
              >
                {group.charAt(0).toUpperCase() + group.slice(1)}
              </span>
            ))}
            {muscleGroups.length > 3 && (
              <span className="inline-block px-2 py-0.5 bg-zinc-800 text-zinc-500 rounded text-xs">
                +{muscleGroups.length - 3}
              </span>
            )}
          </div>
        )}
      </div>
    </Link>
  );
}
