"use client";

import React, { useState } from "react";
import Image from "next/image";
import Link from "next/link";
import {
  ArrowLeft,
  CalendarBlank,
  User,
  Eye,
  WhatsappLogo,
  ShareNetwork,
  ArrowRight,
  Sparkle,
  YoutubeLogo,
  PlayCircle,
  ImageSquare,
  ArrowSquareOut,
  VideoCamera,
  Check,
} from "@phosphor-icons/react";
import { useVillageStore } from "@/lib/data-store";
import {
  findArticleBySlug,
  INITIAL_ARTICLES,
  CANONICAL_SITE_URL,
  type Article,
} from "@/lib/articles";
import { formatDateID, getYouTubeEmbedUrl, isYouTubeUrl, isVideoFile, cn } from "@/lib/utils";
import { ArticleShareModal } from "@/components/article-share-modal";

interface ArticleDetailClientProps {
  slug: string;
  initialArticle?: Article;
}

export default function ArticleDetailClient({
  slug,
  initialArticle,
}: ArticleDetailClientProps) {
  const { articles, isLoaded } = useVillageStore();

  const allArticles: Article[] =
    articles && articles.length > 0 ? articles : INITIAL_ARTICLES;

  // Prioritize article from store (contains user additions & latest admin updates)
  const article =
    findArticleBySlug(slug, allArticles) ||
    initialArticle;

  const relatedArticles = allArticles.filter((a) => a.slug !== article?.slug).slice(0, 3);

  const [activeMedia, setActiveMedia] = useState<"photo" | "video">("photo");
  const [isAutoPlaying, setIsAutoPlaying] = useState(false);
  const [isShareModalOpen, setIsShareModalOpen] = useState(false);
  const [copiedLink, setCopiedLink] = useState(false);

  if (!article) {
    if (!isLoaded) {
      return (
        <div className="py-24 px-4 max-w-xl mx-auto text-center space-y-4">
          <div className="w-10 h-10 border-4 border-emerald-600 border-t-transparent rounded-full animate-spin mx-auto" />
          <h2 className="text-xl font-bold text-slate-800">Memuat Warta...</h2>
          <p className="text-xs text-slate-500">Memperbarui data warta resmi Desa Kadugenep.</p>
        </div>
      );
    }

    return (
      <div className="py-20 px-4 max-w-xl mx-auto text-center space-y-4">
        <h2 className="text-xl font-bold text-slate-800">Warta Tidak Ditemukan</h2>
        <p className="text-xs text-slate-500">Halaman warta resmi Desa Kadugenep tidak ditemukan atau telah diperbarui.</p>
        <Link
          href="/"
          className="inline-block px-4 py-2 rounded-xl bg-emerald-700 text-white text-xs font-bold"
        >
          Kembali ke Beranda
        </Link>
      </div>
    );
  }

  const isYouTube = isYouTubeUrl(article.videoUrl);
  const isLocalVideo = isVideoFile(article.videoUrl);
  const hasVideo = isYouTube || isLocalVideo;
  const embedUrl = isYouTube ? getYouTubeEmbedUrl(article.videoUrl, isAutoPlaying) : null;

  // Resolve public share URL with canonical https://www.desakadugenep.my.id
  const getPublicShareUrl = () => {
    return `${CANONICAL_SITE_URL}/berita/${article.slug}?v=warta`;
  };

  const shareUrl = getPublicShareUrl();

  const handleCopy = async () => {
    try {
      await navigator.clipboard.writeText(shareUrl);
      setCopiedLink(true);
      setTimeout(() => setCopiedLink(false), 2000);
    } catch {
      alert("Tautan artikel berhasil disalin!");
    }
  };

  return (
    <>
      <article className="py-10 md:py-16 px-4 sm:px-6 lg:px-8 max-w-4xl mx-auto">
        {/* Back button */}
        <Link
          href="/#berita"
          className="inline-flex items-center gap-2 text-xs font-semibold text-slate-500 hover:text-emerald-800 transition-colors mb-6 group"
        >
          <ArrowLeft size={16} className="group-hover:-translate-x-1 transition-transform" />
          <span>Kembali ke Warta Desa</span>
        </Link>

        {/* Category and Date */}
        <div className="flex flex-wrap items-center gap-3 mb-4">
          <span className="px-3 py-1 rounded-full text-xs font-bold uppercase tracking-wider bg-[#064e3b] text-emerald-100">
            {article.category}
          </span>
          <div className="flex items-center gap-2 text-xs text-slate-500">
            <CalendarBlank size={14} />
            <span>{formatDateID(article.date)}</span>
          </div>
          <div className="flex items-center gap-1.5 text-xs text-slate-500">
            <Eye size={14} />
            <span>{article.views} pembaca</span>
          </div>
          {hasVideo && (
            <span className="inline-flex items-center gap-1.5 px-2.5 py-0.5 rounded-full text-[11px] font-bold bg-rose-50 text-rose-700 border border-rose-200">
              {isYouTube ? (
                <YoutubeLogo size={14} weight="fill" className="text-rose-600" />
              ) : (
                <VideoCamera size={14} weight="fill" className="text-rose-600" />
              )}
              <span>{isYouTube ? "Ada Video YouTube" : "Ada Video Dokumentasi"}</span>
            </span>
          )}
        </div>

        {/* Headline */}
        <h1 className="text-3xl sm:text-4xl md:text-5xl font-black text-slate-900 tracking-tight leading-tight mb-4">
          {article.title}
        </h1>

        {/* Author and Share */}
        <div className="flex flex-wrap items-center justify-between gap-4 py-4 border-y border-slate-200 mb-6">
          <div className="flex items-center gap-2.5">
            <div className="w-9 h-9 rounded-full bg-emerald-100 text-emerald-800 flex items-center justify-center font-bold text-sm">
              <User size={18} />
            </div>
            <div>
              <p className="text-xs font-bold text-slate-900">{article.author}</p>
              <p className="text-[11px] text-slate-500">Humas Pemerintah Desa Kadugenep</p>
            </div>
          </div>

          <div className="flex items-center gap-2">
            <button
              type="button"
              onClick={() => setIsShareModalOpen(true)}
              className="inline-flex items-center gap-1.5 px-3.5 py-1.5 rounded-xl bg-emerald-600 hover:bg-emerald-700 active:scale-95 text-white text-xs font-semibold shadow-sm transition-all cursor-pointer"
            >
              <WhatsappLogo size={16} weight="fill" />
              <span>Bagikan</span>
            </button>
            <button
              type="button"
              onClick={handleCopy}
              className="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-xl bg-slate-100 hover:bg-slate-200 text-slate-700 text-xs font-semibold border border-slate-200 transition-colors cursor-pointer"
            >
              {copiedLink ? <Check size={16} className="text-emerald-600" /> : <ShareNetwork size={16} />}
              <span>{copiedLink ? "Link Disalin!" : "Salin Link"}</span>
            </button>
          </div>
        </div>

        {/* Media Selector Tabs (If video is available) */}
        {hasVideo && (
          <div className="flex flex-wrap items-center justify-between gap-2 mb-3">
            <div className="inline-flex items-center p-1 rounded-xl bg-slate-100 border border-slate-200/80 shadow-xs">
              <button
                type="button"
                onClick={() => {
                  setActiveMedia("photo");
                  setIsAutoPlaying(false);
                }}
                className={cn(
                  "flex items-center gap-1.5 px-3.5 py-1.5 rounded-lg text-xs font-bold transition-all cursor-pointer",
                  activeMedia === "photo"
                    ? "bg-white text-slate-900 shadow-xs"
                    : "text-slate-600 hover:text-slate-900"
                )}
              >
                <ImageSquare size={16} weight={activeMedia === "photo" ? "fill" : "regular"} />
                <span>Foto Liputan</span>
              </button>
              <button
                type="button"
                onClick={() => {
                  setActiveMedia("video");
                  setIsAutoPlaying(true);
                }}
                className={cn(
                  "flex items-center gap-1.5 px-3.5 py-1.5 rounded-lg text-xs font-bold transition-all cursor-pointer",
                  activeMedia === "video"
                    ? "bg-rose-600 text-white shadow-xs"
                    : "text-rose-700 hover:bg-rose-50"
                )}
              >
                {isYouTube ? (
                  <YoutubeLogo size={17} weight="fill" />
                ) : (
                  <VideoCamera size={17} weight="fill" />
                )}
                <span>Video Dokumentasi</span>
                <span className="w-2 h-2 rounded-full bg-rose-400 animate-pulse" />
              </button>
            </div>

            {isYouTube ? (
              <a
                href={article.videoUrl}
                target="_blank"
                rel="noopener noreferrer"
                className="hidden sm:inline-flex items-center gap-1 text-[11px] font-semibold text-slate-500 hover:text-rose-700 transition-colors"
              >
                <span>Buka di YouTube</span>
                <ArrowSquareOut size={13} />
              </a>
            ) : (
              <a
                href={article.videoUrl}
                target="_blank"
                rel="noopener noreferrer"
                className="hidden sm:inline-flex items-center gap-1 text-[11px] font-semibold text-slate-500 hover:text-rose-700 transition-colors"
              >
                <span>Buka Video Asli</span>
                <ArrowSquareOut size={13} />
              </a>
            )}
          </div>
        )}

        {/* Hero Media Display (Toggle between Photo and Video) */}
        {activeMedia === "video" && hasVideo ? (
          <div className="mb-8 space-y-2">
            <div className="relative aspect-[16/9] rounded-3xl overflow-hidden border border-slate-800 shadow-2xl bg-black">
              {isYouTube && embedUrl ? (
                <iframe
                  src={embedUrl}
                  title={article.videoTitle || article.title}
                  className="w-full h-full border-0"
                  allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
                  allowFullScreen
                />
              ) : (
                <video
                  src={article.videoUrl}
                  controls
                  autoPlay={isAutoPlaying}
                  playsInline
                  poster={article.image}
                  className="w-full h-full object-contain bg-black"
                >
                  Browser Anda tidak mendukung pemutar video HTML5.
                </video>
              )}
            </div>
            <div className="flex items-center justify-between text-xs text-slate-500 px-2">
              <p className="flex items-center gap-1.5 font-medium">
                <VideoCamera size={15} className="text-rose-600" />
                <span>{article.videoTitle || "Dokumentasi Resmi Desa Kadugenep"}</span>
              </p>
              <button
                type="button"
                onClick={() => setActiveMedia("photo")}
                className="text-emerald-700 hover:underline font-semibold text-[11px] cursor-pointer"
              >
                Kembali ke Foto
              </button>
            </div>
          </div>
        ) : (
          <div className="relative aspect-[16/9] rounded-3xl overflow-hidden mb-8 border border-slate-200 shadow-md group">
            <Image
              src={article.image || "/images/hero-kadugenep.jpg"}
              alt={article.title}
              fill
              priority
              unoptimized={Boolean(article.image?.startsWith("/uploads/") || article.image?.startsWith("data:"))}
              sizes="(max-width: 1024px) 100vw, 800px"
              className="object-cover"
            />
            {hasVideo && (
              <div className="absolute inset-0 bg-gradient-to-t from-black/60 via-black/20 to-transparent flex items-center justify-center p-4">
                <button
                  type="button"
                  onClick={() => {
                    setActiveMedia("video");
                    setIsAutoPlaying(true);
                  }}
                  className="inline-flex items-center gap-3 px-5 py-3 rounded-2xl bg-white/95 hover:bg-white text-slate-900 shadow-2xl backdrop-blur-md font-bold text-xs sm:text-sm hover:scale-105 active:scale-95 transition-all cursor-pointer border border-white/80"
                >
                  <div className="w-9 h-9 rounded-full bg-rose-600 text-white flex items-center justify-center shadow-lg">
                    <PlayCircle size={24} weight="fill" />
                  </div>
                  <div className="text-left">
                    <p className="font-extrabold text-slate-900 leading-tight">Putar Video Dokumentasi</p>
                    <p className="text-[10px] text-slate-500 font-semibold">Tonton langsung di web tanpa berpindah halaman</p>
                  </div>
                </button>
              </div>
            )}
          </div>
        )}

        {/* Content Body */}
        <div className="prose prose-slate max-w-none text-slate-700 text-base leading-relaxed space-y-4 pb-12 border-b border-slate-200">
          <p className="font-semibold text-lg text-slate-900 leading-relaxed">
            {article.summary}
          </p>

          {article.content.split("\n\n").map((para, idx) => (
            <p key={idx} className="whitespace-pre-line">
              {para.trim()}
            </p>
          ))}

          {/* Dedicated Embedded Video Showcase Card inside content */}
          {hasVideo && activeMedia === "photo" && (
            <div className="my-8 p-6 rounded-3xl bg-slate-900 text-white shadow-xl border border-slate-800 space-y-4 not-prose">
              <div className="flex flex-col sm:flex-row sm:items-center justify-between gap-3 pb-4 border-b border-slate-800">
                <div className="flex items-center gap-2.5">
                  <div className="w-10 h-10 rounded-xl bg-rose-600/20 text-rose-400 border border-rose-500/30 flex items-center justify-center">
                    {isYouTube ? (
                      <YoutubeLogo size={22} weight="fill" />
                    ) : (
                      <VideoCamera size={22} weight="fill" />
                    )}
                  </div>
                  <div>
                    <h3 className="text-sm font-bold text-white">Liputan Video Dokumentasi</h3>
                    <p className="text-xs text-slate-400">
                      {article.videoTitle || "Tonton liputan terkait artikel ini langsung di website."}
                    </p>
                  </div>
                </div>
                <button
                  type="button"
                  onClick={() => {
                    setActiveMedia("video");
                    setIsAutoPlaying(true);
                    window.scrollTo({ top: 220, behavior: "smooth" });
                  }}
                  className="inline-flex items-center justify-center gap-2 px-4 py-2 rounded-xl bg-rose-600 hover:bg-rose-500 text-white font-bold text-xs shadow-md transition-all active:scale-95 cursor-pointer shrink-0"
                >
                  <PlayCircle size={18} weight="fill" />
                  <span>Putar Video Sekarang</span>
                </button>
              </div>

              <div className="relative aspect-[16/9] rounded-2xl overflow-hidden bg-black border border-slate-800">
                {isYouTube && embedUrl ? (
                  <iframe
                    src={embedUrl}
                    title={article.videoTitle || article.title}
                    className="w-full h-full border-0"
                    allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
                    allowFullScreen
                  />
                ) : (
                  <video
                    src={article.videoUrl}
                    controls
                    playsInline
                    poster={article.image}
                    className="w-full h-full object-contain bg-black"
                  >
                    Browser Anda tidak mendukung pemutar video HTML5.
                  </video>
                )}
              </div>
            </div>
          )}
        </div>

        {/* Bottom Call to Action for Sharing */}
        <div className="my-8 p-5 rounded-2xl bg-emerald-50/70 border border-emerald-200/80 flex flex-col sm:flex-row sm:items-center justify-between gap-4">
          <div className="space-y-1">
            <h4 className="text-sm font-bold text-emerald-950 flex items-center gap-1.5">
              <WhatsappLogo size={18} weight="fill" className="text-emerald-700" />
              <span>Bagikan Informasi Ini ke Warga & Komunitas</span>
            </h4>
            <p className="text-xs text-emerald-800">
              Bantu sebarkan warta resmi kemajuan Desa Kadugenep melalui Status atau Grup WhatsApp.
            </p>
          </div>
          <button
            type="button"
            onClick={() => setIsShareModalOpen(true)}
            className="inline-flex items-center justify-center gap-2 px-4 py-2.5 rounded-xl bg-emerald-600 hover:bg-emerald-700 active:scale-95 text-white text-xs font-bold shadow-md shadow-emerald-600/20 transition-all cursor-pointer shrink-0"
          >
            <WhatsappLogo size={16} weight="fill" />
            <span>Bagikan ke WhatsApp</span>
          </button>
        </div>

        {/* Related News Section */}
        <section className="pt-8">
          <div className="flex items-center justify-between mb-6">
            <h2 className="text-xl font-bold text-slate-900 flex items-center gap-2">
              <Sparkle size={20} className="text-emerald-700" />
              <span>Warta Lainnya dari Kadugenep</span>
            </h2>
            <Link
              href="/#berita"
              className="text-xs font-bold text-emerald-800 hover:text-emerald-700 flex items-center gap-1"
            >
              <span>Lihat Semua</span>
              <ArrowRight size={14} />
            </Link>
          </div>

          <div className="grid grid-cols-1 sm:grid-cols-3 gap-4">
            {relatedArticles.map((rel) => (
              <Link
                key={rel.id}
                href={`/berita/${rel.slug}`}
                className="group p-4 rounded-2xl bg-white border border-slate-200 hover:border-emerald-700/40 hover:shadow-md transition-all space-y-2.5"
              >
                <div className="relative aspect-[16/10] rounded-xl overflow-hidden bg-slate-100">
                  <Image
                    src={rel.image || "/images/hero-kadugenep.jpg"}
                    alt={rel.title}
                    fill
                    sizes="300px"
                    className="object-cover group-hover:scale-105 transition-transform"
                    unoptimized={Boolean(rel.image?.startsWith("/uploads/") || rel.image?.startsWith("data:"))}
                  />
                  {rel.videoUrl && (
                    <span className="absolute bottom-2 right-2 p-1 rounded-md bg-black/70 backdrop-blur-sm text-rose-400">
                      <YoutubeLogo size={14} weight="fill" />
                    </span>
                  )}
                </div>
                <span className="text-[10px] font-bold text-emerald-800 uppercase tracking-wider">
                  {rel.category}
                </span>
                <h4 className="text-xs font-bold text-slate-900 group-hover:text-emerald-800 line-clamp-2 leading-snug">
                  {rel.title}
                </h4>
                <p className="text-[11px] text-slate-400">{formatDateID(rel.date)}</p>
              </Link>
            ))}
          </div>
        </section>
      </article>

      {/* WhatsApp Share Modal with Photo & Status Mode */}
      <ArticleShareModal
        isOpen={isShareModalOpen}
        onClose={() => setIsShareModalOpen(false)}
        article={article}
        shareUrl={shareUrl}
      />
    </>
  );
}
