"use client"

import {
  Server,
  Ticket,
  Activity,
  CreditCard,
  Plus,
  ArrowUpRight,
  TrendingUp,
} from "lucide-react"
import { useLanguage } from "@/hooks/use-language"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import { mockStats, mockTickets, mockServices, mockClient } from "@/lib/mock-data"
import Link from "next/link"

export default function DashboardPage() {
  const { t } = useLanguage()

  const stats = [
    {
      title: t("activeServices"),
      value: mockStats.activeServices,
      icon: Server,
      color: "text-blue-600 dark:text-blue-400",
      bg: "bg-blue-50 dark:bg-blue-950/40",
    },
    {
      title: t("openTickets"),
      value: mockStats.openTickets,
      icon: Ticket,
      color: "text-amber-600 dark:text-amber-400",
      bg: "bg-amber-50 dark:bg-amber-950/40",
    },
    {
      title: t("currentUsage"),
      value: `${mockStats.currentUsageGbps} Gbps`,
      icon: Activity,
      color: "text-emerald-600 dark:text-emerald-400",
      bg: "bg-emerald-50 dark:bg-emerald-950/40",
    },
    {
      title: t("monthlyBill"),
      value: `৳ ${mockStats.monthlyBill.toLocaleString()}`,
      icon: CreditCard,
      color: "text-violet-600 dark:text-violet-400",
      bg: "bg-violet-50 dark:bg-violet-950/40",
    },
  ]

  return (
    <div className="space-y-6">
      {/* Stats Grid */}
      <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
        {stats.map((stat) => (
          <Card key={stat.title} className="overflow-hidden">
            <CardContent className="p-6">
              <div className="flex items-center justify-between">
                <div>
                  <p className="text-sm font-medium text-slate-500 dark:text-slate-400">
                    {stat.title}
                  </p>
                  <p className="mt-2 text-2xl font-bold tracking-tight">{stat.value}</p>
                </div>
                <div className={`rounded-xl p-3 ${stat.bg}`}>
                  <stat.icon className={`h-5 w-5 ${stat.color}`} />
                </div>
              </div>
            </CardContent>
          </Card>
        ))}
      </div>

      <div className="grid gap-6 lg:grid-cols-3">
        {/* Recent Tickets */}
        <Card className="lg:col-span-2">
          <CardHeader className="flex flex-row items-center justify-between">
            <CardTitle className="text-base">{t("recentActivity")}</CardTitle>
            <Link href="/tickets">
              <Button variant="ghost" size="sm" className="gap-1 text-xs">
                View all <ArrowUpRight className="h-3.5 w-3.5" />
              </Button>
            </Link>
          </CardHeader>
          <CardContent>
            <div className="space-y-3">
              {mockTickets.slice(0, 3).map((ticket) => (
                <div
                  key={ticket.id}
                  className="flex items-center justify-between rounded-lg border border-slate-100 p-3 transition-colors hover:bg-slate-50 dark:border-slate-800 dark:hover:bg-slate-900"
                >
                  <div className="min-w-0 flex-1">
                    <div className="flex items-center gap-2">
                      <span className="text-xs font-mono text-slate-400">{ticket.id}</span>
                      <Badge
                        variant={
                          ticket.priority === "high" || ticket.priority === "critical"
                            ? "danger"
                            : ticket.priority === "medium"
                            ? "warning"
                            : "secondary"
                        }
                      >
                        {ticket.priority}
                      </Badge>
                    </div>
                    <p className="mt-1 truncate text-sm font-medium">{ticket.subject}</p>
                  </div>
                  <Badge
                    variant={
                      ticket.status === "resolved" || ticket.status === "closed"
                        ? "success"
                        : ticket.status === "in_progress"
                        ? "default"
                        : "warning"
                    }
                  >
                    {ticket.status.replace("_", " ")}
                  </Badge>
                </div>
              ))}
            </div>
          </CardContent>
        </Card>

        {/* Quick Actions */}
        <Card>
          <CardHeader>
            <CardTitle className="text-base">{t("quickActions")}</CardTitle>
          </CardHeader>
          <CardContent className="space-y-3">
            <Link href="/tickets/new">
              <Button className="w-full justify-start gap-2" variant="outline">
                <Plus className="h-4 w-4" />
                {t("createTicket")}
              </Button>
            </Link>
            <Link href="/services">
              <Button className="w-full justify-start gap-2" variant="outline">
                <TrendingUp className="h-4 w-4" />
                {t("requestUpgrade")}
              </Button>
            </Link>
            <Link href="/billing">
              <Button className="w-full justify-start gap-2" variant="outline">
                <CreditCard className="h-4 w-4" />
                {t("viewInvoices")}
              </Button>
            </Link>
          </CardContent>
        </Card>
      </div>

      {/* Active Services Preview */}
      <Card>
        <CardHeader className="flex flex-row items-center justify-between">
          <CardTitle className="text-base">{t("myServices")}</CardTitle>
          <Link href="/services">
            <Button variant="ghost" size="sm" className="gap-1 text-xs">
              View all <ArrowUpRight className="h-3.5 w-3.5" />
            </Button>
          </Link>
        </CardHeader>
        <CardContent>
          <div className="overflow-x-auto">
            <table className="w-full text-sm">
              <thead>
                <tr className="border-b border-slate-200 text-left text-xs font-medium uppercase tracking-wider text-slate-500 dark:border-slate-800">
                  <th className="pb-3 pr-4">{t("serviceType")}</th>
                  <th className="pb-3 pr-4">{t("capacity")}</th>
                  <th className="pb-3 pr-4">{t("location")}</th>
                  <th className="pb-3">{t("status")}</th>
                </tr>
              </thead>
              <tbody className="divide-y divide-slate-100 dark:divide-slate-800">
                {mockServices.map((svc) => (
                  <tr key={svc.id} className="hover:bg-slate-50 dark:hover:bg-slate-900/50">
                    <td className="py-3 pr-4 font-medium">{svc.type}</td>
                    <td className="py-3 pr-4">{svc.capacity}</td>
                    <td className="py-3 pr-4 text-slate-500">{svc.location}</td>
                    <td className="py-3">
                      <Badge variant="success">{svc.status}</Badge>
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        </CardContent>
      </Card>
    </div>
  )
}
