import { type ReactElement } from "react";
import * as React from "react";
import { cn } from "@/lib/utils";

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

function Card({ className, ...props }: CardProps): ReactElement {
  return (
    <div
      className={cn(
        "flex flex-col gap-6 border bg-card py-6 text-card-foreground shadow-sm",
        className,
      )}
      data-slot="card"
      {...props}
    />
  );
}

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

function CardHeader({ className, ...props }: CardHeaderProps): ReactElement {
  return (
    <div
      className={cn(
        "@container/card-header grid auto-rows-min grid-rows-[auto_auto] items-center gap-2 px-6 has-data-[slot=card-action]:grid-cols-[1fr_auto] [.border-b]:pb-6",
        className,
      )}
      data-slot="card-header"
      {...props}
    />
  );
}

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

function CardTitle({ className, ...props }: CardTitleProps): ReactElement {
  return (
    <div
      className={cn("text-label-large", className)}
      data-slot="card-title"
      {...props}
    />
  );
}

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

function CardDescription({
  className,
  ...props
}: CardDescriptionProps): ReactElement {
  return (
    <div
      className={cn("text-body-small text-muted-foreground", className)}
      data-slot="card-description"
      {...props}
    />
  );
}

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

function CardAction({ className, ...props }: CardActionProps): ReactElement {
  return (
    <div
      className={cn(
        "col-start-2 row-span-2 row-start-1 self-start justify-self-end",
        className,
      )}
      data-slot="card-action"
      {...props}
    />
  );
}

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

function CardContent({ className, ...props }: CardContentProps): ReactElement {
  return (
    <div
      className={cn("px-6", className)}
      data-slot="card-content"
      {...props}
    />
  );
}

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

function CardFooter({ className, ...props }: CardFooterProps): ReactElement {
  return (
    <div
      className={cn("flex items-center px-6 [.border-t]:pt-6", className)}
      data-slot="card-footer"
      {...props}
    />
  );
}

export {
  Card,
  CardHeader,
  CardFooter,
  CardTitle,
  CardAction,
  CardDescription,
  CardContent,
};
