import { Check } from "lucide-react";
import { Fragment } from "react";
import { CONTRACT_STEPS } from "./schema";

const GREEN = "var(--mantine-color-edr-green-5)";
const GREEN_DEEP = "var(--mantine-color-edr-green-7)";
const BORDER = "var(--mantine-color-edr-border-0)";
const MUTED = "var(--mantine-color-edr-muted-0)";
const INK = "var(--mantine-color-edr-text-0)";

// Structural so any wizard's step array works (contract + shipment wizards).
type StepItem = { id: number; label: string; short: string };

export function StepIndicator({
  step,
  steps = CONTRACT_STEPS as readonly StepItem[],
}: {
  step: number;
  steps?: readonly StepItem[];
}) {
  return (
    <div className="flex items-start">
      {steps.map((item, index) => {
        const done = step > item.id;
        const active = step === item.id;
        return (
          <Fragment key={item.id}>
            <div
              className="flex shrink-0 flex-col items-center gap-2"
              style={{ minWidth: 34 }}
            >
              <div
                style={{
                  width: 34,
                  height: 34,
                  borderRadius: "50%",
                  display: "flex",
                  alignItems: "center",
                  justifyContent: "center",
                  fontSize: 13,
                  fontWeight: 700,
                  flexShrink: 0,
                  transition: "all 0.2s",
                  ...(done
                    ? {
                        background:
                          "linear-gradient(135deg, #12B981, #0A8A5F)",
                        color: "#fff",
                        boxShadow: "0 4px 10px rgba(14,163,113,0.35)",
                      }
                    : active
                      ? {
                          border: `2.5px solid ${GREEN}`,
                          color: GREEN_DEEP,
                          backgroundColor: "#fff",
                          boxShadow: "0 0 0 4px rgba(14,163,113,0.12)",
                        }
                      : {
                          backgroundColor: "#fff",
                          color: MUTED,
                          border: `2px solid ${BORDER}`,
                        }),
                }}
              >
                {done ? (
                  <Check style={{ width: 15, height: 15 }} strokeWidth={3} />
                ) : (
                  index + 1
                )}
              </div>
              <span
                style={{
                  fontSize: 12,
                  fontWeight: active ? 700 : 500,
                  textAlign: "center",
                  lineHeight: 1.2,
                  maxWidth: 110,
                  transition: "color 0.2s",
                  color: step >= item.id ? INK : MUTED,
                }}
              >
                {item.short}
              </span>
            </div>
            {index < steps.length - 1 && (
              <div
                style={{
                  flex: 1,
                  height: 3,
                  borderRadius: 999,
                  margin: "16px 8px 0",
                  transition: "background 0.3s",
                  background: done
                    ? "linear-gradient(90deg, #0A8A5F, #12B981)"
                    : BORDER,
                }}
              />
            )}
          </Fragment>
        );
      })}
    </div>
  );
}
