import type { ReactNode } from 'react';
import { cx } from '@/site/lib/cx';

/**
 * Line icons of the areas of expertise, drawn on a 48×48 grid. Keys match
 * `treatment_categories.icon` (= the category key); `aesthetics` marks the
 * medical aesthetics department (config/site.php → departments).
 *
 * Every shape declares a length of 1 (`pathLength`), which changes nothing
 * about how it is drawn but lets a scene draw the icon stroke by stroke
 * with one dash rule (the home page's areas showcase).
 */
const icons: Record<string, ReactNode> = {
    // Strands rising from the scalp, their follicles beneath
    hair: (
        <>
            <path pathLength={1} d="M5 31c6.5-2.2 12 2.2 19 0s12.5-2.2 19 0" />
            <path
                pathLength={1}
                d="M13 30.6c-.6-8.5 1.4-15.5 6.5-22M23 30.2c-.5-9 1.6-16.5 7-23M33 30.6c-.4-7.5 1.2-14 5.5-19.5"
            />
            <path
                pathLength={1}
                d="M13 31.5l-1 5M23 31.2l-1 5.3M33 31.5l-1 5"
            />
            <circle pathLength={1} cx="11.8" cy="38.6" r="1.9" />
            <circle pathLength={1} cx="21.8" cy="38.8" r="1.9" />
            <circle pathLength={1} cx="31.8" cy="38.6" r="1.9" />
        </>
    ),
    // A face and a sparkle (the medical aesthetics department)
    aesthetics: (
        <>
            <path
                pathLength={1}
                d="M21 7c-7.2 0-12 6-12 14.5 0 9.5 5.5 17.5 12 20 6.5-2.5 12-10.5 12-20C33 13 28.2 7 21 7Z"
            />
            <path
                pathLength={1}
                d="M39.5 3.5c.4 3 2 4.6 5 5-3 .4-4.6 2-5 5-.4-3-2-4.6-5-5 3-.4 4.6-2 5-5Z"
            />
            <circle
                pathLength={1}
                cx="17"
                cy="18.5"
                r="1"
                fill="currentColor"
                stroke="none"
            />
            <circle
                pathLength={1}
                cx="25.5"
                cy="27"
                r="1"
                fill="currentColor"
                stroke="none"
            />
        </>
    ),
    // Syringe
    botox: (
        <g transform="rotate(-45 24 24)">
            <path pathLength={1} d="M6 20v8M6 24h7" />
            <rect
                pathLength={1}
                x="13"
                y="19"
                width="19"
                height="10"
                rx="1.5"
            />
            <path
                pathLength={1}
                d="M13 16.5v15M18.5 19v3.5M23 19v3.5M27.5 19v3.5"
            />
            <path pathLength={1} d="M32 21.5l3.5 1v3l-3.5 1M35.5 24H44" />
        </g>
    ),
    // Lips
    fillers: (
        <>
            <path
                pathLength={1}
                d="M7 24c6-7.5 12.5-8.5 17-4 4.5-4.5 11-3.5 17 4"
            />
            <path pathLength={1} d="M7 24c8 11 26 11 34 0" />
            <path pathLength={1} d="M7 24c9 2.5 25 2.5 34 0" />
        </>
    ),
    // Droplets above the skin line
    mesotherapy: (
        <>
            <path pathLength={1} d="M24 6c6 7 6 15 0 15s-6-8 0-15Z" />
            <path
                pathLength={1}
                d="M11 20c3.6 4.3 3.6 9 0 9s-3.6-4.7 0-9ZM37 20c3.6 4.3 3.6 9 0 9s-3.6-4.7 0-9Z"
            />
            <path pathLength={1} d="M5 39c8-4 14 4 19 0s11 4 19 0" />
            <path pathLength={1} d="M11 33v2M24 26v4M37 33v2" />
        </>
    ),
    // Energy waves
    devices: (
        <>
            <circle
                pathLength={1}
                cx="11"
                cy="37"
                r="2.5"
                fill="currentColor"
                stroke="none"
            />
            <path
                pathLength={1}
                d="M11 27a10 10 0 0 1 10 10M11 18.5A18.5 18.5 0 0 1 29.5 37M11 10a27 27 0 0 1 27 27"
            />
        </>
    ),
    // Infusion bag
    'iv-therapy': (
        <>
            <path pathLength={1} d="M24 3v3" />
            <rect pathLength={1} x="15" y="6" width="18" height="22" rx="5" />
            <path pathLength={1} d="M15 16h18M20 21h3M24 28v5" />
            <rect pathLength={1} x="21" y="33" width="6" height="7" rx="2.5" />
            <path pathLength={1} d="M24 40v5M24 35.5v1" />
        </>
    ),
    // Mars symbol
    'mens-health': (
        <>
            <circle pathLength={1} cx="20" cy="28" r="11.5" />
            <path pathLength={1} d="M28.5 19.5 40 8M31 8h9v9" />
        </>
    ),
    // Infinity
    longevity: (
        <>
            <path
                pathLength={1}
                d="M24 24c-4-8-16-8-16 0s12 8 16 0c4-8 16-8 16 0s-12 8-16 0Z"
            />
            <circle
                pathLength={1}
                cx="24"
                cy="24"
                r="1.8"
                fill="currentColor"
                stroke="none"
            />
            <path pathLength={1} d="M24 9v3M24 36v3" />
        </>
    ),
    // Scalpel
    'plastic-surgery': (
        <>
            <path pathLength={1} d="M7 41 26 22" />
            <path
                pathLength={1}
                d="M26 22 38.5 7.5c3 6 .5 14.5-7 19.5L26 22Z"
            />
            <path pathLength={1} d="M10.5 37.5l3 3" />
        </>
    ),
    // Tooth
    dental: (
        <path
            pathLength={1}
            d="M16 8c-4 0-7 3-7 8 0 6 3 8 4 14 1 6 2 11 5 11s2-10 6-10 3 10 6 10 4-5 5-11c1-6 4-8 4-14 0-5-3-8-7-8s-5 2-8 2-4-2-8-2Z"
        />
    ),
    // Sparkles
    beauty: (
        <>
            <path
                pathLength={1}
                d="M21 8c1 10 6 15 16 16-10 1-15 6-16 16-1-10-6-15-16-16 10-1 15-6 16-16Z"
            />
            <path
                pathLength={1}
                d="M37 5c.4 3 2 4.6 5 5-3 .4-4.6 2-5 5-.4-3-2-4.6-5-5 3-.4 4.6-2 5-5Z"
            />
        </>
    ),
};

