"use client";

import Image from "next/image";
import Link from "next/link";
import { useTranslation } from "react-i18next";
import { buttonClasses } from "@/components/ui/Button";
import { formatDateRange } from "@/lib/helpers";
import { clockTimeRange } from "@/features/shared/clockLabel";
import { useLanguageStore } from "@/store/languageStore";
import { listingImage } from "./profileListingImage";
import { asset, type OpportunityItem } from "./commonProfileTypes";

/** One opportunity or event on a public profile. */
export default function ListingCard({ item, isEvent }: { item: OpportunityItem; isEvent: boolean }) {
  const { t } = useTranslation();
  const language = useLanguageStore((state) => state.language);
  const title = item[language === "ar" ? "title_ar" : "title_en"] || "";
  const location = item[language === "ar" ? "location_ar" : "location_en"] || "";
  const status = isEvent ? item.event_status : item.opportunity_status;
  const image = listingImage(item, isEvent);
  const href = isEvent
    ? `/event-details/${item.id}`
    : item.opportunity_type === "volunteer_opportunity"
      ? `/volunteer-event-detail/${item.id}`
      : `/learn-share-event-detail/${item.id}`;
  const detail = isEvent
    ? item.event_type_display
    : item.opportunity_type === "learn_serve_opportunity"
      ? item.learning_type_display
      : item.format_display;

  return (
    // h-full + flex so every card in a row matches the tallest one; the detail
    // rows absorb the slack and the View button stays pinned to the bottom.
    <Link href={href} className="group flex h-full flex-col pb-6">
      <article className="flex flex-1 flex-col overflow-hidden rounded-[20px] border border-primary-5 bg-white shadow-[0_4px_10px_rgba(0,0,0,0.12)]">
        {/* 1:1 — the square every card image in the app uses, so a cover
            uploaded once looks identical here, on the listings and on the
            homepage. It is also what the upload cropper produces, so the card
            never re-crops what the organizer framed. */}
        <div className="relative aspect-square w-full shrink-0 bg-gray-100">
          <Image
            src={image || asset("homepage/treeplanting.png")}
            alt={title}
            fill
            className="object-cover transition-transform group-hover:scale-[1.02]"
            unoptimized
          />
          {status && (
            <span className="absolute left-4 top-4 rounded-full bg-primary-5 px-4 py-1 text-sm font-medium text-white">
              {t(
                status === "inprogress"
                  ? "COMMON.IN_PROGRESS"
                  : status === "completed"
                    ? "COMMON.FINISHED"
                    : "COMMON.UPCOMING"
              )}
            </span>
          )}
          {item.start_date && item.end_date && (
            <div className="absolute inset-x-0 bottom-0 grid h-10 grid-cols-2 items-center bg-black/70 text-center text-xs text-white">
              <span>
                {formatDateRange(item.start_date, item.end_date, language, t)}
              </span>
              <span>
                {clockTimeRange(item.start_time, item.end_time, t)}
              </span>
            </div>
          )}
        </div>
        <div className="flex flex-1 flex-col p-5">
          <h3 className="mb-4 line-clamp-1 text-xl font-bold text-secondary-100">
            {title}
          </h3>
          <div className="flex-1 space-y-3 text-secondary-102">
            {detail && (
              <p className="flex items-center gap-3">
                <Image src={asset("homepage/learn_type.svg")} alt="" width={20} height={20} />
                {detail[language === "ar" ? "value_ar" : "value_en"]}
              </p>
            )}
            {location && (
              <p className="flex items-center gap-3">
                <Image src={asset("homepage/locations.svg")} alt="" width={20} height={20} />
                <span className="line-clamp-1">{location}</span>
              </p>
            )}
            {item.interest_display?.[0] && (
              <p className="flex items-center gap-3">
                <Image src={asset("homepage/health.svg")} alt="" width={20} height={20} />
                {item.interest_display[0][language === "ar" ? "value_ar" : "value_en"]}
              </p>
            )}
          </div>
          <div className="mt-5 flex justify-end">
            {/* A styled `<span>`, not a `<Button>`. It carries no handler — it
                is the card's own "View" affordance, and the whole card is
                already a link to that page. As a `<button>` inside the link's
                `<a>` it was invalid content and a second Tab stop announced as
                a button with no behaviour. `buttonClasses` is the same
                function `Button` uses, so the pill is unchanged. */}
            <span
              className={buttonClasses({
                size: "xss",
                variant: isEvent ? "orange" : "secondarys",
              })}
            >
              {t("COMMON.VIEW")}
            </span>
          </div>
        </div>
      </article>
    </Link>
  );
}

/**
 * Certificates render in their own component that only receives the
 * certificates array — listing items (`ListingCard`) are never passed in, so
 * an opportunity card can't leak into the certificates grid.
 */
