"use client";

import { useRouter } from "next/navigation";
import { Card } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { Skeleton } from "@/components/ui/skeleton";
import {
  ArrowLeft,
  Users,
  Pencil,
  User,
  Mail,
  Building2,
  FileText,
  CreditCard,
  Phone,
  Activity,
  CalendarDays,
  Hash,
} from "lucide-react";
import { useGetClient } from "../hooks/client.hooks";
import { getStatusVariant } from "@/lib/status-variant";
import { formatDateTime } from "@/lib/format-date";

const statusColor: Record<string, string> = {
  ACTIVE: "text-emerald-600 border-emerald-600/30 bg-emerald-500/10",
  INVITED: "text-amber-600 border-amber-600/30 bg-amber-500/10",
  INACTIVE: "text-muted-foreground border-border bg-muted",
};

interface Props {
  id: string;
}

export function ViewClientPage({ id }: Props) {
  const router = useRouter();
  const { data: client, isLoading } = useGetClient(id);

  const initials = client
    ? (client.name ?? "")
        .split(" ")
        .map((n) => n[0])
        .join("")
        .slice(0, 2)
        .toUpperCase()
    : "";

  return (
    <div className="space-y-4">
      <button
        onClick={() => router.back()}
        className="text-muted-foreground hover:text-foreground flex items-center gap-1 text-sm"
      >
        <ArrowLeft className="h-4 w-4" /> Back
      </button>
      <div>
        <h2 className="text-2xl font-bold tracking-tight">Client Details</h2>
        <p className="text-muted-foreground text-sm mt-0.5">
          View client information
        </p>
      </div>

      {isLoading ? (
        <div className="flex flex-col md:flex-row gap-5 items-start">
          <div className="flex flex-col gap-4 w-full md:w-auto">
            <Card className="p-6">
              <Skeleton className="h-40 w-64" />
            </Card>
          </div>
          <Card className="flex-1 p-6 space-y-4">
            {[1, 2, 3, 4, 5].map((i) => (
              <Skeleton key={i} className="h-10 w-full" />
            ))}
          </Card>
        </div>
      ) : !client ? (
        <Card className="flex flex-col items-center justify-center py-20 text-center">
          <Users className="h-10 w-10 text-muted-foreground/30 mb-3" />
          <p className="text-sm font-semibold text-muted-foreground">
            Client not found
          </p>
        </Card>
      ) : (
        <div className="flex flex-col md:flex-row gap-5 items-start w-full">
          {/* LEFT CARD */}
          <div className="flex flex-col gap-4">
            <Card className="p-6 flex flex-col items-center gap-3">
              <div className="w-20 h-20 rounded-xl bg-muted flex items-center justify-center">
                {initials ? (
                  <span className="text-2xl font-bold text-primary">
                    {initials}
                  </span>
                ) : (
                  <Users className="h-8 w-8 text-muted-foreground" />
                )}
              </div>
              <div className="text-center">
                <p className="text-sm font-semibold">{client.name}</p>

                <div className="mt-2">
                  <Badge
                    variant={getStatusVariant(client.status)}
                    className="text-[10px]"
                  >
                    {client.status}
                  </Badge>
                </div>
              </div>
            </Card>
          </div>

          {/* RIGHT CARD */}
          <Card className="p-6 flex flex-col w-full">
            <div className="flex items-center justify-between mb-5">
              <p className="text-sm font-semibold flex items-center gap-2">
                <Users className="h-4 w-4 text-primary" /> Client Information
              </p>
            </div>

            <div className="rounded-lg">
              {[
                { label: "Full Name", value: client.name },
                { label: "Email Address", value: client.email },
                { label: "Company", value: client.companyName ?? "—" },
                { label: "Phone", value: client.phone ?? "—" },
                { label: "GSTIN", value: client.gstin ?? "—" },
                { label: "PAN", value: client.pan ?? "—" },
                {
                  label: "Status",
                  value: (
                    <Badge
                      variant="outline"
                      className={`text-[10px] ${statusColor[client.status] ?? ""}`}
                    >
                      {client.status}
                    </Badge>
                  ),
                },
                { label: "Added", value: formatDateTime(client.createdAt) },
              ].map(({ label, value }) => (
                <div
                  key={label}
                  className="flex items-center justify-between px-4 py-3 border-b border-border last:border-0"
                >
                  <span className="text-xs font-semibold text-muted-foreground uppercase tracking-wide">
                    {label}
                  </span>
                  <span className="text-sm text-foreground">{value}</span>
                </div>
              ))}
            </div>
          </Card>
        </div>
      )}
    </div>
  );
}
