"use client";

import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Checkbox } from "@/components/ui/checkbox";
import { Label } from "@/components/ui/label";
import { LoadingTextSkeleton } from "@/components/shared/skeletons";
import { FIX_PACKAGES_ADMIN } from "@/lib/strings";
import type { ServiceItem } from "@/core/api/services/other-services";
import type { UseFormSetValue, UseFormWatch } from "react-hook-form";
import type { FixPackageInput } from "@/lib/validations/fix-packages";

export interface OtherServicesCardProps {
  availableServices: ServiceItem[];
  servicesLoading: boolean;
  watch: UseFormWatch<FixPackageInput>;
  setValue: UseFormSetValue<FixPackageInput>;
}

export function OtherServicesCard({
  availableServices,
  servicesLoading,
  watch,
  setValue,
}: OtherServicesCardProps) {
  const selectedServiceIds = watch("service_ids") || [];

  return (
    <Card>
      <CardHeader>
        <CardTitle>{FIX_PACKAGES_ADMIN.OTHER_SERVICES_TITLE}</CardTitle>
      </CardHeader>
      <CardContent>
        <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-x-6 gap-y-4">
          {servicesLoading ? (
            <LoadingTextSkeleton
              message={FIX_PACKAGES_ADMIN.LOADING_SERVICES}
              className="col-span-full"
            />
          ) : (
            availableServices.map((service) => {
              const serviceId = Number(service.id);
              return (
                <div
                  key={service.id || service.name}
                  className="flex items-start gap-3"
                >
                  <Checkbox
                    id={`service-${service.id}`}
                    checked={selectedServiceIds.includes(serviceId)}
                    onCheckedChange={(checked) => {
                      if (checked) {
                        setValue("service_ids", [
                          ...selectedServiceIds,
                          serviceId,
                        ]);
                      } else {
                        setValue(
                          "service_ids",
                          selectedServiceIds.filter((id) => id !== serviceId)
                        );
                      }
                    }}
                    className="mt-1"
                  />
                  <Label
                    htmlFor={`service-${service.id}`}
                    className="text-sm mt-1 font-normal text-muted-foreground cursor-pointer leading-tight"
                  >
                    {service.name}
                  </Label>
                </div>
              );
            })
          )}
        </div>
      </CardContent>
    </Card>
  );
}
