"use client"

import { useState, useEffect } 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 { getDashboardStats } from "@/data/dashboard"
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)

  useEffect(() => {
    setStats(getDashboardStats(period))
  }, [period])

  const statCards = stats
    ? [
        { title: "Total Leads", value: stats.totalLeads, icon: Users, variant: "blue" as const, description: stats.periodLabel },
        { title: "Open Leads", value: stats.openLeads, icon: UserPlus, variant: "blue" as const, description: "Needs attention" },
        { title: "Contacted", value: stats.contactedLeads, icon: PhoneCall, variant: "amber" as const, description: "In conversation" },
        { title: "Pending", value: stats.pendingLeads, icon: Clock, variant: "purple" as const, description: "Awaiting decision" },
        { title: "Closed", value: stats.closedLeads, icon: CheckCircle2, variant: "emerald" as const, description: "Won" },
        { title: "Conversion Rate", value: `${stats.conversionRate}%`, icon: TrendingUp, variant: "emerald" as const, description: `${stats.closedLeads} of ${stats.totalLeads} leads` },
      ]
    : []

  return (
    <div className="space-y-6">
      <PageHeader title="Dashboard" 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>

      <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} />
            ))
          : Array.from({ length: 6 }).map((_, i) => <StatCardSkeleton key={i} />)
        }
      </div>

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

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