"use client";

import { type EventData } from "./profileEventCardTypes";
import { type DeleteModalInfo } from "./useProfileEventCard";
import React from "react";
import moment from "moment";
import { useClock } from "@/hooks/useClock";
import { canEditOrDelete, isViewerOrganizer } from "@/features/shared/opportunityButtonState";
import { eventSchedule } from "../eventLifecycle";
import Image from "next/image";
import { useTranslation } from "react-i18next";
import { eventTimeRange } from "../eventClock";
import { formatDateRange } from "@/lib/helpers";
import IconButton from "@/components/ui/IconButton";
import EventPaidBadge from "./EventPaidBadge";
import { eventCardImage } from "../eventCardFields";

/**
 * An event tile's image panel: the cover photo, the status pill and the
 * organiser's edit/delete menu.
 *
 * The menu only renders for the event's own creator, and never once the
 * event has completed — a finished event cannot be edited away.
 */
const asset = (path: string) => `/assets/${path}`;

export default function ProfileEventTileMedia({
  item,
  currentUser,
  onRequestDelete,
  selectedLanguage,
  title,
}: {
  item: EventData;
  currentUser?: { id?: number | string } | null;
  onRequestDelete: (info: DeleteModalInfo) => void;
  selectedLanguage: string;
  title: string;
  relation?: "organizer" | "sponsor" | null;
}) {
  const { t } = useTranslation();
  const clock = useClock(1000);
  const canDelete = Boolean(clock) && canEditOrDelete(eventSchedule(item), moment(clock));

  return (
      <div className="border-[#484848] relative rounded-t-[20px] border-t-[1px] border-l-[1px] border-r-[1px]">
        {currentUser?.id != null && isViewerOrganizer(item, currentUser.id) && (
            <div className="absolute -left-3 -top-3 z-10">
              {/* The twin of `EventListTileMedia`'s delete control, and the
                  same fix: a destructive action must be a real button, named by
                  `aria-label`, with the icon's own `alt` emptied so it is not
                  announced twice. `stopPropagation` keeps the surrounding
                  tile link from firing. */}
              <IconButton
                label={t(canDelete ? "COMMON.DELETE_EVENT" : "COMMON.EDIT_WINDOW_CLOSED")}
                disabled={!canDelete}
                stopPropagation
                className="cursor-pointer disabled:cursor-not-allowed disabled:opacity-40"
                onClick={() =>
                  onRequestDelete({
                    isOpen: true,
                    eventId: String(item.id),
                    eventTitle: {
                      title_en: item.title_en,
                      title_ar: item.title_ar,
                    },
                  })
                }
              >
                <Image
                  src={asset("voluneteerevent/card_delete_icon_orange.svg")}
                  alt=""
                  width={27}
                  height={27}
                  className="w-[27px] h-[27px]"
                />
              </IconButton>
            </div>
          )}
        <Image
          src={
            eventCardImage(item) ||
            asset("voluneteerevent/eventbaner.svg")
          }
          alt={title}
          width={400}
          height={300}
          unoptimized
          /* Square (1:1), matching the upload form and the other cards. */
          className="w-full aspect-square border border-[#484848] border-b-0 rounded-t-[20px] object-cover"
        />

        <EventPaidBadge
          participationTypeEn={item?.participation_type_display?.value_en}
          paidRegistration={item?.paid_registration}
        />

        <div className="card-schedule-strip grid grid-cols-2 text-sm text-gray-600 bg-[#000000B2]/70 absolute w-full bottom-0 h-[39px] items-center">
          <span className="flex miniscreen3:text-[12px] smallscreen:text-[10px] laptopitms:text-xs text-white justify-center gap-2 items-center 2xl:text-base laptopmain:text-sm md:text-sm miniscreen1:text-[13px] xs:text-xs">
            <Image
              className="smallscreen:text-[12px] 2xl:w-5 2xl:h-5 w-4 h-4"
              src={asset("homepage/dateicn.svg")}
              alt=""
              width={20}
              height={20}
            />
            {formatDateRange(
              item.start_date,
              item.end_date,
              selectedLanguage,
              t
            )}
          </span>
          <div className="h-[60%] w-[1px] bg-white absolute left-1/2 top-[8px]" />
          <span className="text-white miniscreen3:text-[12px] smallscreen:text-[10px] laptopitms:text-xs flex justify-center gap-2 items-center 2xl:text-base laptopmain:text-sm md:text-sm miniscreen1:text-[13px] xs:text-xs">
            <Image
              src={asset("homepage/timeicn.svg")}
              className="smallscreen:text-[12px] 2xl:w-5 2xl:h-5 w-4 h-4"
              alt=""
              width={20}
              height={20}
            />
            {eventTimeRange(item.start_time, item.end_time, t)}
          </span>
        </div>
      </div>
  );
}
