import { describe, test, expect, beforeAll, afterAll } from "vitest"
import { existsSync, rmSync, mkdirSync, writeFileSync, copyFileSync, readFileSync } from "node:fs"
import path from "node:path"
import os from "node:os"
import { fileURLToPath } from "node:url"
import { spawnSync } from "node:child_process"
import { validateBrand } from "../src/config"

const __dirname = path.dirname(fileURLToPath(import.meta.url))
const DRAW_IMAGE_DIR = path.resolve(__dirname, "..")
const TMP = path.join(os.tmpdir(), `draw-image-bad-input-${process.pid}`)

beforeAll(() => {
  if (existsSync(TMP)) rmSync(TMP, { recursive: true, force: true })
  mkdirSync(TMP, { recursive: true })
})

afterAll(() => {
  if (existsSync(TMP)) rmSync(TMP, { recursive: true, force: true })
})

describe("e2e — bad input (unit-level, no file mutation)", () => {
  test("invalid brand.json rejected by validateBrand", () => {
    expect(() => validateBrand({ base: "#0a0a0a", surface: "#121212", fg: "#ededed", muted: "#a1a1aa" }))
      .toThrow(/accent/)
  })

  test("non-hex accent rejected", () => {
    expect(() => validateBrand({ base: "#0a0a0a", surface: "#121212", fg: "#ededed", muted: "#a1a1aa", accent: "green" }))
      .toThrow(/hex/)
  })

  test("cli exits non-zero with missing template file", () => {
    const out = path.join(TMP, "bad.png")
    const r = spawnSync("node", ["--experimental-strip-types", path.join(DRAW_IMAGE_DIR, "cli.ts"), "render", "nonexistent", "--title", "Bad", "--out", out], {
      encoding: "utf-8",
      cwd: DRAW_IMAGE_DIR,
    })
    expect(r.status).not.toBe(0)
  })
})