"use client"

import * as React from "react"
import * as AvatarPrimitive from "@radix-ui/react-avatar"
import { Dialog, DialogContent } from "@/components/ui/dialog"
import { cn } from "@/lib/utils"
import { X } from "lucide-react"

const Avatar = React.forwardRef<
  React.ElementRef<typeof AvatarPrimitive.Root>,
  React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Root>
>(({ className, ...props }, ref) => (
  <AvatarPrimitive.Root
    ref={ref}
    className={cn(
      "relative flex h-10 w-10 shrink-0 overflow-hidden rounded-full cursor-pointer",
      className
    )}
    {...props}
  />
))
Avatar.displayName = AvatarPrimitive.Root.displayName

interface EnhancedAvatarImageProps extends React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Image> {
  viewerClassName?: string
}

const AvatarImage = React.forwardRef<
  React.ElementRef<typeof AvatarPrimitive.Image>,
  EnhancedAvatarImageProps
>(({ className, viewerClassName, src, alt, ...props }, ref) => {
  const [isOpen, setIsOpen] = React.useState(false)

  console.log(isOpen)
  return (
    <>
      <AvatarPrimitive.Image
        ref={ref}
        src={src}
        alt={alt}
        className={cn("aspect-square h-full w-full", className)}
        onClick={() => setIsOpen(true)}
        {...props}
      />

      <Dialog open={isOpen} onOpenChange={setIsOpen}>
        <DialogContent className="max-w-[90vw] max-h-[90vh] p-0 bg-transparent border-none" hideCloseButton>
          <div className="relative w-full h-full flex items-center justify-center rounded-lg overflow-hidden">
            {/* Blurred background using the same image */}
            <div 
              className="absolute inset-0 bg-cover bg-center bg-no-repeat blur-xl opacity-90 scale-110"
              style={{ backgroundImage: `url(${src})` }}
            />
            {/* Semi-transparent overlay to improve contrast */}
            <div className="absolute inset-0 bg-white/10 backdrop-blur-xl" />
            
            <button
              onClick={() => setIsOpen(false)}
              className="absolute top-4 right-4 text-white hover:text-gray-300 focus:outline-none z-10"
            >
              <X className="h-6 w-6" />
            </button>
            
            <img
              src={src}
              alt={alt}
              className={cn(
                "relative z-10 max-w-full max-h-[80vh] object-contain",
                viewerClassName
              )}
            />
          </div>
        </DialogContent>
      </Dialog>
    </>
  )
})
AvatarImage.displayName = AvatarPrimitive.Image.displayName

const AvatarFallback = React.forwardRef<
  React.ElementRef<typeof AvatarPrimitive.Fallback>,
  React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Fallback>
>(({ className, ...props }, ref) => (
  <AvatarPrimitive.Fallback
    ref={ref}
    className={cn(
      "flex h-full w-full items-center justify-center rounded-full bg-muted",
      className
    )}
    {...props}
  />
))
AvatarFallback.displayName = AvatarPrimitive.Fallback.displayName

export { Avatar, AvatarImage, AvatarFallback }