"use client";

import { Button } from "@/components/ui/button";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { FIX_PACKAGES_ADMIN } from "@/lib/strings";
import { getStorageUrl } from "@/lib/utils";
import type { PackageImageInput } from "@/lib/validations/fix-packages";
import { Plus, Upload, X } from "lucide-react";
import { useRef } from "react";

const MAX_SLIDER_IMAGES = 6;

export interface BannerSliderCardProps {
  bannerPreview: string | null;
  onBannerChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
  extraImages: PackageImageInput[];
  onSliderImagesChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
  onDeleteImage: (index: number) => void;
}

export function BannerSliderCard({
  bannerPreview,
  onBannerChange,
  extraImages,
  onSliderImagesChange,
  onDeleteImage,
}: BannerSliderCardProps) {
  const bannerInputRef = useRef<HTMLInputElement>(null);
  const sliderInputRef = useRef<HTMLInputElement>(null);

  return (
    <Card className="col-span-12">
      <CardHeader className="flex flex-row items-center justify-between">
        <CardTitle>{FIX_PACKAGES_ADMIN.BANNER_IMAGE}</CardTitle>
        <Button
          type="button"
          variant="outline"
          size="sm"
          onClick={() => sliderInputRef.current?.click()}
          disabled={extraImages.length >= MAX_SLIDER_IMAGES}
        >
          <Plus className="mr-2 h-4 w-4" />
          {FIX_PACKAGES_ADMIN.ADD_SLIDER_IMAGE}
          {extraImages.length > 0
            ? FIX_PACKAGES_ADMIN.SLIDER_IMAGE_COUNT.replace(
                "{count}",
                String(extraImages.length)
              )
            : ""}
        </Button>
      </CardHeader>
      <CardContent className="space-y-4">
        <div
          className="aspect-[32/9] bg-muted flex items-center justify-center border-2 border-dashed rounded-lg cursor-pointer overflow-hidden"
          onClick={() => bannerInputRef.current?.click()}
        >
          {bannerPreview ? (
            <img
              src={bannerPreview}
              alt={FIX_PACKAGES_ADMIN.BANNER_PREVIEW_ALT}
              className="w-full h-full object-cover"
            />
          ) : (
            <div className="text-center">
              <Upload className="mx-auto h-8 w-8 text-muted-foreground mb-2" />
              <p className="text-xs text-muted-foreground">
                {FIX_PACKAGES_ADMIN.CLICK_TO_UPLOAD_BANNER}
              </p>
            </div>
          )}
        </div>
        <input
          type="file"
          ref={bannerInputRef}
          className="hidden"
          accept="image/*"
          onChange={onBannerChange}
        />
        <input
          type="file"
          ref={sliderInputRef}
          className="hidden"
          accept="image/*"
          multiple
          onChange={onSliderImagesChange}
        />
        {extraImages.length > 0 && (
          <div className="space-y-2">
            <p className="text-sm font-medium text-muted-foreground">
              {FIX_PACKAGES_ADMIN.SLIDER_IMAGES}
            </p>
            <div className="grid grid-cols-6 gap-2">
              {extraImages.map((img, i) => (
                <div
                  key={i}
                  className="relative group aspect-square bg-muted rounded-md overflow-hidden"
                >
                  {img.image ? (
                    <img
                      src={
                        img.image instanceof File
                          ? URL.createObjectURL(img.image)
                          : getStorageUrl(img.image)
                      }
                      alt={`Slider image ${i}`}
                      className="w-full h-full object-cover"
                    />
                  ) : null}
                  <div className="absolute inset-0 bg-black/50 opacity-0 group-hover:opacity-100 transition-opacity flex items-center justify-center gap-2">
                    <Button
                      size="icon"
                      variant="ghost"
                      type="button"
                      onClick={() => onDeleteImage(i)}
                    >
                      <X className="h-4 w-4 text-white" />
                    </Button>
                  </div>
                </div>
              ))}
            </div>
          </div>
        )}
      </CardContent>
    </Card>
  );
}
