import type { ReactNode } from "react";
import type { RoomArt } from "@/lib/data";

type Common = { className?: string };

function wrap(children: ReactNode, className?: string) {
  return (
    <svg
      viewBox="0 0 800 500"
      role="img"
      aria-hidden="true"
      preserveAspectRatio="xMidYMid slice"
      className={className ?? "h-full w-full"}
    >
      {children}
    </svg>
  );
}

/* Colour "moods" — NexCart palette: mint, blush, teal accents */
type Tone = { wall: string; wallDeep: string; accent: string; deep: string };
const tones: Record<"warm" | "clay" | "sage", Tone> = {
  warm: { wall: "#F6EFE7", wallDeep: "#F1E5D6", accent: "#EE7B70", deep: "#B4574E" },
  clay: { wall: "#EAF2EF", wallDeep: "#DFEDE7", accent: "#0F7B7B", deep: "#0C6262" },
  sage: { wall: "#FDF3E3", wallDeep: "#F9E9CE", accent: "#F0A04B", deep: "#C77E28" },
};

const INK = "#173231";
const SOFT = "#42605C";
const CREAM = "#FDFEFE";
const PAPER = "#FFFFFF";

/* ------------------------------ Room scenes ------------------------------ */

function Scene({ t, children }: { t: Tone; children: ReactNode }) {
  return (
    <>
      <rect width="800" height="500" fill={t.wall} />
      <rect y="380" width="800" height="120" fill={t.wallDeep} />
      <rect y="378" width="800" height="4" fill={t.deep} opacity="0.35" />
      {children}
    </>
  );
}

function living(t: Tone) {
  return (
    <Scene t={t}>
      {/* arched window */}
      <path d="M96 380V210a92 92 0 0 1 184 0v170Z" fill={PAPER} stroke={t.deep} strokeWidth="5" />
      <path d="M188 128v252M104 246h168" stroke={t.deep} strokeWidth="3" opacity="0.5" />
      {/* sun */}
      <circle cx="150" cy="200" r="26" fill={t.accent} opacity="0.85" />
      {/* sofa */}
      <rect x="340" y="268" width="330" height="112" rx="16" fill={CREAM} stroke={INK} strokeWidth="5" />
      <rect x="322" y="238" width="70" height="142" rx="16" fill={CREAM} stroke={INK} strokeWidth="5" />
      <rect x="618" y="238" width="70" height="142" rx="16" fill={CREAM} stroke={INK} strokeWidth="5" />
      <path d="M340 324h330" stroke={INK} strokeWidth="4" />
      <rect x="368" y="236" width="80" height="58" rx="14" fill={t.accent} stroke={INK} strokeWidth="4" />
      <rect x="560" y="236" width="80" height="58" rx="14" fill={PAPER} stroke={INK} strokeWidth="4" />
      {/* legs */}
      <path d="M356 380v26M654 380v26" stroke={INK} strokeWidth="6" />
      {/* floor lamp */}
      <path d="M712 380V196" stroke={INK} strokeWidth="5" />
      <path d="M688 196h48l-10-44h-28Z" fill={t.accent} stroke={INK} strokeWidth="4" />
      <path d="M694 380h36" stroke={INK} strokeWidth="6" />
      {/* art */}
      <rect x="392" y="118" width="130" height="96" fill={PAPER} stroke={INK} strokeWidth="5" />
      <circle cx="457" cy="166" r="26" fill={t.accent} opacity="0.8" />
      {/* plant */}
      <path d="M270 380v-44" stroke={INK} strokeWidth="5" />
      <path d="M270 344c-30-8-44-30-40-58 30 2 46 22 40 58Zm0-14c26-14 34-40 26-68-28 8-40 34-26 68Z" fill={t.deep} opacity="0.75" />
    </Scene>
  );
}

