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

interface ProgressProps extends React.HTMLAttributes<HTMLDivElement> {
  value: number;
  max?: number;
  tone?: "brand" | "success" | "warning" | "danger" | "accent";
  size?: "sm" | "md" | "lg";
  showLabel?: boolean;
  label?: string;
}

const TONE_CLASS: Record<NonNullable<ProgressProps["tone"]>, string> = {
  brand: "bg-brand-600",
  success: "bg-success-500",
  warning: "bg-warning-500",
  danger: "bg-danger-500",
  accent: "bg-accent-500",
};

const SIZE_CLASS: Record<NonNullable<ProgressProps["size"]>, string> = {
  sm: "h-1",
  md: "h-1.5",
  lg: "h-2",
};

/** Square bar — a ruled measure, not a pill. */
export function Progress({
  value,
  max = 100,
  tone = "brand",
  size = "md",
  showLabel = false,
  label,
  className,
  ...props
}: ProgressProps) {
  const safeMax = max <= 0 ? 100 : max;
  const percent = Math.max(0, Math.min(100, Math.round((value / safeMax) * 100)));

  return (
    <div className={cn("w-full", className)} {...props}>
      {showLabel ? (
        <div className="mb-1.5 flex items-center justify-between text-xs">
          <span className="ws-label">{label ?? "Progres"}</span>
          <span className="ws-num font-semibold text-primary">{percent}%</span>
        </div>
      ) : null}
      <div
        className={cn("w-full overflow-hidden border border-subtle bg-sunken", SIZE_CLASS[size])}
        role="progressbar"
        aria-valuenow={percent}
        aria-valuemin={0}
        aria-valuemax={100}
        aria-label={label ?? "Progres"}
      >
        <div
          className={cn("h-full transition-[width] duration-500 ease-out", TONE_CLASS[tone])}
          style={{ width: `${percent}%` }}
        />
      </div>
    </div>
  );
}

/** Thin ring progress used for XP / score dials. */
export function RingProgress({
  value,
  max = 100,
  size = 72,
  strokeWidth = 6,
  tone = "#16150f",
  children,
  className,
}: {
  value: number;
  max?: number;
  size?: number;
  strokeWidth?: number;
  tone?: string;
  children?: React.ReactNode;
  className?: string;
}) {
  const safeMax = max <= 0 ? 100 : max;
  const percent = Math.max(0, Math.min(100, (value / safeMax) * 100));
  const radius = (size - strokeWidth) / 2;
  const circumference = 2 * Math.PI * radius;
  const offset = circumference - (percent / 100) * circumference;

  return (
    <div className={cn("relative inline-flex items-center justify-center", className)} style={{ width: size, height: size }}>
      <svg width={size} height={size} className="-rotate-90" aria-hidden="true">
        <circle
          cx={size / 2}
          cy={size / 2}
          r={radius}
          fill="none"
          stroke="currentColor"
          strokeWidth={strokeWidth}
          className="text-sunken"
        />
        <circle
          cx={size / 2}
          cy={size / 2}
          r={radius}
          fill="none"
          stroke={tone}
          strokeWidth={strokeWidth}
          strokeDasharray={circumference}
          strokeDashoffset={offset}
          style={{ transition: "stroke-dashoffset 600ms ease-out" }}
        />
      </svg>
      <div className="absolute inset-0 flex flex-col items-center justify-center">{children}</div>
    </div>
  );
}
