/**
 * The designed article list of the static page (placeholder tabs, cards and pagination).
 * Shown until the API has a published article, or when it can't be reached.
 */
import type { ReactNode } from "react";
import type { ArticleListingContent } from "@/app/(site)/_lib/pageContent";
import { spaced } from "../parts";

const CATEGORIES = ["AI & Supply Chain", "Sustainability & Ethical Sourcing", "Market Trends & Global Trade Insights", "AI-Powered Agriculture"];
const ARTICLE = {
  image: { url: "/assets/images/thought-leadership/article-thumb.png", alt: "Automated fruit processing line with conveyor belts" },
  category: "The Future of Work in Agri-Tech & Supply Chains",
  title: "AI & Automation in Global Food Logistics",
  description: "Figma ipsum component variant main layer. Community vertical pixel move share star editor con..",
  href: "#",
};
const ARTICLES = Array.from({ length: 9 }, () => ARTICLE);

export const Chevron = ({ d }: { d: string[] }) => (
  <svg className="w-4 h-4" viewBox="0 0 16 16" fill="none" aria-hidden="true">
    {d.map((p, i) => (
      <path key={i} d={p} stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"></path>
    ))}
  </svg>
);

/** The tabs band: scroll buttons around the pills, then the divider. */
export function FilterBar({ children }: { children: ReactNode }) {
  return (
    <section className="articles-filter-section" aria-label="Article categories">
      <div className="w-full max-w-[1180px] mx-auto px-6">
        <div className="articles-filter-bar">
          <button type="button" className="articles-filter-nav" aria-label="Scroll categories left">
            {" "}
            <Chevron d={["M10 3L5 8l5 5"]} />{" "}
          </button>{" "}
          <div className="articles-filter-track" id="articles-filter-track" role="tablist" aria-label="Filter articles by category">
            {children}
          </div>{" "}
          <button type="button" className="articles-filter-nav" aria-label="Scroll categories right">
            {" "}
            <Chevron d={["M6 3l5 5-5 5"]} />{" "}
          </button>
        </div>{" "}
        <hr className="articles-divider" aria-hidden="true" />
      </div>
    </section>
  );
}

export function StaticArticles({ c }: { c: ArticleListingContent }) {
  return (
    <>
      <FilterBar>
        {spaced([
          <button key="all" type="button" className="articles-filter-pill is-active" role="tab" aria-selected="true">
            {c.allFilterLabel}
          </button>,
          ...CATEGORIES.map((cat) => (
            <button key={cat} type="button" className="articles-filter-pill" role="tab" aria-selected="false">
              {cat}
            </button>
          )),
        ])}
      </FilterBar>
      <section className="articles-section" aria-labelledby="articles-heading">
        <div className="w-full max-w-[1180px] mx-auto px-6">
          <h2 id="articles-heading" className="sr-only">
            {c.heading}
          </h2>
          <div className="articles-grid">
            {ARTICLES.map((a, i) => (
              <article key={i} className="article-card">
                <a href={a.href} className="article-card-image">
                  {" "}
                  <img src={a.image.url} alt={a.image.alt} width="360" height="220" loading="lazy" />{" "}
                </a>{" "}
                <a href={a.href} className="article-card-category">
                  {a.category}
                </a>{" "}
                <h3 className="article-card-title">
                  <a href={a.href}>{a.title}</a>
                </h3>
                <p className="article-card-desc">{a.description}</p>
              </article>
            ))}
          </div>
          <nav className="pagination mt-12 mb-4 flex items-center justify-center gap-2 flex-wrap" aria-label="Article pages" data-reveal="">
            <button type="button" className="pagination-btn pagination-arrow" aria-label="Previous page" disabled>
              {" "}
              <Chevron d={["M10 3L5 8l5 5", "M12 3L7 8l5 5"]} />{" "}
            </button>{" "}
            <button type="button" className="pagination-btn is-active" aria-current="page">
              1
            </button>{" "}
            <button type="button" className="pagination-btn">
              2
            </button>{" "}
            <button type="button" className="pagination-btn">
              3
            </button>{" "}
            <span className="pagination-ellipsis text-[#2E2E2E] px-1 select-none" aria-hidden="true">
              …
            </span>{" "}
            <button type="button" className="pagination-btn">
              10
            </button>{" "}
            <button type="button" className="pagination-btn pagination-arrow" aria-label="Next page">
              {" "}
              <Chevron d={["M6 3l5 5-5 5", "M4 3l5 5-5 5"]} />{" "}
            </button>
          </nav>
        </div>
      </section>
    </>
  );
}