function bedroom(t: Tone) {
  return (
    <Scene t={t}>
      {/* headboard */}
      <path d="M220 380V208a120 120 0 0 1 240 0v172Z" fill={PAPER} stroke={INK} strokeWidth="5" />
      {/* bed */}
      <rect x="188" y="300" width="424" height="80" rx="12" fill={CREAM} stroke={INK} strokeWidth="5" />
      <path d="M188 340h424" stroke={INK} strokeWidth="4" />
      <rect x="252" y="266" width="86" height="44" rx="12" fill={t.accent} stroke={INK} strokeWidth="4" />
      <rect x="352" y="266" width="86" height="44" rx="12" fill={PAPER} stroke={INK} strokeWidth="4" />
      <path d="M206 380v24M594 380v24" stroke={INK} strokeWidth="6" />
      {/* pendant */}
      <path d="M612 0v128" stroke={INK} strokeWidth="4" />
      <path d="M588 128h48l-8 36h-32Z" fill={t.deep} stroke={INK} strokeWidth="4" />
      <circle cx="612" cy="176" r="7" fill={t.accent} />
      {/* nightstand + vase */}
      <rect x="640" y="316" width="84" height="64" fill={PAPER} stroke={INK} strokeWidth="5" />
      <path d="M682 316v-28" stroke={INK} strokeWidth="4" />
      <path d="M682 290c-16-4-24-16-22-32 16 2 24 14 22 32Z" fill={t.deep} opacity="0.8" />
      {/* art */}
      <rect x="88" y="120" width="110" height="140" fill={PAPER} stroke={INK} strokeWidth="5" />
      <path d="M112 228c18-40 30-60 44-60s22 20 30 60Z" fill={t.accent} opacity="0.85" />
    </Scene>
  );
}

function kitchen(t: Tone) {
  return (
    <Scene t={t}>
      {/* hood */}
      <path d="M296 96h208l-34 74H330Z" fill={t.deep} stroke={INK} strokeWidth="5" />
      <path d="M400 0v96" stroke={INK} strokeWidth="6" />
      {/* shelves */}
      <path d="M84 176h180M84 246h180" stroke={INK} strokeWidth="5" />
      <path d="M110 176v-36m44 36v-44m44 44v-30" stroke={SOFT} strokeWidth="4" />
      <path d="M120 246v-30m50 30v-40m46 40v-24" stroke={SOFT} strokeWidth="4" />
      {/* counter */}
      <rect x="330" y="252" width="400" height="128" fill={PAPER} stroke={INK} strokeWidth="5" />
      <rect x="322" y="238" width="416" height="16" fill={t.accent} stroke={INK} strokeWidth="5" />
      <path d="M370 252v128M470 252v128M570 252v128" stroke={INK} strokeWidth="3" opacity="0.4" />
      {/* oven */}
      <rect x="588" y="286" width="112" height="94" rx="6" fill={t.wallDeep} stroke={INK} strokeWidth="5" />
      <circle cx="644" cy="332" r="20" fill="none" stroke={INK} strokeWidth="4" />
      <path d="M330 398h400" stroke={INK} strokeWidth="6" />
      {/* stools */}
      <path d="M180 398v-56m56 56v-56m-66 0h76" stroke={INK} strokeWidth="5" />
      <path d="M196 342l24-24m0 24l-24-24" stroke={SOFT} strokeWidth="4" opacity="0.6" />
      {/* pot on counter */}
      <path d="M420 238v-20h44v20" fill={t.accent} stroke={INK} strokeWidth="4" />
      <path d="M464 226h14" stroke={INK} strokeWidth="4" />
    </Scene>
  );
}

function bath(t: Tone) {
  return (
    <Scene t={t}>
      {/* round mirror */}
      <circle cx="400" cy="170" r="64" fill={PAPER} stroke={INK} strokeWidth="6" />
      <circle cx="400" cy="170" r="48" fill={t.wallDeep} />
      {/* vanity */}
      <rect x="320" y="300" width="160" height="80" fill={PAPER} stroke={INK} strokeWidth="5" />
      <path d="M320 300h160" stroke={t.accent} strokeWidth="10" />
      <path d="M400 268v32" stroke={INK} strokeWidth="4" />
      {/* tub */}
      <path d="M60 300h250v34a60 60 0 0 1-60 60H120a60 60 0 0 1-60-60Z" fill={CREAM} stroke={INK} strokeWidth="5" />
      <path d="M60 300h250" stroke={INK} strokeWidth="6" />
      <path d="M96 300v-56c0-20 14-34 34-34" fill="none" stroke={t.deep} strokeWidth="5" />
      <circle cx="134" cy="212" r="6" fill={t.deep} />
      <path d="M84 394v22M286 394v22" stroke={INK} strokeWidth="6" />
      {/* tiles */}
      <path d="M620 96v284M680 96v284M740 96v284M580 156h200M580 226h200M580 296h200" stroke={t.deep} strokeWidth="3" opacity="0.35" />
      {/* stool + towels */}
      <path d="M530 398v-52m64 52v-52m-72 0h80" stroke={INK} strokeWidth="5" />
      <rect x="540" y="316" width="46" height="30" rx="6" fill={t.accent} stroke={INK} strokeWidth="4" />
    </Scene>
  );
}

