import { useState } from "react"
import { ExternalLink } from "lucide-react"

import { Section } from "@/components/layout/Section"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog"
import {
  IndicatorCard,
  typeMeta,
} from "@/components/sections/IndicatorCard"
import { indicators, type Indicator, type IndicatorType } from "@/data/project"

const groups: { type: IndicatorType; heading: string }[] = [
  { type: "journal", heading: "Journals" },
  { type: "conference", heading: "Conferences" },
  { type: "msc", heading: "MSc Dissertations" },
]

export function Indicators() {
  const [open, setOpen] = useState<Indicator | null>(null)

  return (
    <Section
      id="indicators"
      eyebrow="Indicators"
      title="Impact & dissemination"
      description="Journals, conference papers and dissertations produced within the project. Select any card for full details."
      className="bg-background"
    >
      <div className="space-y-12">
        {groups.map((g) => {
          const items = indicators.filter((i) => i.type === g.type)
          if (items.length === 0) return null
          return (
            <div key={g.type}>
              <div className="mb-5 flex items-center gap-3">
                <h3 className="text-lg font-semibold text-foreground">
                  {g.heading}
                </h3>
                <Badge variant="secondary">{items.length}</Badge>
              </div>
              <div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
                {items.map((indicator, i) => (
                  <IndicatorCard
                    key={indicator.id}
                    indicator={indicator}
                    index={i}
                    onOpen={() => setOpen(indicator)}
                  />
                ))}
              </div>
            </div>
          )
        })}
      </div>

      <Dialog open={open !== null} onOpenChange={(o) => !o && setOpen(null)}>
        <DialogContent>
          {open && (
            <>
              <DialogHeader>
                <div className="mb-1">
                  <Badge variant="brand">{typeMeta[open.type].label}</Badge>
                </div>
                <DialogTitle>{open.title}</DialogTitle>
                {open.authors && (
                  <DialogDescription>{open.authors}</DialogDescription>
                )}
              </DialogHeader>
              <dl className="space-y-2 text-sm">
                {open.venue && (
                  <div className="flex gap-2">
                    <dt className="w-20 shrink-0 text-muted-foreground">Venue</dt>
                    <dd className="text-foreground">{open.venue}</dd>
                  </div>
                )}
                {open.year && (
                  <div className="flex gap-2">
                    <dt className="w-20 shrink-0 text-muted-foreground">Year</dt>
                    <dd className="text-foreground">{open.year}</dd>
                  </div>
                )}
              </dl>
              {open.url && (
                <Button variant="brand" asChild className="mt-2 w-fit">
                  <a href={open.url} target="_blank" rel="noopener noreferrer">
                    Open publication
                    <ExternalLink className="h-4 w-4" />
                  </a>
                </Button>
              )}
            </>
          )}
        </DialogContent>
      </Dialog>
    </Section>
  )
}
