"use client";

import React, { useState, useMemo, useEffect } from "react";
import Link from "next/link";
import { useSearchParams } from "next/navigation";
import Header from "@/components/Header";
import Footer from "@/components/Footer";
import AppImage from "@/components/ui/AppImage";
import type { Article } from "@/lib/articles/types";
import AISuggestedArticles from "@/components/AISuggestedArticles";
import SidebarAdStack from "@/components/SidebarAdStack";
import { cleanExcerpt } from "@/lib/articles/excerpt";

const CATEGORIES = ["All", "Cloud", "AI", "IP Workflows", "Live Production", "Streaming", "Audio", "Cameras", "NAB Show", "EAS", "Sports Broadcasting", "Ad Tech"];

// Map URL category slugs (?category=live-production) to a matching CATEGORIES label.
function categoryFromSlug(slug: string | null): string {
  if (!slug) return "All";
  const norm = slug.toLowerCase().replace(/[-_]+/g, " ").trim();
  const hit = CATEGORIES.find((c) => c.toLowerCase() === norm);
  return hit || "All";
}

const SORT_OPTIONS = [
  { value: "newest", label: "Newest First" },
  { value: "oldest", label: "Oldest First" },
  { value: "az", label: "A → Z" },
  { value: "za", label: "Z → A" },
];

function parseArticleDate(dateStr: string): Date {
  return new Date(dateStr);
}

interface NewsPageProps {
  articles: Article[];
  /** Eyebrow label rendered above the H1. Defaults to "News". */
  kicker?: string;
  /** Page H1. Defaults to "Industry News". */
  heading?: string;
  /** Subhead under the H1. */
  subtitle?: string;
  /** Hide the AI Suggested Articles sidebar widget (it surfaces general
      recommendations and shouldn't appear on curated views like Featured). */
  hideAiSuggestions?: boolean;
}

