import Link from "@/components/common/versioned-link"
import SafeImage from "@/components/common/safe-img"
import { CourseCardDto } from "@/lib/dto/catalogue.dto"
import { faNumber, toman } from "@/lib/helpers/format.helper"
import { cn } from "@/lib/utils"

/** Course card (DESIGN.md §11): image with the medium, title, coach, then price or progress. */
export default function CourseCard({ course, variant = "carousel" }: { course: CourseCardDto; variant?: "carousel" | "grid" }) {
  const progress = course.progress_percent
  const grid = variant === "grid"

  return (
    <Link
      href={`/course/detail?id=${course.id}`}
      className={cn(
        "shrink-0 bg-canvas border border-line rounded-card shadow-card overflow-hidden active:scale-[0.97] transition flex flex-col",
        grid ? "w-full" : "w-[232px]"
      )}
    >
      <div className={cn("relative bg-paper-2", grid ? "aspect-[4/3]" : "h-[150px]")}>
        <SafeImage src={course.cover} />
        {course.medium && (
          <span className="absolute top-2.5 right-2.5 px-2 py-0.5 rounded-full text-micro font-bold bg-canvas/95 text-ink">
            {course.medium.label}
          </span>
        )}
      </div>

      <div className={cn("flex-1 flex flex-col", grid ? "p-2.5" : "p-3")}>
        <p className={cn("font-extrabold", grid ? "text-body line-clamp-2 leading-6" : "text-title-3 truncate")}>{course.title}</p>
        <p className="text-caption text-ink-soft truncate">{course.coach_name ?? ""}</p>

        {progress !== null ? (
          <div className="mt-auto pt-2">
            <div className="h-1.5 rounded-full bg-paper-2 overflow-hidden">
              <div className={`h-full rounded-full ${progress >= 100 ? "bg-sage" : "bg-sienna"}`} style={{ width: `${progress}%` }} />
            </div>
            <p className="text-micro text-ink-soft mt-1">{faNumber(progress)}٪ پیش رفته‌ای</p>
          </div>
        ) : (
          <p className="text-body font-extrabold mt-auto pt-1.5">{toman(course.price)}</p>
        )}
      </div>
    </Link>
  )
}