function dining(t: Tone) {
  return (
    <Scene t={t}>
      {/* pendants */}
      <path d="M300 0v90M500 0v130" stroke={INK} strokeWidth="4" />
      <path d="M276 90h48l-8 34h-32Z" fill={t.deep} stroke={INK} strokeWidth="4" />
      <path d="M476 130h48l-8 34h-32Z" fill={t.deep} stroke={INK} strokeWidth="4" />
      <circle cx="300" cy="136" r="6" fill={t.accent} />
      <circle cx="500" cy="176" r="6" fill={t.accent} />
      {/* table */}
      <rect x="196" y="290" width="408" height="18" rx="6" fill={PAPER} stroke={INK} strokeWidth="5" />
      <path d="M232 308v84m336-84v84" stroke={INK} strokeWidth="7" />
      {/* vase */}
      <path d="M382 290v-24h36v24" fill={t.accent} stroke={INK} strokeWidth="4" />
      <path d="M400 266c-4-24 6-40 26-48 2 22-6 38-26 48Zm0-14c-16-10-22-26-18-46 18 8 26 24 18 46Z" fill={t.deep} opacity="0.8" />
      {/* chairs */}
      <path d="M120 392v-96h20v96m-20-44h20" stroke={INK} strokeWidth="5" />
      <path d="M660 392v-96h20v96m-20-44h20" stroke={INK} strokeWidth="5" />
      <path d="M676 296h-40" stroke={INK} strokeWidth="5" />
      {/* window */}
      <rect x="620" y="96" width="130" height="150" fill={PAPER} stroke={INK} strokeWidth="5" />
      <path d="M685 96v150M620 171h130" stroke={INK} strokeWidth="3" />
    </Scene>
  );
}

function office(t: Tone) {
  return (
    <Scene t={t}>
      {/* window wall */}
      <rect x="80" y="80" width="150" height="300" fill={PAPER} stroke={INK} strokeWidth="5" />
      <path d="M155 80v300M80 230h150" stroke={INK} strokeWidth="3" />
      {/* shelf */}
      <path d="M560 150h180M560 230h180" stroke={INK} strokeWidth="5" />
      <path d="M590 150v-40m50 40v-52m50 52v-36" stroke={SOFT} strokeWidth="4" />
      <rect x="650" y="200" width="44" height="30" fill={t.accent} stroke={INK} strokeWidth="4" />
      {/* desk */}
      <rect x="300" y="270" width="260" height="14" fill={PAPER} stroke={INK} strokeWidth="5" />
      <path d="M318 284v108m224-108v108" stroke={INK} strokeWidth="6" />
      {/* laptop */}
      <path d="M390 270v-46h64v46" fill={t.wallDeep} stroke={INK} strokeWidth="4" />
      <path d="M380 270h84" stroke={INK} strokeWidth="5" />
      {/* lamp */}
      <path d="M510 270v-60l30-14" fill="none" stroke={INK} strokeWidth="4" />
      <path d="M534 190l24 10-8 22-24-10Z" fill={t.accent} stroke={INK} strokeWidth="4" />
      {/* chair */}
      <path d="M170 392c0-52 20-78 52-78" fill="none" stroke={INK} strokeWidth="5" />
      <path d="M170 392h88" stroke={INK} strokeWidth="5" />
    </Scene>
  );
}

