"use client";

import Image from "next/image";
import Link from "next/link";
import { useTranslation } from "react-i18next";
import { Eye } from "lucide-react";
import { Button } from "@/components/ui/Button";
import OpportunityCardTag from "@/features/opportunities/components/OpportunityCardTag";
import { useEventCardAction, type EventCardActionSource } from "./useEventCardAction";

/**
 * The lower half of every event card — the homepage carousel
 * (`HomeEventTile`) and the events list (`EventListTile`) — in the same theme
 * as `OpportunityCard`: grey body and the same fact rows, stacked, with the
 * action as a rounded button in the body's end corner.
 *
 * The two event tiles used to carry their own copy of this markup with a
 * different button each (a square `<span>` on the homepage, a small pill at
 * the corner on the list), so the same event looked different depending on
 * the page. Everything that varies per surface is a prop.
 */
export default function EventCardBody({
  title,
  viewCount,
  typeLabel,
  locationLabel,
  tag,
  onTagClick,
  detailHref,
  accent,
  event,
}: {
  title: string;
  viewCount?: number | string | null;
  typeLabel?: string | null;
  /** Empty hides the row — a new event has a location link only (PDF p.22). */
  locationLabel: string;
  tag: string;
  /** Filters the events list by the tag; omitted where the tag is plain text. */
  onTagClick?: (tag: string) => void;
  detailHref: string;
  /** The homepage draws events in orange; the list follows its own section colour. */
  accent: "orange" | "primary";
  event: EventCardActionSource;
}) {
  const { t } = useTranslation();
  const action = useEventCardAction(event, detailHref);
  const borderClass = accent === "orange" ? "border-primary-801" : "border-primary-5";
  const buttonClass = accent === "orange" ? "bg-primary-801" : "bg-primary-5";
  const iconClass = accent === "orange" ? "text-primary-801" : "text-primary-5";

  /* Same row type as `OpportunityCard`, so the two families line up. */
  const rowClass =
    "flex items-center gap-2 leading-tight text-secondary-102 text-sm xss:text-base lg:text-base 2xl:text-lg";

  return (
    <>
      {/* `border-t-0`: the image above already draws the top edge. */}
      <div
        className={`event-card-body boxshadowsitm relative flex flex-1 flex-col rounded-b-[20px] border border-t-0 bg-[#F7F7F7] px-3 ${borderClass}`}
      >
        {/* A stretched overlay rather than a wrapper, so the tag can stay a
            sibling `<button>` (a button inside a link is invalid content). */}
        <Link href={detailHref} className="absolute inset-0 z-0 rounded-b-[20px]">
          <span className="sr-only">{title}</span>
        </Link>
        <div className="relative z-10 flex flex-1 flex-col">
          <div className="flex items-start justify-between gap-2 pb-1 pt-[19px] 2xl:pb-3">
            <h3 className="min-w-0 text-lg font-bold text-secondary-100 2xl:text-[20px]">
              <span className="line-clamp-1">{title}</span>
            </h3>
            <div className={`${rowClass} shrink-0`}>
              <Eye className={`h-5 w-5 ${iconClass}`} aria-hidden="true" />
              <span>{viewCount ?? 0}</span>
            </div>
          </div>

          {/* The facts stacked, with the action in the end corner beside them
              (client, 2026-10-18) — it used to be a centred pill overlapping
              the card's bottom edge. */}
          <div className="flex flex-1 items-end justify-between gap-3 pb-3">
            <div className="flex min-w-0 flex-col gap-3">
              <div className={rowClass}>
                <Image
                  src="/assets/homepage/learn_type.svg"
                  className="h-5 w-5 shrink-0 object-contain"
                  width={20}
                  height={20}
                  unoptimized
                  alt=""
                  aria-hidden="true"
                />
                <span className="line-clamp-1">{typeLabel}</span>
              </div>
              {locationLabel && <div className={rowClass}>
                <Image
                  src="/assets/homepage/locations.svg"
                  className="h-5 w-5 shrink-0 object-contain"
                  width={20}
                  height={20}
                  unoptimized
                  alt=""
                  aria-hidden="true"
                />
                <span className="line-clamp-1 flex-1 overflow-hidden text-ellipsis">
                  {locationLabel}
                </span>
              </div>}
              <div className={`${rowClass} overflow-hidden`}>
                <Image
                  src="/assets/homepage/health.svg"
                  className="h-5 w-5 shrink-0 object-contain"
                  width={20}
                  height={20}
                  unoptimized
                  alt=""
                  aria-hidden="true"
                />
                <OpportunityCardTag tag={tag} onTagClick={onTagClick} />
              </div>
            </div>

            <Button
              type="button"
              className={`shrink-0 !rounded-full border-0 font-bold ${buttonClass}`}
              variant="primary"
              size="xs"
              aria-busy={action.loading}
              onClick={() => { void action.onClick(); }}
            >
              {/* Client confirmation supersedes the old October label. */}
              {t(action.loading ? "COMMON.LOADING" : action.label)}
            </Button>
          </div>
        </div>
      </div>
    </>
  );
}
