"use client"

import * as React from "react"
import { Database, Lock, ShieldCheck, ChevronLeft, ChevronRight, Sparkles } from "lucide-react"

interface ResultItem {
  name: string
  ind: string
  base: string
  fc: string
  cagr: string
  an: string
  ver: string
  spark: string
  locked: boolean
  pick: boolean
}

interface Pagination {
  text: string
  pages: number[]
}

interface ResultsListData {
  title: string
  count: string
  subtitle: string
  sortLabel: string
  sortOptions: string[]
  items: ResultItem[]
  pagination: Pagination
}

function Sparkline({ points, locked }: { points: string; locked: boolean }) {
  return (
    <svg viewBox="0 0 90 24" style={{ width: 90, height: 26, flexShrink: 0 }} fill="none">
      <polyline
        points={points}
        fill="none"
        stroke={locked ? "#C8C3BA" : "#1A7340"}
        strokeWidth="2"
        strokeLinejoin="round"
        strokeLinecap="round"
      />
    </svg>
  )
}

export function IQResultsList({
  data,
  onUnlock,
}: {
  data: ResultsListData
  onUnlock?: () => void
}) {
  const [sortIdx, setSortIdx] = React.useState(0)

  return (
    <div>
      {/* Results header */}
      <div className="flex items-center justify-between gap-3.5 mb-3.5 flex-wrap">
        <div>
          <div className="text-[16px] font-semibold text-ink flex items-center gap-2.5">
            <Database className="size-[18px] text-navy" />
            {data.title}
            <span className="text-[13px] font-medium text-[#9A968E]">{data.count}</span>
          </div>
          <div className="text-[12px] text-[#9A968E] mt-0.5">{data.subtitle}</div>
        </div>
        <div className="flex items-center gap-2">
          <span className="text-[12px] text-[#9A968E]">{data.sortLabel}</span>
          <select
            value={sortIdx}
            onChange={(e) => setSortIdx(Number(e.target.value))}
            className="border border-[#C8C3BA] rounded-[6px] font-sans text-[12.5px] text-ink font-medium px-[11px] py-2 cursor-pointer bg-white"
          >
            {data.sortOptions.map((opt, i) => (
              <option key={i} value={i}>{opt}</option>
            ))}
          </select>
        </div>
      </div>

      {/* Cards — matching HTML reference */}
      <div id="results" className="flex flex-col gap-3">
        {data.items.map((item, i) => {
          const pickBadge = item.pick ? (
            <span className="inline-flex items-center gap-1 text-[9.5px] font-bold tracking-[.04em] uppercase text-ink bg-[#F5BC50] px-[7px] py-[2px] rounded-[3px]">
              <Sparkles className="size-2.5" />
              Used by IQ
            </span>
          ) : null

          const lockBadge = item.locked ? (
            <span className="inline-flex items-center gap-1 text-[11px] font-semibold text-[#9A968E]">
              <Lock className="size-3" />
              Preview
            </span>
          ) : (
            <span className="inline-flex items-center gap-1 text-[11px] font-semibold text-[#1A7340]">
              <ShieldCheck className="size-3" />
              Verified · {item.ver}
            </span>
          )

          return (
            <div
              key={i}
              onClick={item.locked ? onUnlock : undefined}
              className="bg-white border border-[#E2DDD6] rounded-[9px] p-[16px_18px] cursor-pointer flex items-center gap-[18px] hover:border-[#C8C3BA] hover:shadow-[0_6px_18px_rgba(10,13,20,.07)] transition-all duration-150"
            >
              {/* Left: text info */}
              <div className="flex-1 min-w-0">
                <div className="flex items-center gap-2.5 mb-[7px] flex-wrap">
                  <span className="text-[10px] font-bold tracking-[.05em] uppercase text-[#E8A020]">
                    {item.ind}
                  </span>
                  {pickBadge}
                </div>
                <div className="text-[15px] font-semibold text-ink leading-[1.35] mb-2.5">
                  {item.locked ? (
                    <span className="text-[#9A968E]">{item.name}</span>
                  ) : item.name}
                </div>
                <div className="flex items-center gap-3.5 flex-wrap">
                  <span className="inline-flex items-center gap-1.5 text-[11.5px] text-[#6B6760]">
                    <span className="w-[22px] h-[22px] rounded-full bg-navy text-[#F5BC50] inline-flex items-center justify-center font-display text-[9.5px] shrink-0">
                      {item.an}
                    </span>
                    Meticulous analyst
                  </span>
                  {lockBadge}
                </div>
              </div>

              {/* Sparkline */}
              <Sparkline points={item.spark} locked={item.locked} />

              {/* Right: forecast */}
              <div className="text-right min-w-[96px] shrink-0">
                <div className="text-[10.5px] text-[#9A968E] mb-0.5">2030</div>
                <div className="font-display text-[21px] leading-none" style={{ color: item.locked ? "#9A968E" : "#0D2B55" }}>
                  {item.locked ? "$ ●●" : item.fc}
                </div>
                <div className="text-[12px] font-bold mt-0.5" style={{ color: item.locked ? "#C8C3BA" : "#1A7340" }}>
                  {item.locked ? "•••" : item.cagr}
                </div>
              </div>

              {/* Chevron */}
              <ChevronRight className="size-[18px] text-[#C8C3BA] shrink-0" />
            </div>
          )
        })}
      </div>

      {/* Pagination */}
      <div className="flex items-center justify-between mt-[22px] flex-wrap gap-2.5">
        <span className="text-[12px] text-[#9A968E]">{data.pagination.text}</span>
        <div className="flex items-center gap-1">
          <button className="w-[34px] h-[34px] border border-[#E2DDD6] rounded-[6px] flex items-center justify-center text-[#C8C3BA] cursor-pointer bg-white hover:border-navy transition-colors">
            <ChevronLeft className="size-4" />
          </button>
          {data.pagination.pages.map((page, i) => (
            <button
              key={i}
              className={`w-[34px] h-[34px] rounded-[6px] flex items-center justify-center text-[13px] font-semibold cursor-pointer transition-colors ${
                page === 1
                  ? "bg-navy text-white border-none"
                  : "border border-[#E2DDD6] text-[#6B6760] bg-white hover:border-navy hover:text-navy"
              }`}
            >
              {page}
            </button>
          ))}
          <span className="text-[13px] text-[#9A968E] px-1">…</span>
          <button className="w-[34px] h-[34px] border border-[#E2DDD6] rounded-[6px] flex items-center justify-center text-[#6B6760] cursor-pointer bg-white hover:border-navy transition-colors">
            <ChevronRight className="size-4" />
          </button>
        </div>
      </div>
    </div>
  )
}