const fallback = <circle pathLength={1} cx="24" cy="24" r="14" />;

/**
 * The shapes of an icon alone, in their 48×48 space — for a drawing that
 * places and strokes them itself (the atlas of the treatments page).
 */
export function CategoryGlyph({ name }: { name: string }) {
    return <>{icons[name] ?? fallback}</>;
}

type CategoryIconProps = {
    name: string;
    className?: string;
    /** Accessible label; omit for decorative use next to a visible name. */
    title?: string;
};

/** A size utility among the caller's classes (size-5, md:size-12…). */
const SIZED = /(^|[\s:])size-/;

export function CategoryIcon({ name, className, title }: CategoryIconProps) {
    return (
        <svg
            viewBox="0 0 48 48"
            fill="none"
            stroke="currentColor"
            strokeWidth="1.3"
            strokeLinecap="round"
            strokeLinejoin="round"
            // The default size steps aside when the caller sets one: two
            // size classes on one element would be decided by their order
            // in the style sheet, not by the caller.
            className={cx(!SIZED.test(className ?? '') && 'size-12', className)}
            aria-hidden={title ? undefined : true}
            role={title ? 'img' : undefined}
        >
            {title ? <title>{title}</title> : null}
            {icons[name] ?? fallback}
        </svg>
    );
}
