/**
 * Drawer — WealthX DS overrides (vaul/shadcn base)
 *
 * Changes from shadcn default:
 *   - DrawerOverlay:  `bg-black/50` → `bg-foreground/50` — uses DS foreground token (matches Figma overlay)
 *   - DrawerContent:  removed `rounded-b-lg` / `rounded-t-lg` — sharp corners per Figma (no radius)
 *   - DrawerContent:  handle bar also shown for `direction=top` (not just bottom)
 *   - DrawerHeader:   removed `text-center` for top/bottom — always left-aligned per DS rule
 *   - DrawerTitle:    added `text-lg` — matches Figma Text-lg/Semibold
 */
import { type ReactElement } from "react";
import * as React from "react";
import { Drawer as DrawerPrimitive } from "vaul";
import { cn } from "@/lib/utils";
import { useThemeVars } from "@/lib/theme-provider";

export type DrawerProps = React.ComponentProps<typeof DrawerPrimitive.Root>;

function Drawer({ ...props }: DrawerProps): ReactElement {
  return <DrawerPrimitive.Root data-slot="drawer" {...props} />;
}

export type DrawerTriggerProps = React.ComponentProps<
  typeof DrawerPrimitive.Trigger
>;

function DrawerTrigger({ ...props }: DrawerTriggerProps): ReactElement {
  return <DrawerPrimitive.Trigger data-slot="drawer-trigger" {...props} />;
}

export type DrawerPortalProps = React.ComponentProps<
  typeof DrawerPrimitive.Portal
>;

function DrawerPortal({ ...props }: DrawerPortalProps): ReactElement {
  return <DrawerPrimitive.Portal data-slot="drawer-portal" {...props} />;
}

export type DrawerCloseProps = React.ComponentProps<
  typeof DrawerPrimitive.Close
>;

function DrawerClose({ ...props }: DrawerCloseProps): ReactElement {
  return <DrawerPrimitive.Close data-slot="drawer-close" {...props} />;
}

export type DrawerOverlayProps = React.ComponentProps<
  typeof DrawerPrimitive.Overlay
>;

function DrawerOverlay({
  className,
  ...props
}: DrawerOverlayProps): ReactElement {
  return (
    <DrawerPrimitive.Overlay
      className={cn(
        // WealthX: foreground/50 scrim — matches Figma overlay token
        "fixed inset-0 z-50 bg-foreground/50 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:fill-mode-forwards data-[state=open]:animate-in data-[state=open]:fade-in-0",
        className,
      )}
      data-slot="drawer-overlay"
      {...props}
    />
  );
}

export type DrawerContentProps = React.ComponentProps<
  typeof DrawerPrimitive.Content
>;

function DrawerContent({
  className,
  children,
  style,
  ...props
}: DrawerContentProps): ReactElement {
  const themeVars = useThemeVars();
  return (
    <DrawerPortal data-slot="drawer-portal">
      <DrawerOverlay />
      <DrawerPrimitive.Content
        className={cn(
          "group/drawer-content fixed z-50 flex h-auto flex-col bg-background",
          // WealthX: no rounded corners (Figma: sharp edges)
          "data-[vaul-drawer-direction=top]:inset-x-0 data-[vaul-drawer-direction=top]:top-0 data-[vaul-drawer-direction=top]:mb-24 data-[vaul-drawer-direction=top]:max-h-[80vh] data-[vaul-drawer-direction=top]:border-b",
          "data-[vaul-drawer-direction=bottom]:inset-x-0 data-[vaul-drawer-direction=bottom]:bottom-0 data-[vaul-drawer-direction=bottom]:mt-24 data-[vaul-drawer-direction=bottom]:max-h-[80vh] data-[vaul-drawer-direction=bottom]:border-t",
          "data-[vaul-drawer-direction=right]:inset-y-0 data-[vaul-drawer-direction=right]:right-0 data-[vaul-drawer-direction=right]:w-3/4 data-[vaul-drawer-direction=right]:border-l",
          "data-[vaul-drawer-direction=left]:inset-y-0 data-[vaul-drawer-direction=left]:left-0 data-[vaul-drawer-direction=left]:w-3/4 data-[vaul-drawer-direction=left]:border-r",
          className,
        )}
        data-slot="drawer-content"
        style={{ ...themeVars, ...style } as React.CSSProperties}
        {...props}
      >
        {/* WealthX: handle shown for both top and bottom directions */}
        <div className="mx-auto mt-4 hidden h-2 w-[100px] shrink-0 rounded-full bg-muted group-data-[vaul-drawer-direction=bottom]/drawer-content:block group-data-[vaul-drawer-direction=top]/drawer-content:block" />
        {children}
      </DrawerPrimitive.Content>
    </DrawerPortal>
  );
}

export type DrawerHeaderProps = React.ComponentProps<"div">;

function DrawerHeader({
  className,
  ...props
}: DrawerHeaderProps): ReactElement {
  return (
    <div
      // WealthX: always left-aligned — no text-center for any direction
      className={cn("flex flex-col gap-1 p-4", className)}
      data-slot="drawer-header"
      {...props}
    />
  );
}

export type DrawerFooterProps = React.ComponentProps<"div">;

function DrawerFooter({
  className,
  ...props
}: DrawerFooterProps): ReactElement {
  return (
    <div
      className={cn(
        "mt-auto flex flex-row justify-end gap-2 border-t border-border p-4",
        className,
      )}
      data-slot="drawer-footer"
      {...props}
    />
  );
}

export type DrawerTitleProps = React.ComponentProps<
  typeof DrawerPrimitive.Title
>;

function DrawerTitle({ className, ...props }: DrawerTitleProps): ReactElement {
  return (
    <DrawerPrimitive.Title
      // WealthX: text-lg matches Figma Text-lg/Semibold
      className={cn("text-h5 text-foreground", className)}
      data-slot="drawer-title"
      {...props}
    />
  );
}

export type DrawerDescriptionProps = React.ComponentProps<
  typeof DrawerPrimitive.Description
>;

function DrawerDescription({
  className,
  ...props
}: DrawerDescriptionProps): ReactElement {
  return (
    <DrawerPrimitive.Description
      className={cn("text-body-small text-muted-foreground", className)}
      data-slot="drawer-description"
      {...props}
    />
  );
}

export {
  Drawer,
  DrawerPortal,
  DrawerOverlay,
  DrawerTrigger,
  DrawerClose,
  DrawerContent,
  DrawerHeader,
  DrawerFooter,
  DrawerTitle,
  DrawerDescription,
};
