"use client";

import Image from "next/image";
import { useTheme } from "next-themes";
import { useEffect, useState } from "react";
import { cn } from "@/lib/utils";

export const BRAND_NAME = "Miaro";

type BrandLogoProps = {
  /** Prefer dark logo (for light surfaces) or white logo (for dark surfaces) */
  variant?: "auto" | "dark" | "white";
  className?: string;
  /** Image height in px (width scales with intrinsic ratio) */
  height?: number;
  priority?: boolean;
  /** Clip to the mark only (useful in collapsed nav) */
  markOnly?: boolean;
};

/**
 * Global Miaro brand mark.
 * - Dark logo → light backgrounds
 * - White logo → dark backgrounds / brand panels
 */
export function BrandLogo({
  variant = "auto",
  className,
  height = 28,
  priority = false,
  markOnly = false,
}: BrandLogoProps) {
  const { resolvedTheme } = useTheme();
  const [mounted, setMounted] = useState(false);

  useEffect(() => setMounted(true), []);

  const useWhite =
    variant === "white" ||
    (variant === "auto" && mounted && resolvedTheme === "dark");

  const src = markOnly
    ? useWhite
      ? "/brand/miaro-mark-white.svg"
      : "/brand/miaro-mark.svg"
    : useWhite
      ? "/brand/miaro-logo-white.svg"
      : "/brand/miaro-logo-dark.svg";

  // Full lockup 967.59×205.65; mark 440×206
  const ratio = markOnly ? 440 / 206 : 967.59 / 205.65;
  const imgWidth = Math.round(height * ratio);

  return (
    <span
      className={cn(
        "inline-flex items-center min-w-0 max-w-full",
        markOnly && "overflow-hidden justify-start",
        className,
      )}
      style={{ width: imgWidth, maxWidth: "100%" }}
    >
      <Image
        src={src}
        alt={BRAND_NAME}
        width={imgWidth}
        height={height}
        unoptimized
        className="h-auto w-full max-w-full object-contain object-left"
        priority={priority}
      />
    </span>
  );
}

type BrandLockupProps = {
  variant?: "auto" | "dark" | "white";
  className?: string;
  height?: number;
  priority?: boolean;
  /** On dark brand panels use the white logo */
  onDarkSurface?: boolean;
};

/** Full Miaro Pulse lockup (mark + wordmark). */
export function BrandLockup({
  variant = "auto",
  className,
  height = 26,
  priority = false,
  onDarkSurface = false,
}: BrandLockupProps) {
  return (
    <BrandLogo
      variant={onDarkSurface ? "white" : variant}
      height={height}
      priority={priority}
      className={className}
    />
  );
}
