"use client";

import { useRouter } from "next/navigation";
import { Card, CardHeader, CardTitle, CardContent } 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,
  Mail,
  Phone,
  User,
  UserRound,
  ShieldCheck,
  Building2,
  Activity,
  CalendarDays,
} from "lucide-react";
import { useGetEmployee } from "../hooks/employee.hook";
import { getStatusVariant } from "@/lib/status-variant";
import { formatDateTime } from "@/lib/format-date";

interface Props {
  id: string;
}

export function ViewEmployeePage({ id }: Props) {
  const router = useRouter();
  const { data: emp, isLoading } = useGetEmployee(id);

  const initials = emp
    ? (emp.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">Employee Details</h2>
        <p className="text-muted-foreground text-sm mt-0.5">
          View employee information
        </p>
      </div>

      {isLoading ? (
        <div className="flex flex-col md:flex-row gap-5 items-start w-full">
          <div className="flex flex-col gap-4 w-full md:w-auto">
            <Card>
              <CardContent className="p-6">
                <Skeleton className="h-40 w-64" />
              </CardContent>
            </Card>
            <Skeleton className="h-16 w-64 rounded-xl" />
          </div>
          <Card className="flex-1 w-full">
            <CardContent className="p-6 space-y-4">
              {[1, 2, 3, 4, 5].map((i) => (
                <Skeleton key={i} className="h-10 w-full" />
              ))}
            </CardContent>
          </Card>
        </div>
      ) : !emp ? (
        <Card>
          <CardContent 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">
              Employee not found
            </p>
          </CardContent>
        </Card>
      ) : (
        <div className="flex flex-col md:flex-row gap-5 items-start w-full">
          {/* LEFT */}
          <div className="flex flex-col gap-4">
            <Card>
              <CardContent 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">
                  <div className="mt-2">
                    <Badge
                      variant={getStatusVariant(emp.status)}
                      className="text-[10px]"
                    >
                      {emp.status}
                    </Badge>
                  </div>
                </div>
              </CardContent>
            </Card>

            <Card>
              <CardContent className="p-4 flex gap-3">
                <Mail className="h-4 w-4 text-muted-foreground mt-0.5 shrink-0" />
                <p className="text-xs text-muted-foreground leading-relaxed">
                  Email cannot be changed after invitation. Contact admin to
                  update email.
                </p>
              </CardContent>
            </Card>
          </div>

          {/* RIGHT */}
          <Card className="w-full">
            <CardHeader>
              <CardTitle className="flex items-center gap-2">
                <Users className="h-4 w-4 text-primary" />
                Personal Information
              </CardTitle>
            </CardHeader>

            <CardContent>
              <div className="rounded-lg">
                {[
                  { label: "Full Name", value: emp.name },
                  { label: "Email Address", value: emp.email },
                  { label: "Phone", value: emp.phone ?? "—" },
                  {
                    label: "Gender",
                    value: emp.gender
                      ? emp.gender.charAt(0) + emp.gender.slice(1).toLowerCase()
                      : "—",
                  },
                  {
                    label: "Role",
                    value: (
                      <Badge
                        variant="outline"
                        className="text-[10px] capitalize"
                      >
                        {emp.role?.toLowerCase()}
                      </Badge>
                    ),
                  },
                  { label: "Department", value: emp.department?.name ?? "—" },
                  {
                    label: "Status",
                    value: (
                      <Badge
                        variant={getStatusVariant(emp.status)}
                        className="text-[10px]"
                      >
                        {emp.status}
                      </Badge>
                    ),
                  },
                  { label: "Joined", value: formatDateTime(emp.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>
            </CardContent>
          </Card>
        </div>
      )}
    </div>
  );
}
