"use client";

import type { ReactNode } from "react";

import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { cn } from "@/lib/utils";

type ConfigDocPageProps = {
  title: string;
  titleSuffix?: ReactNode;
  description?: string;
  /** Category / play-type chips etc., rendered above the version toolbar. */
  filters?: ReactNode;
  toolbar?: ReactNode;
  /** @deprecated Pass `footer` on `ConfigDocToolbar` instead. */
  context?: ReactNode;
  children: ReactNode;
  className?: string;
  contentClassName?: string;
};

export function ConfigDocToolbar({
  switcher,
  actions,
  footer,
  className,
}: {
  switcher: ReactNode;
  actions: ReactNode;
  /** Live version / read-only hint — rendered as a subtle line under the main row. */
  footer?: ReactNode;
  className?: string;
}) {
  return (
    <div className={cn("rounded-lg border border-border/70 bg-background", className)}>
      <div className="flex flex-col gap-3 px-3 py-2.5 sm:flex-row sm:items-center sm:justify-between sm:gap-4">
        <div className="min-w-0 flex-1">{switcher}</div>
        <div className="flex shrink-0 flex-wrap items-center gap-1.5 sm:justify-end">{actions}</div>
      </div>
      {footer ? (
        <div className="border-t border-border/60 px-3 py-2 text-xs text-muted-foreground sm:px-3">{footer}</div>
      ) : null}
    </div>
  );
}

export function ConfigDocPage({
  title,
  titleSuffix,
  description,
  filters,
  toolbar,
  context,
  children,
  className,
  contentClassName,
}: ConfigDocPageProps) {
  return (
    <Card className={className}>
      <CardHeader className="border-b border-border/60 pb-4">
        <CardTitle className="text-lg">
          {title}
          {titleSuffix ? (
            <span className="ml-2 font-normal text-muted-foreground">{titleSuffix}</span>
          ) : null}
        </CardTitle>
        {description ? <CardDescription className="text-base">{description}</CardDescription> : null}
      </CardHeader>
      <CardContent className={cn("space-y-6 pt-6", contentClassName)}>
        {toolbar}
        {filters}
        {context}
        {children}
      </CardContent>
    </Card>
  );
}
