"use client";

import type { ReactNode } from "react";
import { cn } from "@/lib/utils";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog";

/**
 * Application dialog, composed from the shadcn `Dialog` primitive.
 *
 * Radix now owns the behaviour that used to be hand-rolled here — focus trap,
 * focus restore, Escape, scroll lock, `aria-modal`, portalling and the
 * enter/exit transitions. This component only contributes the product-specific
 * layout: eyebrow + icon tile, a scrollable body, and a footer that becomes a
 * full-width stack on phones.
 *
 * The prop shape is unchanged, so every caller keeps working as-is.
 */

type ModalSize = "sm" | "md" | "lg" | "xl";

const SIZE_CLASS: Record<ModalSize, string> = {
  sm: "sm:max-w-sm",
  md: "sm:max-w-lg",
  lg: "sm:max-w-2xl",
  xl: "sm:max-w-4xl",
};

export interface ModalShellProps {
  isOpen: boolean;
  onClose: () => void;
  title: ReactNode;
  /** Short line under the title. Rendered as the dialog's aria description. */
  description?: ReactNode;
  eyebrow?: ReactNode;
  icon?: ReactNode;
  children: ReactNode;
  footer?: ReactNode;
  size?: ModalSize;
  /** Set false for destructive flows where a stray click should not dismiss. */
  closeOnOverlayClick?: boolean;
  className?: string;
}

export function ModalShell({
  isOpen,
  onClose,
  title,
  description,
  eyebrow,
  icon,
  children,
  footer,
  size = "md",
  closeOnOverlayClick = true,
  className,
}: ModalShellProps) {
  return (
    <Dialog open={isOpen} onOpenChange={(open) => !open && onClose()}>
      <DialogContent
        className={cn(
          // Reset the primitive's uniform padding — this dialog has its own
          // header / body / footer bands with their own rules between them.
          "flex max-h-[92dvh] flex-col gap-0 overflow-hidden p-0",
          SIZE_CLASS[size],
          className
        )}
        onInteractOutside={(event) => {
          if (!closeOnOverlayClick) event.preventDefault();
        }}
      >
        <DialogHeader className="flex-row items-start gap-3 space-y-0 border-b border-border px-5 py-4 pr-12 text-left">
          {icon && (
            <span
              className="mt-0.5 flex size-9 shrink-0 items-center justify-center rounded-md bg-accent text-primary [&>svg]:size-[18px]"
              aria-hidden
            >
              {icon}
            </span>
          )}

          <div className="min-w-0 flex-1">
            {eyebrow && <p className="oxy-eyebrow mb-0.5">{eyebrow}</p>}
            <DialogTitle className="truncate text-base">{title}</DialogTitle>
            {description ? (
              <DialogDescription className="mt-1 leading-relaxed">
                {description}
              </DialogDescription>
            ) : (
              // Radix warns when a dialog has no description; this keeps the
              // a11y tree complete without rendering visible text.
              <DialogDescription className="sr-only">{title}</DialogDescription>
            )}
          </div>
        </DialogHeader>

        <div className="min-h-0 flex-1 overflow-y-auto px-5 py-4">{children}</div>

        {footer && (
          <DialogFooter className="border-t border-border bg-muted/40 px-5 py-3.5">
            {footer}
          </DialogFooter>
        )}
      </DialogContent>
    </Dialog>
  );
}
