"use client";

import * as React from "react";
import { cva, type VariantProps } from "class-variance-authority";
import { AlertCircle, AlertTriangle, Info, Check, X } from "lucide-react";

import { cn } from "../../lib/utils";

/**
 * Alert Component
 *
 * Yüksek kaliteli, özelleştirilebilir ve erişilebilir alert bileşeni.
 * Bildirim, uyarı ve dikkat çekmek gereken içerikler için kullanılır.
 */

const MoonUIalertVariantsPro = cva(
  "relative w-full flex items-center gap-3 p-4 border text-foreground [&>svg]:shrink-0",
  {
    variants: {
      variant: {
        default: "bg-background text-foreground border-border",
        primary: "bg-primary/10 text-primary border-primary/30",
        success: "bg-success/10 text-success border-success/30",
        warning: "bg-warning/10 text-warning border-warning/30",
        error: "bg-destructive/10 text-destructive border-destructive/30",
        info: "bg-blue-500/10 text-blue-500 border-blue-500/30",
      },
      size: {
        sm: "py-2 text-xs",
        default: "py-3 text-sm",
        lg: "py-4 text-base",
      },
      radius: {
        none: "rounded-none",
        sm: "rounded-sm",
        default: "rounded-md",
        lg: "rounded-lg",
        full: "rounded-full",
      },
      withClose: {
        true: "pr-10",
      },
    },
    defaultVariants: {
      variant: "default",
      size: "default",
      radius: "default",
      withClose: false,
    },
  }
);

export interface AlertProps
  extends React.HTMLAttributes<HTMLDivElement>,
    VariantProps<typeof MoonUIalertVariantsPro> {
  /** Alert ikonunu gizler */
  hideIcon?: boolean;
  /** Kapatma butonu ekler */
  closable?: boolean;
  /** Kapatma butonu tıklandığında çalışacak fonksiyon */
  onClose?: () => void;
}

const MoonUIAlertPro = React.forwardRef<HTMLDivElement, AlertProps>(
  ({ className, variant = "default", size, radius, hideIcon = false, closable = false, onClose, children, ...props }, ref) => {
    // Alert türüne göre ikon belirleme
    const MoonUIIconPro = React.useMemo(() => {
      switch (variant) {
        case "success":
          return Check;
        case "warning":
          return AlertTriangle;
        case "error":
          return AlertCircle;
        case "info":
          return Info;
        default:
          return Info;
      }
    }, [variant]);

    return (
      <div
        ref={ref}
        role="alert"
        className={cn(MoonUIalertVariantsPro({ variant, size, radius, withClose: closable }), className)}
        {...props}
      >
        {!hideIcon && <MoonUIIconPro className="h-5 w-5" />}
        <div className="flex-1">{children}</div>
        {closable && onClose && (
          <button
            onClick={onClose}
            className="absolute right-3 top-3 inline-flex h-6 w-6 items-center justify-center rounded-full opacity-70 transition-opacity hover:opacity-100"
            aria-label="Kapat"
          >
            <X className="h-4 w-4" />
          </button>
        )}
      </div>
    );
  }
);
MoonUIAlertPro.displayName = "AlertPro";

const MoonUIAlertTitlePro = React.forwardRef<
  HTMLParagraphElement,
  React.HTMLAttributes<HTMLHeadingElement>
>(({ className, ...props }, ref) => (
  <h5
    ref={ref}
    className={cn("font-semibold leading-tight tracking-tight mb-1", className)}
    {...props}
  />
));
MoonUIAlertTitlePro.displayName = "AlertTitlePro";

const MoonUIAlertDescriptionPro = React.forwardRef<
  HTMLParagraphElement,
  React.HTMLAttributes<HTMLParagraphElement>
>(({ className, ...props }, ref) => (
  <div
    ref={ref}
    className={cn("text-sm leading-5 text-muted-foreground", className)}
    {...props}
  />
));
MoonUIAlertDescriptionPro.displayName = "AlertDescriptionPro";


// Internal aliases for Pro component usage
export const alertVariantsInternal = MoonUIalertVariantsPro
export const AlertInternal = MoonUIAlertPro
export const AlertTitleInternal = MoonUIAlertTitlePro
export const AlertDescriptionInternal = MoonUIAlertDescriptionPro

// Pro exports  
export { MoonUIalertVariantsPro, MoonUIAlertPro, MoonUIAlertTitlePro, MoonUIAlertDescriptionPro }

// Clean exports (without MoonUI prefix for easier usage)
export { MoonUIalertVariantsPro as alertVariants, MoonUIAlertPro as Alert, MoonUIAlertTitlePro as AlertTitle, MoonUIAlertDescriptionPro as AlertDescription };
