import { useNavigate, useParams } from "react-router-dom";
import { ArrowLeftIcon, EditIcon, ClockIcon } from "lucide-react";
import { Button } from "@/shared/common/ui/button";
import { Card, CardContent } from "@/shared/common/ui/card";
import { Skeleton } from "@/shared/common/ui/skeleton";
import { Separator } from "@/shared/common/ui/separator";
import { useService } from "@/performance-management/hooks/useServiceHook"; // your hook
import { useLocalizedName } from "@/shared/common/localizedName";

const ServiceDetails = () => {
  const { id } = useParams<{ id: string }>();
  const navigate = useNavigate();
  const localizedName = useLocalizedName();
  const { data: service, isLoading, isError, refetch } = useService(id || "");

  if (isLoading) {
    return (
      <div className="p-6 space-y-6">
        <Skeleton className="h-8 w-2/5" />
        <Card>
          <CardContent className="p-6 space-y-4">
            <Skeleton className="h-5 w-1/4" />
            <Skeleton className="h-9 w-3/5" />
            <Skeleton className="h-5 w-1/4" />
            <Skeleton className="h-9 w-3/5" />
          </CardContent>
        </Card>
        <div className="flex gap-4">
          <Skeleton className="h-9 w-28" />
          <Skeleton className="h-9 w-28" />
        </div>
      </div>
    );
  }

  if (isError || !service) {
    return (
      <div className="p-6 space-y-6">
        <Card>
          <CardContent className="p-6">
            <div className="text-red-600 text-lg mb-4">
              {isError ? "Error loading service details" : "Service not found"}
            </div>
            <div className="flex gap-4">
              <Button onClick={() => refetch()} variant="outline">
                Retry
              </Button>
              <Button
                variant="ghost"
                onClick={() => navigate("/performance-management/services")}
                className="flex items-center gap-2">
                <ArrowLeftIcon className="h-4 w-4" />
                Back to List
              </Button>
            </div>
          </CardContent>
        </Card>
      </div>
    );
  }

  return (
    <div className="p-6 space-y-6">
      <div className="flex justify-between items-center">
        <h2 className="text-2xl font-bold">Service Details</h2>
        <Button
          variant="ghost"
          onClick={() => navigate("/performance-management/services")}
          className="flex items-center gap-2">
          <ArrowLeftIcon className="h-4 w-4" />
          Back to List
        </Button>
      </div>

      <Card>
        <CardContent className="p-6 space-y-6">
          {/* Service Name */}
          <div>
            <div className="text-sm text-muted-foreground mb-2">
              Service Name
            </div>
            <div className="p-3 border rounded-md bg-muted/50">
              <div className="text-base">
                {localizedName(service.data.name)}
              </div>
            </div>
          </div>

          {/* Description */}
          <div>
            <div className="text-sm text-muted-foreground mb-2">
              Description
            </div>
            <div className="p-3 border rounded-md bg-muted/50">
              <div className="text-base">
                {localizedName(service.data.description)}
              </div>
            </div>
          </div>

          {/* Category */}
          <div>
            <div className="text-sm text-muted-foreground mb-2">Created At</div>
            <div className="p-3 border rounded-md bg-muted/50">
              <div className="text-base">
                {service.data.amharicCreatedAt || "N/A"}
              </div>
            </div>
          </div>

          {/* Estimated Duration and Status */}
          <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
            <div>
              <div className="text-sm text-muted-foreground mb-2">
                Estimated Duration
              </div>
              <div className="p-3 border rounded-md bg-muted/50">
                <div className="flex items-center gap-2">
                  <ClockIcon className="h-4 w-4" />
                  <div className="text-base">
                    {service.data.estimatedTime}{" "}
                    {service.data.estimatedTimeUnit?.toLowerCase()}
                  </div>
                </div>
              </div>
            </div>
          </div>
          <Separator />

          {/* Timestamps */}
          <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
            <div>
              <div className="text-sm text-muted-foreground mb-2">
                Created At
              </div>
              <div className="p-3 border rounded-md bg-muted/50">
                <div className="text-base">
                  {service.data.createdAt
                    ? new Date(service.data.createdAt).toLocaleString()
                    : "N/A"}
                </div>
              </div>
            </div>

            <div>
              <div className="text-sm text-muted-foreground mb-2">
                Last Updated
              </div>
              <div className="p-3 border rounded-md bg-muted/50">
                <div className="text-base">
                  {service.data.updatedAt
                    ? new Date(service.data.updatedAt).toLocaleString()
                    : "N/A"}
                </div>
              </div>
            </div>
          </div>

          
        </CardContent>
      </Card>
    </div>
  );
};

export default ServiceDetails;
