import Link from "next/link";

type ArticleTag = {
  id: number;
  name: string;
};

type ArticleCardProps = {
  article: {
    id?: number;
    title: string;
    description?: string;
    date_creation: Date;
    content: string;
    tags: ArticleTag[];
    image: {
      path: string;
    };
    author?: {
      id?: number;
      name?: string;
      email?: string;
      image?: string;
    };
  };
};

export default function ArticleCard({ article }: ArticleCardProps) {
  return (
    <article className="h-full flex flex-col rounded-2xl shadow-xl overflow-hidden bg-white transition-transform hover:-translate-y-1 hover:shadow-2xl border border-gray-300">
      <Link
        href={`/blog/article?id=${article.id}`}
        className="w-full h-full flex flex-col"
      >
        <div
          className="h-50 bg-cover bg-center relative"
          style={{
            background: `linear-gradient(0deg, #08052379 40%, #00000033), url('${process.env.NEXT_PUBLIC_API_URL}${article.image?.path}')`,
            backgroundPosition: "center",
            backgroundSize: "cover",
          }}
        ></div>
        <div className="flex flex-col flex-1 p-6 pt-4 gap-3 items-stretch">
          <div className="flex flex-wrap gap-2">
            {article.tags.map((tag) => (
              <span
                key={tag.id}
                className="bg-primary rounded-full px-3 py-1 text-xs font-medium text-white"
              >
                {tag.name.trim()}
              </span>
            ))}
          </div>
          <h2 className="text-2xl font-bold text-gray-900 mb-1">
            {article.title}
          </h2>
          <p className="text-gray-600 text-sm line-clamp-3">
            {article.description}
          </p>
          <div className="flex mt-auto mb-0 pt-2 items-center gap-3 justify-between">
            <span className="text-sm text-gray-700">
              Écrit par {article.author?.name}
            </span>
            <span className="text-sm text-gray-700 text-end">
              {article.date_creation
                ? new Date(article.date_creation).toLocaleDateString("fr-FR", {
                  day: "2-digit",
                  month: "long",
                  year: "numeric",
                })
                : "Date inconnue"}
            </span>
          </div>
        </div>
      </Link>
    </article>
  );
}