function cafe(t: Tone) {
  return (
    <Scene t={t}>
      {/* bar */}
      <rect x="230" y="250" width="360" height="130" fill={t.deep} stroke={INK} strokeWidth="5" />
      <rect x="222" y="236" width="376" height="16" fill={t.accent} stroke={INK} strokeWidth="5" />
      <path d="M262 266v114m150-114v114m150-114v114" stroke={CREAM} strokeWidth="3" opacity="0.35" />
      {/* stools */}
      <path d="M150 398v-58m60 58v-58m-70-12h80" stroke={INK} strokeWidth="5" />
      <path d="M640 398v-58m60 58v-58m-70-12h80" stroke={INK} strokeWidth="5" />
      {/* pendants */}
      <path d="M320 0v110M420 0v140M520 0v110" stroke={INK} strokeWidth="4" />
      <path d="M302 110h36l-6 28h-24Z" fill={t.accent} stroke={INK} strokeWidth="4" />
      <path d="M402 140h36l-6 28h-24Z" fill={t.accent} stroke={INK} strokeWidth="4" />
      <path d="M502 110h36l-6 28h-24Z" fill={t.accent} stroke={INK} strokeWidth="4" />
      {/* back bar shelves */}
      <path d="M640 130h120M640 200h120" stroke={INK} strokeWidth="5" />
      <path d="M662 130v-34m40 34v-46m40 46v-28" stroke={SOFT} strokeWidth="4" />
      <path d="M670 200v-26m44 26v-38m36 38v-20" stroke={SOFT} strokeWidth="4" />
      {/* espresso machine */}
      <path d="M300 236v-56h96v56" fill={PAPER} stroke={INK} strokeWidth="4" />
      <circle cx="348" cy="208" r="14" fill="none" stroke={INK} strokeWidth="4" />
    </Scene>
  );
}

function loft(t: Tone) {
  return (
    <Scene t={t}>
      {/* steel window wall */}
      <rect x="70" y="70" width="300" height="310" fill={PAPER} stroke={INK} strokeWidth="6" />
      <path d="M220 70v310M70 225h300M70 148h300M70 302h300" stroke={INK} strokeWidth="4" />
      <circle cx="146" cy="187" r="24" fill={t.accent} opacity="0.85" />
      {/* plaster curve */}
      <path d="M470 500V190a150 150 0 0 1 300 0v310Z" fill={PAPER} stroke={INK} strokeWidth="5" />
      <path d="M470 300h300" stroke={INK} strokeWidth="3" opacity="0.3" />
      {/* low sofa */}
      <rect x="380" y="316" width="230" height="66" rx="14" fill={t.wallDeep} stroke={INK} strokeWidth="5" />
      <path d="M396 382v18m198-18v18" stroke={INK} strokeWidth="6" />
      <rect x="404" y="292" width="66" height="42" rx="12" fill={t.accent} stroke={INK} strokeWidth="4" />
      {/* pendant */}
      <path d="M740 0v110" stroke={INK} strokeWidth="4" />
      <circle cx="740" cy="130" r="22" fill={t.deep} stroke={INK} strokeWidth="4" />
      {/* surfboard */}
      <path d="M120 500c-26-90-26-180 0-270 26 90 26 180 0 270Z" fill={t.accent} stroke={INK} strokeWidth="5" />
    </Scene>
  );
}

function studio(t: Tone) {
  return (
    <Scene t={t}>
      {/* easel */}
      <path d="M170 500 300 90m130 410L300 90m0 0v330" stroke={INK} strokeWidth="5" />
      <rect x="228" y="140" width="144" height="120" fill={PAPER} stroke={INK} strokeWidth="5" />
      <path d="M252 232c20-42 34-62 48-62s24 20 34 62Z" fill={t.accent} opacity="0.85" />
      <circle cx="336" cy="176" r="16" fill={t.deep} opacity="0.8" />
      {/* daybed */}
      <rect x="430" y="300" width="290" height="80" rx="12" fill={CREAM} stroke={INK} strokeWidth="5" />
      <path d="M430 340h290" stroke={INK} strokeWidth="4" />
      <rect x="448" y="264" width="72" height="44" rx="12" fill={t.accent} stroke={INK} strokeWidth="4" />
      <path d="M450 380v18m250-18v18" stroke={INK} strokeWidth="6" />
      {/* shelf */}
      <path d="M96 220h150M96 300h150" stroke={INK} strokeWidth="5" />
      <path d="M126 220v-34m48 34v-44m42 44v-28" stroke={SOFT} strokeWidth="4" />
      <rect x="120" y="272" width="40" height="28" fill={t.wallDeep} stroke={INK} strokeWidth="4" />
      <path d="M196 300v-30m30 30v-40" stroke={SOFT} strokeWidth="4" />
    </Scene>
  );
}

