"use client";

import React from "react";
import "react-multi-carousel/lib/styles.css";
import "moment/locale/ar";
import {
  type EventData,
} from "./eventListCardTypes";
import EventListTileMedia from "./EventListTileMedia";
import EventCardBody from "./EventCardBody";
import type { EventTitlePair } from "@/features/events/eventRecord";
import { useRouter } from "next/navigation";
import { eventCardLocation } from "../eventCardFields";

/**
 * One event row in the all-events list.
 *
 * The organiser's edit/delete controls are the parent's business — this
 * raises the delete intent rather than owning the confirm dialog.
 */
export default function EventListTile({
  item,
  user,
  selectedLanguage,
  onRequestDelete,
  is_homepage,
}: {
  item: EventData;
  user: { id?: number | string } | null | undefined;
  selectedLanguage: string;
  onRequestDelete: (info: {
    isOpen: boolean;
    eventId?: string;
    eventTitle?: EventTitlePair;
  }) => void;
  is_homepage: boolean;
}) {
  const router = useRouter();
  /* The first interest, in the active language — read twice before (once for
     the label, once inside the handler) and the two reads could disagree. */
  const tagLabel =
    item?.interest_display?.[0]?.[
      selectedLanguage === "ar" ? "value_ar" : "value_en"
    ] ?? "";

  return (
            <div className="content-card flex h-full flex-col" key={item.id}>
              <EventListTileMedia
                item={item}
                user={user}
                selectedLanguage={selectedLanguage}
                onRequestDelete={onRequestDelete}
                is_homepage={is_homepage}
              />
              <EventCardBody
                event={item}
                title={(selectedLanguage === "ar" ? item.title_ar : item.title_en) || ""}
                viewCount={item?.view_count}
                typeLabel={item?.event_type_display?.[selectedLanguage === "ar" ? "value_ar" : "value_en"]}
                locationLabel={eventCardLocation(item, selectedLanguage)}
                tag={tagLabel}
                onTagClick={(tag) =>
                  router.push(`/events-and-activities?tags=${encodeURIComponent(tag)}`)
                }
                detailHref={`/event-details/${item.id}`}
                accent={is_homepage ? "orange" : "primary"}
              />
            </div>
  );
}
