"use client"

import { useState, useEffect, useRef } from "react"
import { PageHeader } from "@/components/shared/page-header"
import { StatCard } from "@/components/dashboard/stat-card"
import { StatCardSkeleton } from "@/components/dashboard/stat-card-skeleton"
import { RecentLeadsTable } from "@/components/dashboard/recent-leads-table"
import { LeadStatusChart } from "@/components/dashboard/lead-status-chart"
import { LeadsPerMonthChart } from "@/components/dashboard/leads-per-month-chart"
import {
  Users,
  UserPlus,
  PhoneCall,
  Clock,
  CheckCircle2,
  TrendingUp,
  ListFilter,
} from "lucide-react"
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select"
import { DashboardStats } from "@/types"

export default function DashboardPage() {
  const [period, setPeriod] = useState("6months")
  const [stats, setStats] = useState<DashboardStats | null>(null)
  const pollingRef = useRef<NodeJS.Timeout | null>(null)

  async function fetchStats(p: string) {
    try {
      const res = await fetch(`/api/dashboard?period=${p}`)
      if (res.ok) {
        const data = await res.json()
        setStats(data)
      }
    } catch {
      console.warn("Failed to fetch dashboard stats")
    }
  }

  useEffect(() => {
    fetchStats(period)
    pollingRef.current = setInterval(() => fetchStats(period), 30000)
    return () => {
      if (pollingRef.current) clearInterval(pollingRef.current)
    }
  }, [period])

  const statCards = stats
    ? [
        { title: "Total Leads", value: stats.totalLeads, icon: Users, description: stats.periodLabel, trend: stats.trends.totalLeads, sparklineField: "total" as const },
        { title: "Open Leads", value: stats.openLeads, icon: UserPlus, description: "Needs attention", trend: stats.trends.openLeads, sparklineField: "open" as const },
        { title: "Contacted", value: stats.contactedLeads, icon: PhoneCall, description: "In conversation", trend: stats.trends.contactedLeads, sparklineField: "contacted" as const },
        { title: "Pending", value: stats.pendingLeads, icon: Clock, description: "Awaiting decision", trend: stats.trends.pendingLeads, sparklineField: "pending" as const },
        { title: "Closed", value: stats.closedLeads, icon: CheckCircle2, description: "Won", trend: stats.trends.closedLeads, sparklineField: "closed" as const },
        { title: "Conversion Rate", value: `${stats.conversionRate}%`, icon: TrendingUp, description: `${stats.closedLeads} of ${stats.totalLeads} leads`, trend: stats.trends.conversionRate, sparklineField: "closed" as const, conversionRate: stats.conversionRate },
      ]
    : []

  return (
    <div className="space-y-6 relative">
      {/* Daily Bugle watermark */}
      <div className="absolute top-12 right-4 pointer-events-none select-none z-0 opacity-[0.04] dark:opacity-[0.06]">
        <span className="text-[96px] font-['Bangers',cursive] leading-none text-[#e62020] dark:text-[#FF6666] tracking-wider">
          DAILY BUGLE
        </span>
      </div>

      <PageHeader title={<span style={{fontFamily:"'Bangers',cursive"}}>Dashboard</span>} description="Overview of your sales pipeline">
        <Select value={period} onValueChange={setPeriod}>
          <SelectTrigger className="h-9 w-[160px]">
            <ListFilter className="mr-2 h-4 w-4" />
            <SelectValue />
          </SelectTrigger>
          <SelectContent>
            <SelectItem value="7days">Last 7 days</SelectItem>
            <SelectItem value="30days">Last 30 days</SelectItem>
            <SelectItem value="6months">Last 6 months</SelectItem>
            <SelectItem value="12months">Last 12 months</SelectItem>
          </SelectContent>
        </Select>
      </PageHeader>

      <p className="text-xs font-semibold tracking-widest uppercase text-[#888888] dark:text-[#666666]">Pipeline Overview</p>

      <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-6">
        {stats
          ? statCards.map((card, i) => (
              <StatCard key={card.title} {...card} index={i} monthlyBreakdown={stats.monthlyBreakdown} />
            ))
          : Array.from({ length: 6 }).map((_, i) => <StatCardSkeleton key={i} />)
        }
      </div>

      <p className="text-xs font-semibold tracking-widest uppercase text-[#888888] dark:text-[#666666]">Analytics</p>

      <div className="grid gap-6 lg:grid-cols-2">
        <LeadStatusChart data={stats?.statusDistribution ?? []} />
        <LeadsPerMonthChart data={stats?.leadsPerMonth ?? []} />
      </div>

      <RecentLeadsTable leads={stats?.recentLeads ?? []} />
    </div>
  )
}
