import { IColourRecipe, IPigment } from "@/lib/interfaces/course/course.interface"
import { faNumber } from "@/lib/helpers/format.helper"

export default function PaletteBlock({ palette, recipes }: { palette: IPigment[]; recipes: IColourRecipe[] }) {
  return (
    <div className="flex flex-col [&>*:not(:last-child)]:mb-3">
      {palette.length > 0 && (
        <div className="bg-canvas border border-line rounded-card p-3.5">
          <p className="text-caption font-bold text-ink-soft mb-3">رنگ‌های این دوره</p>
          <div className="grid grid-cols-4 gap-3">
            {palette.map((pigment) => (
              <div key={pigment.key} className="text-center">
                <span className="block w-12 h-12 mx-auto rounded-2xl border border-line shadow-card" style={{ background: pigment.hex }} />
                <p className="text-micro mt-1.5 leading-4">{pigment.name}</p>
              </div>
            ))}
          </div>
        </div>
      )}

      {recipes.length > 0 && (
        <div className="bg-canvas border border-line rounded-card p-3.5">
          <p className="text-caption font-bold text-ink-soft mb-1">دستور ساخت رنگ</p>
          {recipes.map((recipe, index) => (
            <div key={`${recipe.name}-${index}`} className={`flex items-center py-2.5 ${index ? "border-t border-line" : ""}`}>
              <span className="flex shrink-0 rounded-xl overflow-hidden border border-line h-10" aria-hidden="true">
                {recipe.parts.map((part, i) => (
                  <span key={i} className="block h-10" style={{ width: `${Math.max(1, part.parts) * 8}px`, background: part.hex ?? undefined }} />
                ))}
              </span>
              <div className="flex-1 min-w-0 mr-3">
                <p className="text-body font-bold">{recipe.name}</p>
                <p className="text-micro text-ink-soft truncate">
                  {recipe.parts.map((part) => `${faNumber(part.parts)} سهم ${part.name ?? part.pigment ?? ""}`).join(" + ")}
                </p>
              </div>
            </div>
          ))}
        </div>
      )}
    </div>
  )
}