export default function NewsPage({
  articles,
  kicker = "News",
  heading = "Industry News",
  subtitle = "Breaking news and industry updates from the world of broadcast engineering",
  hideAiSuggestions = false,
}: NewsPageProps) {
  const allArticles = Array.isArray(articles) ? articles : [];
  const searchParams = useSearchParams();

  const [search, setSearch] = useState("");
  const [activeCategory, setActiveCategory] = useState("All");
  const [dateFrom, setDateFrom] = useState("");
  const [dateTo, setDateTo] = useState("");
  const [sortBy, setSortBy] = useState("newest");

  // Hydrate filter state from URL (?category=live-production&search=foo)
  useEffect(() => {
    const cat = categoryFromSlug(searchParams?.get("category") ?? null);
    if (cat !== "All") setActiveCategory(cat);
    const q = searchParams?.get("search") ?? searchParams?.get("q");
    if (q) setSearch(q);
  }, [searchParams]);

  const filtered = useMemo(() => {
    let result = [...allArticles];

    // Search filter
    if (search.trim()) {
      const q = search.toLowerCase();
      result = result.filter((a) => {
        const title = (a?.title ?? "").toLowerCase();
        const excerpt = (a?.excerpt ?? "").toLowerCase();
        const author = (a?.author ?? "").toLowerCase();
        const tags = Array.isArray(a?.tags) ? a.tags : [];
        return (
          title.includes(q) ||
          excerpt.includes(q) ||
          author.includes(q) ||
          tags.some((t) => (t ?? "").toLowerCase().includes(q))
        );
      });
    }

    // Category filter
    if (activeCategory !== "All") {
      const cat = activeCategory.toLowerCase();
      // The NAB Show chip should match any NAB-tagged article regardless of
      // year suffix in the legacy tag (e.g. "NAB 2026", "NAB", "NAB Show").
      const isNabChip = /\bnab\b/.test(cat);
      result = result.filter((a) => {
        const tags = Array.isArray(a?.tags) ? a.tags : [];
        const category = (a?.category ?? "").toLowerCase();
        if (isNabChip) {
          return (
            tags.some((t) => /\bnab\b/.test((t ?? "").toLowerCase())) ||
            /\bnab\b/.test(category)
          );
        }
        return (
          tags.some((t) => (t ?? "").toLowerCase() === cat) ||
          category.includes(cat)
        );
      });
    }

    // Date range filter
    if (dateFrom) {
      const from = new Date(dateFrom);
      result = result.filter((a) => parseArticleDate(a?.date ?? "") >= from);
    }
    if (dateTo) {
      const to = new Date(dateTo);
      to.setHours(23, 59, 59, 999);
      result = result.filter((a) => parseArticleDate(a?.date ?? "") <= to);
    }

    // Sorting
    result.sort((a, b) => {
      const ta = (a?.title ?? "");
      const tb = (b?.title ?? "");
      if (sortBy === "newest") return parseArticleDate(b?.date ?? "").getTime() - parseArticleDate(a?.date ?? "").getTime();
      if (sortBy === "oldest") return parseArticleDate(a?.date ?? "").getTime() - parseArticleDate(b?.date ?? "").getTime();
      if (sortBy === "az") return ta.localeCompare(tb);
      if (sortBy === "za") return tb.localeCompare(ta);
      return 0;
    });

    return result;
  }, [allArticles, search, activeCategory, dateFrom, dateTo, sortBy]);

  const hasActiveFilters = search || activeCategory !== "All" || dateFrom || dateTo || sortBy !== "newest";

  function clearFilters() {
    setSearch("");
    setActiveCategory("All");
    setDateFrom("");
    setDateTo("");
    setSortBy("newest");
  }

  return (
    <div className="min-h-screen bg-background">
      <Header />
      {/* DO NOT OVERRIDE — News page hero header */}
      <div className="bg-[#111] border-b border-[#222] py-8">
        <div className="max-w-container mx-auto px-4">
          <div className="flex items-center gap-3 mb-2">
            <span className="section-label">{kicker}</span>
            <div className="flex-1 h-px bg-[#DCE6F2]" />
          </div>
          <h1 className="font-heading text-white text-3xl font-bold">{heading}</h1>
          <p className="text-[#777] font-body text-sm mt-2">{subtitle}</p>
        </div>
      </div>

      {/* Search & Filter Bar */}
      <div className="bg-[#F8FAFC] border-b border-[#1e1e1e] py-4">
        <div className="max-w-container mx-auto px-4 space-y-3">
          {/* Row 1: Search + Sort */}
          <div className="flex flex-col sm:flex-row sm:items-center gap-3">
            {/* Search */}
            <div className="relative flex-1">
              <svg className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-[#555]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M21 21l-4.35-4.35M17 11A6 6 0 1 1 5 11a6 6 0 0 1 12 0z" />
              </svg>
              <input
                type="text"
                placeholder="Search articles, topics, authors…"
                value={search}
                onChange={(e) => setSearch(e.target.value)}
                className="w-full bg-[#FFFFFF] border border-[#DCE6F2] text-[#e0e0e0] placeholder-[#555] text-sm font-body pl-9 pr-4 py-2.5 rounded-sm focus:outline-none focus:border-[#1D4ED8] transition-colors"
              />
              {search && (
                <button
                  onClick={() => setSearch("")}
                  className="absolute right-3 top-1/2 -translate-y-1/2 text-[#555] hover:text-[#e0e0e0] transition-colors"
                  aria-label="Clear search"
                >
                  <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
                  </svg>
                </button>
              )}
            </div>

            {/* Date Range */}
            <div className="flex items-center gap-2">
              <input
                type="date"
                value={dateFrom}
                onChange={(e) => setDateFrom(e.target.value)}
                className="bg-[#FFFFFF] border border-[#DCE6F2] text-[#888] text-xs font-body px-3 py-2.5 rounded-sm focus:outline-none focus:border-[#1D4ED8] transition-colors [color-scheme:dark]"
                title="From date"
              />
              <span className="text-[#444] text-xs">–</span>
              <input
                type="date"
                value={dateTo}
                onChange={(e) => setDateTo(e.target.value)}
                className="bg-[#FFFFFF] border border-[#DCE6F2] text-[#888] text-xs font-body px-3 py-2.5 rounded-sm focus:outline-none focus:border-[#1D4ED8] transition-colors [color-scheme:dark]"
                title="To date"
              />
            </div>

            {/* Sort */}
            <div className="relative">
              <select
                value={sortBy}
                onChange={(e) => setSortBy(e.target.value)}
                className="appearance-none bg-[#FFFFFF] border border-[#DCE6F2] text-[#888] text-xs font-body pl-3 pr-8 py-2.5 rounded-sm focus:outline-none focus:border-[#1D4ED8] transition-colors cursor-pointer"
              >
                {SORT_OPTIONS.map((opt) => (
                  <option key={opt.value} value={opt.value}>{opt.label}</option>
                ))}
              </select>
              <svg className="absolute right-2.5 top-1/2 -translate-y-1/2 w-3 h-3 text-[#555] pointer-events-none" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 9l-7 7-7-7" />
              </svg>
            </div>
          </div>

          {/* Topics chip row hidden per current design direction (2026-06-03). */}
        </div>
      </div>

      <main className="max-w-container mx-auto px-4 py-8">
        <div className="grid grid-cols-1 lg:grid-cols-12 gap-8">
          {/* Article list */}
          <div className="lg:col-span-8">
            {/* Results count */}
            <div className="flex items-center justify-between mb-4">
              <p className="text-[#555] font-body text-xs">
                {filtered.length === allArticles.length
                  ? `${allArticles.length} articles`
                  : `${filtered.length} of ${allArticles.length} articles`}
              </p>
            </div>

            {filtered.length === 0 ? (
              <div className="py-16 text-center">
                <svg className="w-10 h-10 text-[#333] mx-auto mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M9.172 16.172a4 4 0 015.656 0M9 10h.01M15 10h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
                </svg>
                <p className="text-[#555] font-body text-sm">No articles match your filters.</p>
                <button onClick={clearFilters} className="mt-3 text-[#1D4ED8] text-xs font-body hover:underline">Clear filters</button>
              </div>
            ) : (
              <div className="space-y-0">
                {filtered.map((article) => {
                  if (!article?.slug) return null;
                  return (
                    <Link
                      key={article.slug}
                      href={`/news/${article.slug}`}
                      className="flex gap-4 py-5 border-b border-[#DCE6F2] group hover:bg-[#EFF6FF] transition-colors px-2 -mx-2 rounded-sm">
                      <div className="flex-shrink-0 w-[160px] h-[105px] relative overflow-hidden rounded-sm">
                        <AppImage
                          src={article.image || "/assets/images/article-placeholder.jpg"}
                          alt={article.alt || article.title || "AV Beat article"}
                          fill
                          className="object-contain group-hover:scale-105 transition-transform duration-300"
                          sizes="160px"
                        />
                      </div>
                      <div className="flex-1 min-w-0">
                        <div className="flex items-center gap-2 mb-1.5">
                          <span className="text-[#1D4ED8] font-body text-[10px] font-bold uppercase tracking-wider">{article.category || "NEWS"}</span>
                        </div>
                        <h2 className="font-heading text-[#0F172A] text-base font-bold leading-snug mb-2 group-hover:text-[#1D4ED8] transition-colors line-clamp-2">
                          {article.title || "Untitled"}
                        </h2>
                        <p className="text-[#475569] font-body text-sm leading-relaxed line-clamp-2">{cleanExcerpt(article.excerpt || "")}</p>
                        <div className="flex items-center gap-2 mt-2">
                          <span className="text-[#555] font-body text-xs">By {article.author || "AV Beat"}</span>
                          <span className="text-[#444] text-[10px]">·</span>
                          <span className="text-[#555] font-body text-xs">{article.readTime || ""}</span>
                        </div>
                      </div>
                    </Link>
                  );
                })}
              </div>
            )}
          </div>

          {/* Sidebar */}
          <aside className="lg:col-span-4 space-y-6">
            {/* Banner stack: 300x600 + every 300x250 in rotation —
                grows as the user scrolls the article list */}
            <SidebarAdStack />

            {/* AI Suggested Articles */}
            {!hideAiSuggestions && <AISuggestedArticles variant="full" />}

            <div className="bg-[#111] border border-[#222] p-4">
              <h3 className="font-body font-bold text-xs text-[#1D4ED8] uppercase tracking-widest mb-3 pb-2 border-b border-[#222]">
                Popular Tags
              </h3>
              <div className="flex flex-wrap gap-2">
                {["IP Workflows", "Cloud", "AI", "Live Production", "Streaming", "NAB Show", "Audio", "Cameras"].map((tag) => (
                  <button
                    key={tag}
                    onClick={() => setActiveCategory(tag)}
                    className={`px-2 py-1 border text-xs font-body rounded-sm transition-colors ${
                      activeCategory === tag
                        ? "bg-[#1D4ED8] border-[#1D4ED8] text-white"
                        : "bg-[#FFFFFF] border-[#DCE6F2] text-[#888] hover:border-[#1D4ED8] hover:text-[#1D4ED8]"
                    }`}
                  >
                    {tag}
                  </button>
                ))}
              </div>
            </div>
            <div className="bg-[#0d1520] border border-[#DCE6F2] p-4">
              <h3 className="font-body font-bold text-xs text-[#1D4ED8] uppercase tracking-widest mb-2">Newsletter</h3>
              <p className="text-[#777] text-xs font-body mb-3">Get pro AV news delivered to your inbox every week.</p>
              <Link href="/home-page#newsletter" className="btn-subscribe text-xs py-2 px-4 inline-block">Subscribe Free</Link>
            </div>
          </aside>
        </div>
      </main>
      <Footer />
    </div>
  );
}
