import { Command } from "commander";
import path from "node:path";
import { addEntity, getEntity, updateEntity, deleteEntity, listEntities } from "../lib/registry.js";
import { slugify } from "../lib/ids.js";
import { out, ok } from "../lib/output.js";
import { raiseError } from "../lib/errors/index.js";
import { extractSvgFile } from "../lib/svg-extract.js";

export function brandCmd() {
  const cmd = new Command("brand").description("Manage brands (design systems)");

  cmd
    .command("create")
    .description("Create a new brand")
    .requiredOption("--name <name>", "Brand name")
    .option("--url <url>", "Website URL")
    .option("--primary <color>", "Primary color")
    .option("--secondary <color>", "Secondary color")
    .option("--accent <color>", "Accent color")
    .option("--font <family>", "Font family")
    .action(async (opts) => {
      const id = slugify(opts.name);
      const data: Record<string, unknown> = {
        name: opts.name,
        slug: id,
        createdAt: new Date().toISOString(),
      };
      if (opts.url) data.url = opts.url;
      if (opts.primary || opts.secondary || opts.accent) {
        data.colors = {
          ...(opts.primary && { primary: opts.primary }),
          ...(opts.secondary && { secondary: opts.secondary }),
          ...(opts.accent && { accent: opts.accent }),
        };
      }
      if (opts.font) data.font = opts.font;

      const brand = await addEntity("brands", id, data);
      ok(`Brand created: ${id}`);
      out(brand);
    });

  cmd
    .command("list")
    .description("List all brands")
    .action(async () => {
      const brands = await listEntities("brands");
      out(
        brands.map((b: any) => ({
          id: b.id,
          name: b.name,
          url: b.url || "—",
        }))
      );
    });

  cmd
    .command("show <id>")
    .description("Show brand details")
    .action(async (id: string) => {
      const brand = await getEntity("brands", id);
      if (!brand) raiseError("E_NOT_FOUND", { kind: "Brand", id });
      out(brand);
    });

  cmd
    .command("update <id>")
    .description("Update a brand")
    .option("--name <name>", "New name")
    .option("--url <url>", "New URL")
    .option("--primary <color>", "Primary color")
    .action(async (id: string, opts: any) => {
      const updates: Record<string, unknown> = {};
      if (opts.name) updates.name = opts.name;
      if (opts.url) updates.url = opts.url;
      if (opts.primary) updates.colors = { primary: opts.primary };
      const brand = await updateEntity("brands", id, updates);
      if (!brand) raiseError("E_NOT_FOUND", { kind: "Brand", id });
      ok(`Brand updated: ${id}`);
      out(brand);
    });

  cmd
    .command("delete <id>")
    .description("Delete a brand")
    .action(async (id: string) => {
      const ok_ = await deleteEntity("brands", id);
      if (!ok_) raiseError("E_NOT_FOUND", { kind: "Brand", id });
      ok(`Brand deleted: ${id}`);
      out({ deleted: id });
    });

  // ── extract (#049) ─────────────────────────────────────────────────────
  // `ralphy brand extract <svg>` — list compound paths, fill-rule attrs,
  // interior polygons, overlay rects. Helps agents avoid the
  // twitch-fb-ads-001 missed-white-interior bug.
  cmd
    .command("extract <svg>")
    .description(
      "Parse an SVG and report layer structure: compound paths, fill-rule, interior polygons, overlay rects. JSON output.",
    )
    .action(async (svg: string) => {
      const p = path.resolve(svg);
      try {
        const report = await extractSvgFile(p);
        out(report);
      } catch (e) {
        raiseError("E_FILE_UNREADABLE", { path: p, detail: (e as Error).message });
      }
    });

  return cmd;
}
