"use client";

import { useRouter } from "next/navigation";
import { useLanguageStore } from "@/store/languageStore";
import { type EventData } from "./profileEventCardTypes";
import { type DeleteModalInfo } from "./useProfileEventCard";

import React from "react";
import Link from "next/link";
import ProfileEventTileMedia from "./ProfileEventTileMedia";
import EventCardBody from "./EventCardBody";
import { eventCardLocation } from "../eventCardFields";

/**
 * One event tile in a profile's events grid.
 *
 * The paid/free badge is decided by the participation type's English label,
 * which is how the rest of the app reads that choice too.
 */
export default function ProfileEventTile({
  item,
  currentUser,
  onRequestDelete,
  relation,
}: {
  item: EventData;
  /* «منظم» / «راعي» on the cover, from the list the tile is in. */
  relation?: "organizer" | "sponsor" | null;
  /* Only used to decide whether the organiser's own badge shows. */
  currentUser?: { id?: number | string } | null;
  /* Raised to the parent, which owns the confirm dialog. */
  onRequestDelete: (info: DeleteModalInfo) => void;
}) {
  const router = useRouter();
  const selectedLanguage = useLanguageStore((s) => s.language);

            const title =
              item[selectedLanguage === "ar" ? "title_ar" : "title_en"] || "";

            return (
              /*
                A stretched overlay rather than a wrapper. This `<Link>` used
                to contain `ProfileEventTileMedia`, which renders the
                organiser's **delete** control, plus the action button below —
                two real `<button>`s inside an `<a>`. The nesting was invisible
                to a per-file read because the link and the buttons live in
                different components.
              */
              <div
                key={item.id}
                className="content-card h-full relative flex flex-col"
              >
                <Link
                  href={`/event-details/${item.id}`}
                  className="absolute inset-0 z-0 rounded-[20px] focus-visible:outline focus-visible:outline-2 focus-visible:outline-primary-5"
                >
                  <span className="sr-only">{title}</span>
                </Link>
                {/* Let clicks on non-interactive content reach the full-card link. */}
                <div className="pointer-events-none relative z-10 flex h-full flex-col [&_a]:pointer-events-auto [&_button]:pointer-events-auto">
                  <ProfileEventTileMedia
                    item={item}
                    currentUser={currentUser}
                    onRequestDelete={onRequestDelete}
                    selectedLanguage={selectedLanguage}
                    title={title}
                    relation={relation}
                  />

                  <EventCardBody
                    event={item}
                    title={title}
                    viewCount={item.view_count}
                    typeLabel={
                      item.event_type_display?.[
                        selectedLanguage === "ar" ? "value_ar" : "value_en"
                      ]
                    }
                    locationLabel={eventCardLocation(item, selectedLanguage)}
                    tag={
                      item.interest_display?.[0]?.[
                        selectedLanguage === "ar" ? "value_ar" : "value_en"
                      ] ?? ""
                    }
                    onTagClick={(tag) =>
                      router.push(`/events-and-activities?tags=${encodeURIComponent(tag)}`)
                    }
                    detailHref={`/event-details/${item.id}`}
                    accent="orange"
                  />
                </div>
              </div>
            );
}
