"use client";

import Link from "next/link";
import { useRouter } from "next/navigation";
import { ArrowLeft } from "lucide-react";
import { Fragment, type ReactNode } from "react";
import {
  Breadcrumb,
  BreadcrumbItem,
  BreadcrumbLink,
  BreadcrumbList,
  BreadcrumbPage,
  BreadcrumbSeparator,
} from "@/components/ui/breadcrumb";
import { Button } from "@/components/ui/button";
import { Separator } from "@/components/ui/separator";
import { cn } from "@/lib/utils";

/**
 * The page header used across every list, report and form screen.
 *
 * Breadcrumbs now come from the shadcn `Breadcrumb` primitive (correct
 * `nav`/`ol` semantics and `aria-current` handling), the back control from
 * `Button`, and the rule beneath from `Separator`.
 *
 * This and `FormSectionHeading` were previously two near-identical components,
 * each painting a tinted gradient panel, a 3px accent bar and a blurred blob —
 * decoration that competed with the page's own content and made the page title
 * the third-loudest thing on screen. Hierarchy now comes from type scale and
 * spacing instead of a coloured container.
 */

interface BreadcrumbItemData {
  label: string;
  href?: string;
}

export interface PageHeaderProps {
  title: string;
  breadcrumbs?: BreadcrumbItemData[];
  description?: string;
  actions?: ReactNode;
  /** Renders a back control to the left of the title. */
  showBackButton?: boolean;
  /** Optional glyph, shown in a soft brand tile before the title. */
  icon?: ReactNode;
  /** Kept for source compatibility with the old FormSectionHeading API. */
  eyebrow?: string;
  className?: string;
}

export function PageHeader({
  title,
  breadcrumbs = [],
  description,
  actions,
  showBackButton = false,
  icon,
  eyebrow,
  className,
}: PageHeaderProps) {
  const router = useRouter();

  return (
    <header className={cn("mb-6", className)}>
      {breadcrumbs.length > 0 && (
        <Breadcrumb className="mb-2.5">
          <BreadcrumbList className="text-xs sm:gap-1.5">
            {breadcrumbs.map((crumb, index) => {
              const isLast = index === breadcrumbs.length - 1;
              return (
                <Fragment key={`${crumb.label}-${index}`}>
                  <BreadcrumbItem>
                    {crumb.href && !isLast ? (
                      <BreadcrumbLink asChild>
                        <Link href={crumb.href}>{crumb.label}</Link>
                      </BreadcrumbLink>
                    ) : (
                      <BreadcrumbPage>{crumb.label}</BreadcrumbPage>
                    )}
                  </BreadcrumbItem>
                  {!isLast && <BreadcrumbSeparator />}
                </Fragment>
              );
            })}
          </BreadcrumbList>
        </Breadcrumb>
      )}

      <div className="flex flex-col gap-3 sm:flex-row sm:items-start sm:justify-between sm:gap-6">
        <div className="flex min-w-0 items-start gap-3">
          {showBackButton && (
            <Button
              variant="outline"
              size="icon-sm"
              onClick={() => router.back()}
              aria-label="Go back"
              className="mt-0.5 shrink-0"
            >
              <ArrowLeft className="size-4" aria-hidden />
            </Button>
          )}

          {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">
            {eyebrow && <p className="oxy-eyebrow mb-1">{eyebrow}</p>}
            <h1 className="truncate text-xl font-semibold tracking-tight text-foreground">
              {title}
            </h1>
            {description && (
              <p className="mt-1 max-w-2xl text-sm leading-relaxed text-muted-foreground">
                {description}
              </p>
            )}
          </div>
        </div>

        {actions && (
          <div className="flex flex-wrap items-center gap-2 sm:shrink-0">{actions}</div>
        )}
      </div>

      <Separator className="mt-4" />
    </header>
  );
}
