"use client";

import { useState } from "react";
import { Search, Filter } from "lucide-react";
import { useBills } from "@/lib/hooks/useBills";
import { BillCard } from "@/components/shared/BillCard";

const CHAMBERS = ["", "House", "Senate"];
const TOPICS = [
  "", "healthcare", "taxation", "defense", "education", "immigration",
  "environment", "housing", "infrastructure", "technology", "agriculture",
  "judiciary", "foreign-policy", "veterans", "social-security", "trade",
  "budget", "energy", "banking", "transportation", "labor",
];

export default function BillsPage() {
  const [q, setQ] = useState("");
  const [chamber, setChamber] = useState("");
  const [topic, setTopic] = useState("");
  const [page, setPage] = useState(1);

  const params = {
    ...(q && { q }),
    ...(chamber && { chamber }),
    ...(topic && { topic }),
    page,
    per_page: 20,
    sort: "latest_action_date",
  };

  const { data, isLoading } = useBills(params);

  return (
    <div className="space-y-6">
      <div>
        <h1 className="text-2xl font-bold">Bills</h1>
        <p className="text-muted-foreground text-sm mt-1">Browse and search US Congressional legislation</p>
      </div>

      {/* Filters */}
      <div className="flex gap-3 flex-wrap">
        <div className="relative flex-1 min-w-48">
          <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground" />
          <input
            type="text"
            placeholder="Search bills..."
            value={q}
            onChange={(e) => { setQ(e.target.value.trim()); setPage(1); }}
            className="w-full pl-9 pr-3 py-2 text-sm bg-card border border-border rounded-md focus:outline-none focus:ring-1 focus:ring-primary"
          />
        </div>
        <select
          value={chamber}
          onChange={(e) => { setChamber(e.target.value); setPage(1); }}
          className="px-3 py-2 text-sm bg-card border border-border rounded-md focus:outline-none"
        >
          <option value="">All Chambers</option>
          {CHAMBERS.slice(1).map((c) => <option key={c} value={c}>{c}</option>)}
        </select>
        <select
          value={topic}
          onChange={(e) => { setTopic(e.target.value); setPage(1); }}
          className="px-3 py-2 text-sm bg-card border border-border rounded-md focus:outline-none"
        >
          <option value="">All Topics</option>
          {TOPICS.slice(1).map((t) => <option key={t} value={t}>{t}</option>)}
        </select>
      </div>

      {/* Results */}
      {isLoading ? (
        <div className="text-center py-20 text-muted-foreground">Loading bills...</div>
      ) : (
        <>
          <div className="flex items-center justify-between text-sm text-muted-foreground">
            <span>{data?.total ?? 0} bills found</span>
            <span>Page {data?.page} of {data?.pages}</span>
          </div>

          <div className="space-y-3">
            {data?.items?.map((bill) => (
              <BillCard key={bill.bill_id} bill={bill} />
            ))}
          </div>

          {/* Pagination */}
          {data && data.pages > 1 && (
            <div className="flex justify-center gap-2">
              <button
                onClick={() => setPage((p) => Math.max(1, p - 1))}
                disabled={page === 1}
                className="px-4 py-2 text-sm bg-card border border-border rounded-md disabled:opacity-40 hover:bg-accent transition-colors"
              >
                Previous
              </button>
              <button
                onClick={() => setPage((p) => Math.min(data.pages, p + 1))}
                disabled={page === data.pages}
                className="px-4 py-2 text-sm bg-card border border-border rounded-md disabled:opacity-40 hover:bg-accent transition-colors"
              >
                Next
              </button>
            </div>
          )}
        </>
      )}
    </div>
  );
}
