import styles from "./Card.module.css";
import Link from "next/link";
import Thumbnail from "@/components/Thumbnail/Thumbnail";

type CardProps = {
  imageUrl?: string;
  title: string;
  tagline?: string;
  excerpt?: string;
  href?: string | null;
};

const Card = ({
  imageUrl,
  title,
  tagline,
  excerpt,
  href = null,
}: CardProps) => {
  const normalizedHref =
    href && typeof href === "string" && href.trim().length > 0 ? href.trim() : null;


  const content = (
    <>
      {imageUrl && imageUrl.trim().length > 0 && (
        <div className={styles.card__img_wrap}>
          <Thumbnail
            width={473}
            height={473}
            imageUrl={imageUrl}
            alt={title}
          />
        </div>
      )}

      <div className="h3" dangerouslySetInnerHTML={{ __html: title }} />

      {tagline && <h6>{tagline}</h6>}

      {excerpt && <div dangerouslySetInnerHTML={{ __html: excerpt }} />}
    </>
  );

  return normalizedHref ? (
    <Link href={normalizedHref} className={styles.card}>
      {content}
    </Link>
  ) : (
    <div className={styles.card}>{content}</div>
  );
};

export default Card;
