"use client";
import { useState } from "react";
import type { PostSummary } from "@/lib/blog/types";
import { PostCard } from "./post-card";
import "./blog-list.css";
export function BlogList({ posts }: { posts: PostSummary[] }) {
  const [query, setQuery] = useState("");
  const [year, setYear] = useState("all");
  const [page, setPage] = useState(1);
  const pageSize = 12;
  const years = [...new Set(posts.map((post) => post.publishedAt.slice(0, 4)))]
    .sort()
    .reverse();
  const term = query.trim().toLocaleLowerCase();
  const results = posts.filter(
    (post) =>
      `${post.title} ${post.excerpt} ${post.author}`
        .toLocaleLowerCase()
        .includes(term) &&
      (year === "all" || post.publishedAt.startsWith(year)),
  );
  const pages = Math.max(1, Math.ceil(results.length / pageSize));
  const current = Math.min(page, pages);
  return (
    <>
      <div className="blog-tools">
        <p id="post-count" role="status">
          {results.length} {results.length === 1 ? "article" : "articles"} from
          the movement
        </p>
        <div className="blog-filter-controls">
          <label className="visually-hidden" htmlFor="post-year">
            Filter articles by year
          </label>
          <select
            id="post-year"
            value={year}
            onChange={(event) => {
              setYear(event.target.value);
              setPage(1);
            }}
          >
            <option value="all">All years</option>
            {years.map((value) => (
              <option key={value} value={value}>
                {value}
              </option>
            ))}
          </select>
          <input
            className="search"
            type="search"
            id="post-search"
            aria-label="Search articles"
            placeholder="Search articles…"
            value={query}
            onChange={(event) => {
              setQuery(event.target.value);
              setPage(1);
            }}
          />
        </div>
      </div>
      <div className="posts blog-posts">
        {results
          .slice((current - 1) * pageSize, current * pageSize)
          .map((post) => (
            <PostCard key={post.slug} post={post} />
          ))}
      </div>
      <p id="no-results" className="empty-state" hidden={results.length > 0}>
        No articles match your search. Try another word or clear the search.
      </p>
      {results.length > pageSize && (
        <nav className="blog-pagination" aria-label="Article pages">
          <button
            type="button"
            disabled={current === 1}
            onClick={() => setPage(current - 1)}
          >
            Previous
          </button>
          <span role="status">
            Page {current} of {pages}
          </span>
          <button
            type="button"
            disabled={current === pages}
            onClick={() => setPage(current + 1)}
          >
            Next
          </button>
        </nav>
      )}
    </>
  );
}
