"use client";

import { useState } from "react";
import Image from "next/image";
import Link from "next/link";
import {
  ArrowLeft,
  Newspaper,
  Radio,
  Video,
  Calendar,
  Clock,
  X,
  ExternalLink,
} from "lucide-react";
import { cn } from "@/lib/utils";
import type { PresseItem } from "@/lib/presse-types";
import { formatDatePresse } from "@/lib/formatDatePresse";

const getTypeColor = (type: string) => {
  switch (type) {
    case "radio":
      return "bg-orange text-white";
    case "video":
      return "bg-turquoise text-white";
    default:
      return "bg-violet text-white";
  }
};

const getExternalLinkLabel = (type: string) => {
  switch (type) {
    case "radio":
      return "Écouter l'émission";
    case "video":
      return "Voir la vidéo";
    default:
      return "Lire l'article complet";
  }
};

interface PresseDetailClientProps {
  item: PresseItem;
}

export default function PresseDetailClient({ item }: PresseDetailClientProps) {
  const [isZoomed, setIsZoomed] = useState(false);

  if (!item || typeof item !== "object") {
    return (
      <div className="flex flex-col gap-8 w-full max-w-4xl">
        <Link
          href="/presse"
          className="flex items-center gap-2 text-violet hover:text-turquoise transition-colors w-fit"
        >
          <ArrowLeft size={20} />
          <span>Retour à la revue de presse</span>
        </Link>
        <p className="text-zinc-600 dark:text-zinc-400">Entrée introuvable.</p>
      </div>
    );
  }

  const imageSrc = item.image && String(item.image).trim() ? item.image : null;

  return (
    <>
      <div className="flex flex-col gap-8 w-full max-w-4xl">
        <Link
          href="/presse"
          className="flex items-center gap-2 text-violet hover:text-turquoise transition-colors w-fit"
        >
          <ArrowLeft size={20} />
          <span>Retour à la revue de presse</span>
        </Link>

        <article className="flex flex-col gap-6">
          <div className="flex items-center gap-4 flex-wrap">
            <div
              className={cn(
                "px-4 py-2 rounded-full flex items-center gap-2 font-medium",
                getTypeColor(item.type)
              )}
            >
              {item.type === "radio" ? (
                <Radio size={20} />
              ) : item.type === "video" ? (
                <Video size={20} />
              ) : (
                <Newspaper size={20} />
              )}
              <span className="capitalize">{item.type}</span>
            </div>
            <div className="flex items-center gap-2 text-zinc-600 dark:text-zinc-400">
              <Calendar size={18} />
              <span>{formatDatePresse(item.date)}</span>
            </div>
            <div className="text-zinc-600 dark:text-zinc-400">
              {item.source}
            </div>
            {item.duration != null &&
              item.duration !== "" &&
              String(item.duration).toLowerCase() !== "null" && (
                <div className="flex items-center gap-2 text-zinc-600 dark:text-zinc-400">
                  <Clock size={18} />
                  <span>{item.duration}</span>
                </div>
              )}
          </div>

          <h1 className="text-4xl font-semibold leading-tight text-foreground">
            {item.title}
          </h1>

          <div
            className="relative w-full h-96 rounded-lg overflow-hidden cursor-zoom-in group bg-zinc-100"
            onClick={() => setIsZoomed(true)}
          >
            {imageSrc ? (
              <Image
                src={imageSrc}
                alt={item.title ?? ""}
                fill
                className="object-cover object-top"
                unoptimized
              />
            ) : (
              <div className="absolute inset-0 flex items-center justify-center text-zinc-400">
                <Newspaper size={64} />
              </div>
            )}
            {item.type === "video" && (
              <div className="absolute inset-0 flex items-center justify-center bg-black/30 pointer-events-none">
                <div className="bg-turquoise text-white rounded-full p-6 hover:scale-110 transition-transform">
                  <Video size={48} />
                </div>
              </div>
            )}
            {item.type === "radio" && (
              <div className="absolute inset-0 flex items-center justify-center bg-black/30 pointer-events-none">
                <div className="bg-orange text-white rounded-full p-6 hover:scale-110 transition-transform">
                  <Radio size={48} />
                </div>
              </div>
            )}
            {item.type === "article" && (
              <div className="absolute top-4 right-4 bg-black/70 text-white p-3 rounded-full opacity-0 group-hover:opacity-100 transition-opacity">
                <span className="text-sm">Agrandir</span>
              </div>
            )}
          </div>

          {item.content && (
            <div className="text-zinc-700 dark:text-zinc-300 leading-7 whitespace-pre-line">
              {item.content}
            </div>
          )}

          {typeof item.externalUrl === "string" &&
            item.externalUrl.trim() !== "" &&
            (item.externalUrl.startsWith("http://") ||
              item.externalUrl.startsWith("https://")) && (
              <a
                href={item.externalUrl}
                target="_blank"
                rel="noopener noreferrer"
                className={cn(
                  "inline-flex items-center gap-2 px-5 py-3 rounded-lg font-medium transition-colors",
                  item.type === "radio"
                    ? "bg-orange text-white hover:bg-orange/90"
                    : item.type === "video"
                      ? "bg-turquoise text-white hover:bg-turquoise/90"
                      : "bg-violet text-white hover:bg-violet/90"
                )}
              >
                <ExternalLink size={20} />
                {getExternalLinkLabel(item.type)}
              </a>
            )}
        </article>
      </div>

      {/* Modal de zoom */}
      {isZoomed && (
        <div
          className="fixed inset-0 z-50 bg-black/90 flex items-center justify-center p-4"
          onClick={() => setIsZoomed(false)}
        >
          <button
            className="absolute top-4 right-4 text-white hover:text-zinc-300 transition-colors z-10"
            onClick={() => setIsZoomed(false)}
            aria-label="Fermer"
          >
            <X size={32} />
          </button>
          <div
            className="relative w-full h-full max-w-7xl max-h-[90vh]"
            onClick={(e) => e.stopPropagation()}
          >
            {imageSrc ? (
              <Image
                src={imageSrc}
                alt={item.title ?? ""}
                fill
                className="object-contain"
                unoptimized
              />
            ) : (
              <div className="absolute inset-0 flex items-center justify-center text-white">
                <Newspaper size={64} />
              </div>
            )}
          </div>
        </div>
      )}
    </>
  );
}