function terrace(t: Tone) {
  return (
    <Scene t={t}>
      {/* arch */}
      <path d="M180 500V230a130 130 0 0 1 260 0v270Z" fill={PAPER} stroke={INK} strokeWidth="6" />
      {/* sun + hills */}
      <circle cx="330" cy="220" r="34" fill={t.accent} />
      <path d="M200 340c40-40 80-56 110-40s70 10 110-24v224H200Z" fill={t.deep} opacity="0.4" />
      {/* railing */}
      <path d="M470 340h330m-330-52h330" stroke={INK} strokeWidth="5" />
      <path d="M490 288v104m60-104v104m60-104v104m60-104v104m60-104v104" stroke={INK} strokeWidth="4" />
      {/* plant */}
      <path d="M600 340v-40" stroke={INK} strokeWidth="5" />
      <path d="M600 302c-26-8-38-26-34-52 26 2 40 20 34 52Zm0-10c22-12 30-34 22-58-24 6-34 30-22 58Z" fill={t.deep} opacity="0.8" />
    </Scene>
  );
}

const scenes: Record<RoomArt, (t: Tone) => ReactNode> = {
  living, bedroom, kitchen, bath, dining, office, cafe, loft, studio, terrace,
};

export function RoomArtwork({
  kind,
  tone = "warm",
  className,
}: {
  kind: RoomArt;
  tone?: "warm" | "clay" | "sage";
  className?: string;
}) {
  return wrap(scenes[kind](tones[tone]), className);
}

/* ------------------------------ Service icons ---------------------------- */

function iconWrap(children: ReactNode, className?: string) {
  return (
    <svg
      viewBox="0 0 48 48"
      fill="none"
      stroke="currentColor"
      strokeWidth="2"
      strokeLinecap="round"
      strokeLinejoin="round"
      aria-hidden="true"
      className={className ?? "h-7 w-7"}
    >
      {children}
    </svg>
  );
}

const icons = {
  sofa: (
    <>
      <path d="M8 22v-6a4 4 0 0 1 4-4h24a4 4 0 0 1 4 4v6" />
      <path d="M8 22a5 5 0 0 1 5 5v4h22v-4a5 5 0 0 1 10 0v6a5 5 0 0 1-5 5H8a5 5 0 0 1-5-5v-6a5 5 0 0 1 5-5Z" />
      <path d="M10 42v-4m28 4v-4" />
    </>
  ),
  lamp: (
    <>
      <path d="M16 6h16l4 12H12Z" />
      <path d="M24 18v22m-8 4h16" />
    </>
  ),
  palette: (
    <>
      <path d="M24 6a18 18 0 1 0 0 36c3 0 5-2 5-5s-2-4-2-7 2-4 5-4h6a4 4 0 0 0 4-4c0-9-8-16-18-16Z" />
      <circle cx="16" cy="18" r="1.6" fill="currentColor" />
      <circle cx="25" cy="14" r="1.6" fill="currentColor" />
      <circle cx="14" cy="27" r="1.6" fill="currentColor" />
    </>
  ),
  ruler: (
    <>
      <rect x="4" y="18" width="40" height="12" rx="2" transform="rotate(-20 24 24)" />
      <path d="M15 26l2-5m6 9 2-5m6 9 2-5" />
    </>
  ),
  key: (
    <>
      <circle cx="17" cy="17" r="9" />
      <circle cx="17" cy="17" r="3" />
      <path d="M23.5 23.5 40 40m-6-2 4-4m-10-2 4-4" />
    </>
  ),
  plan: (
    <>
      <rect x="6" y="6" width="36" height="36" />
      <path d="M6 20h14V6m8 36V26h14" />
    </>
  ),
  bath: (
    <>
      <path d="M6 26h36v4a10 10 0 0 1-10 10H16A10 10 0 0 1 6 30Z" />
      <path d="M10 26V12a6 6 0 0 1 12 0" />
      <path d="M12 40l-2 4m28-4 2 4" />
    </>
  ),
} as const;

export type ServiceIconName = keyof typeof icons;

export function ServiceIcon({ name, className }: { name: string; className?: string }) {
  return iconWrap(icons[name as ServiceIconName] ?? icons.plan, className);
}

/* --------------------------------- Logo ---------------------------------- */

export function LogoMark({ className }: Common) {
  return (
    <svg viewBox="0 0 32 32" aria-hidden="true" className={className ?? "h-8 w-8"}>
      <path d="M4 28V16a12 12 0 0 1 24 0v12" fill="none" stroke="currentColor" strokeWidth="2.4" />
      <circle cx="16" cy="17" r="4" fill="currentColor" />
      <path d="M2 28h28" stroke="currentColor" strokeWidth="2.4" />
    </svg>
  );
}
