import * as React from "react";
import Link from "next/link";
import { Breadcrumbs, type Crumb } from "@/components/ui/breadcrumbs";
import { cn } from "@/lib/utils";

/**
 * Page-level heading.
 *
 * A monospaced folio marker above a display-face title, separated from the page
 * body by a heavy rule — the same hierarchy the prospectus sections use.
 *
 * Pass `crumbs` for the standard trail (it prepends "Dasbor" automatically);
 * `breadcrumb` remains for one-off nodes such as a bare "back" link.
 */
export function PageHeader({
  title,
  description,
  actions,
  crumbs,
  breadcrumb,
  folio,
  className,
}: {
  title: string;
  description?: string;
  actions?: React.ReactNode;
  /** Structured trail, e.g. `[{ label: "Kursus", href: "/dashboard/courses" }, { label: course.title }]`. */
  crumbs?: Crumb[];
  /** Escape hatch for a custom node. Takes precedence over `crumbs`. */
  breadcrumb?: React.ReactNode;
  folio?: string;
  className?: string;
}) {
  const trail = breadcrumb ?? (crumbs && crumbs.length > 0 ? <Breadcrumbs items={crumbs} /> : null);

  return (
    <div className={cn("border-b border-line pb-5", className)}>
      <div className="flex flex-col gap-4 sm:flex-row sm:items-end sm:justify-between">
        <div className="min-w-0 space-y-2">
          {folio ? <p className="ws-folio">{folio}</p> : null}
          {trail ? <div className="text-xs text-tertiary">{trail}</div> : null}
          <h1 className="ws-display text-[1.75rem] uppercase text-primary sm:text-[2rem]">
            {title}
          </h1>
          {description ? (
            <p className="max-w-3xl text-sm text-secondary">{description}</p>
          ) : null}
        </div>
        {actions ? (
          <div className="flex shrink-0 flex-wrap items-center gap-2">{actions}</div>
        ) : null}
      </div>
    </div>
  );
}

/** Labelled statistic tile — a printed figure box. */
export function StatCard({
  label,
  value,
  hint,
  icon,
  tone = "brand",
  href,
  className,
}: {
  label: string;
  value: React.ReactNode;
  hint?: string;
  icon?: React.ReactNode;
  tone?: "brand" | "success" | "warning" | "danger" | "accent";
  /** When set, the whole card becomes a link to the underlying detail view. */
  href?: string;
  className?: string;
}) {
  const toneClass = {
    brand: "border-subtle bg-sunken text-primary",
    success: "border-success-100 bg-success-50 text-success-700 dark:border-success-700/40 dark:bg-success-700/15 dark:text-success-500",
    warning: "border-warning-100 bg-warning-50 text-warning-700 dark:border-warning-700/40 dark:bg-warning-700/15 dark:text-warning-500",
    danger: "border-danger-100 bg-danger-50 text-danger-700 dark:border-danger-700/40 dark:bg-danger-700/15 dark:text-danger-500",
    accent: "border-accent-400/40 bg-accent-500/10 text-accent-600 dark:text-accent-400",
  }[tone];

  const body = (
    <>
      <div className="flex items-start justify-between gap-3">
        <p className="ws-label">{label}</p>
        {icon ? (
          <span className={cn("flex size-7 shrink-0 items-center justify-center border", toneClass)}>
            {icon}
          </span>
        ) : null}
      </div>
      <p className="ws-num mt-3 text-[1.75rem] font-semibold leading-none text-primary">{value}</p>
      {hint ? <p className="mt-2 text-xs text-tertiary">{hint}</p> : null}
    </>
  );

  if (href) {
    return (
      <Link
        href={href}
        className={cn(
          "block border border-subtle bg-surface p-4 transition-shadow hover:shadow-[var(--shadow-overlay)]",
          className
        )}
      >
        {body}
      </Link>
    );
  }

  return <div className={cn("border border-subtle bg-surface p-4", className)}>{body}</div>;
}

/** Section wrapper — rule-topped head over its content. */
export function Section({
  title,
  description,
  actions,
  children,
  className,
}: {
  title?: string;
  description?: string;
  actions?: React.ReactNode;
  children: React.ReactNode;
  className?: string;
}) {
  return (
    <section className={cn("space-y-4", className)}>
      {title ? (
        <div className="ws-head">
          <div>
            <h2 className="text-base">{title}</h2>
            {description ? <p className="mt-1 text-sm text-secondary">{description}</p> : null}
          </div>
          {actions}
        </div>
      ) : null}
      {children}
    </section>
  );
}
