"use client";

import type moment from "moment";
import type { ReactNode } from "react";
import Image from "next/image";
import Link from "next/link";
import { useTranslation } from "react-i18next";
import { useAuthStore } from "@/store/authStore";
import { formatDateRange, formatPriceTag } from "@/lib/helpers";
import OpportunityBadges from "./OpportunityBadges";
import {
  CARD_TAG_CLASS,
  cardStatusBadges,
  type CardBadgePerspective,
} from "@/features/opportunities/cardStatusBadges";
import {
  formatTimeRange,
  type OpportunityCardItem,
} from "./opportunityCardTypes";

interface OpportunityCardCoverProps {
  item: OpportunityCardItem;
  /** Alt text — the card's localized title, resolved by the parent. */
  title: string;
  /** Anything the caller draws over the cover, e.g. the creator's action menu. */
  headerOverlay?: ReactNode;
  detailHref: string;
  language: string;
  isLearnServe?: boolean;
  badgePerspective?: CardBadgePerspective;
  /** The action button's i18n key: a state pill saying the same is dropped. */
  buttonLabelKey?: string;
  now?: moment.Moment;
}

/**
 * The card's cover: the photo, the date/time strip across its foot, and the
 * classification badges.
 *
 * Square by design — these photos are cropped 1:1 by the upload form, so the
 * card's native ratio avoids a second crop.
 */
export default function OpportunityCardCover({
  item,
  title,
  headerOverlay,
  detailHref,
  language,
  isLearnServe = false,
  badgePerspective = "viewer",
  buttonLabelKey,
  now,
}: OpportunityCardCoverProps) {
  const { t } = useTranslation();
  // «انتهت» on the cover above «انتهت» on the button says it twice (client,
  // 2026-10-18); the pill stays where the button reads «التفاصيل».
  const userType = useAuthStore((s) => s.user?.user_type);
  const statusBadges = cardStatusBadges(
    item,
    badgePerspective,
    isLearnServe,
    userType ? userType !== "organization" : undefined,
    now
  ).filter(
    (badge) => !(badge.tone === "state" && badge.label === buttonLabelKey)
  );

  return (
      <div className="relative">
        <Link href={detailHref}>
          {/* Square (1:1) — the ratio the upload form crops to, so the card
              never letterboxes, stretches or re-crops. */}
          <div className="relative aspect-square w-full overflow-hidden rounded-t-[20px] border border-b-0 border-[#484848]">
            <Image
              src={
                item.opportunity_images?.[0]?.image ||
                "/assets/homepage/baner_img.png"
              }
              alt={title}
              fill
              sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw"
              className="object-cover"
              loading="lazy"
            />
          </div>
          <OpportunityBadges item={item} />
          {/* The status pills, on the corner the classification icons leave
              free (physical left — those sit on the right in both
              directions). */}
          {/* `items-stretch` in a shrink-to-fit box: every pill takes the
              widest one's width (client, 2026-10-18). */}
          {statusBadges.length > 0 && (
            <div className="absolute left-4 top-4 flex flex-col items-stretch gap-2">
              {statusBadges.map((badge) => (
                <span
                  key={badge.label}
                  className={`whitespace-nowrap rounded-full px-3 py-1 text-center text-sm leading-tight shadow-md 2xl:text-base ${
                    CARD_TAG_CLASS[badge.tone]
                  }`}
                >
                  {(badge.text && formatPriceTag(badge.text, t)) || t(badge.label)}
                </span>
              ))}
            </div>
          )}
          <div className="card-schedule-strip absolute bottom-0 grid h-[39px] w-full grid-cols-2 items-center bg-[#000000B2]/70 text-sm">
            <span className="flex items-center justify-center gap-1 text-white xs:text-sm smallscreen:text-[12px] miniscreen3:text-[14px] miniscreen1:text-[14px] laptopitms:text-sm md:text-md laptopmain:text-md 2xl:text-md">
              <Image className="h-4 w-4 smallscreen:w-3 2xl:h-5 2xl:w-5" src="/assets/homepage/dateicn.svg" alt="" width={20} height={20} unoptimized aria-hidden="true" />
              {formatDateRange(
                item.start_date ?? "",
                item.end_date ?? "",
                language,
                t
              )}
            </span>
            <div className="absolute left-1/2 top-[8px] h-[60%] w-[1px] bg-white" />
            <span className="flex items-center justify-center gap-1 text-white xs:text-sm smallscreen:text-[12px] miniscreen3:text-[14px] miniscreen1:text-[14px] laptopitms:text-sm md:text-md laptopmain:text-md 2xl:text-md">
              <Image
                src="/assets/homepage/timeicn.svg"
                className="h-4 w-4 smallscreen:w-3 2xl:h-5 2xl:w-5"
                alt=""
                width={20}
                height={20}
                unoptimized
                aria-hidden="true"
              />
              {formatTimeRange(item.start_time, item.end_time, t)}
            </span>
          </div>
        </Link>
        {headerOverlay}
      </div>
  );
}
