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

/**
 * Low-level shimmer block used while a server component streams in.
 *
 * Purely decorative: hidden from assistive tech so screen readers rely on the
 * surrounding `aria-busy` region rather than announcing dozens of empty boxes.
 */
export function Skeleton({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) {
  return (
    <div
      aria-hidden="true"
      className={cn("animate-pulse bg-sunken", className)}
      {...props}
    />
  );
}

/** Skeleton for a single stat/metric tile. */
export function SkeletonStat() {
  return (
    <div className="border border-subtle bg-surface p-4">
      <div className="flex items-start justify-between gap-3">
        <div className="space-y-2">
          <Skeleton className="h-3 w-24" />
          <Skeleton className="h-7 w-16" />
          <Skeleton className="h-3 w-32" />
        </div>
        <Skeleton className="size-8" />
      </div>
    </div>
  );
}

/** Skeleton for a row-based card list (announcements, notifications, classes). */
export function SkeletonList({ rows = 4 }: { rows?: number }) {
  return (
    <div className="space-y-3" aria-busy="true">
      {Array.from({ length: rows }).map((_, index) => (
        <div
          key={index}
          className="flex items-center gap-4 border border-subtle bg-surface p-4"
        >
          <Skeleton className="size-8 shrink-0" />
          <div className="flex-1 space-y-2">
            <Skeleton className="h-4 w-1/3" />
            <Skeleton className="h-3 w-2/3" />
          </div>
          <Skeleton className="h-5 w-20 shrink-0" />
        </div>
      ))}
    </div>
  );
}

/** Skeleton for tabular data (users, gradebook, audit log). */
export function SkeletonTable({ rows = 6, columns = 4 }: { rows?: number; columns?: number }) {
  return (
    <div className="border border-subtle bg-surface" aria-busy="true">
      <div className="flex gap-4 border-b border-subtle bg-sunken px-4 py-3">
        {Array.from({ length: columns }).map((_, index) => (
          <Skeleton key={index} className="h-3 flex-1" />
        ))}
      </div>
      {Array.from({ length: rows }).map((_, rowIndex) => (
        <div key={rowIndex} className="flex items-center gap-4 border-b border-subtle px-4 py-3.5 last:border-0">
          {Array.from({ length: columns }).map((_, colIndex) => (
            <Skeleton key={colIndex} className={cn("h-4 flex-1", colIndex === 0 && "max-w-48")} />
          ))}
        </div>
      ))}
    </div>
  );
}
