class GoblinPurrfect < Formula
  desc "Retro terminal word processor with WordPerfect files and modern equations"
  homepage "https://brew.goblinreactor.com/goblin-purrfect.html"
  url "https://brew.goblinreactor.com/sources/goblin-purrfect-0.1.0-goblin20260911.1.tar.gz"
  version "0.1.0-goblin20260911.1"
  sha256 "a45b229e31a0b853c834b1e5b988df7ca98e12644355cfb8ef482110ab8752fd"
  license "Apache-2.0"

  livecheck do
    skip "Reviewed development snapshots are published by the Goblin Reactor maintainer"
  end

  bottle do
    root_url "https://brew.goblinreactor.com/bottles"
    sha256 cellar: :any_skip_relocation, arm64_tahoe: "67d1acecccd77ba162f5086a726f1f1e441811a79a4bbd805a6619c37fb505b8"
  end

  depends_on "rust" => :build
  depends_on "aspell"
  depends_on :macos
  depends_on "python@3.14"

  def install
    ENV["PYTHONPATH"] = buildpath/"python"
    system "cargo", "test", "--locked", "--offline", "--all-targets"
    system "python3.14", "-m", "unittest", "discover", "-s", "python/tests", "-v"
    system "python3.14", "-m", "wp51_tools.package", "--output", "brew-bundle"
    archives = (buildpath/"brew-bundle").glob("*.tar.gz")
    odie "Expected exactly one admitted binary bundle" if archives.length != 1
    mkdir "brew-install" do
      system "tar", "-xzf", archives.first, "--strip-components=1"
      bin.install "bin/goblin-purrfect"
      doc.install "LICENSE", "NOTICE", "README.md", "THIRD-PARTY-NOTICES.txt", "docs", "third-party"
      pkgshare.install "assets", "themes", "examples", "python"
    end
    (doc/"release").install "SOURCE-MANIFEST.json", "Cargo.lock"
    man1.install "packaging/debian/goblin-purrfect.1"
    rust = Formula["rust"]
    runtime_notices = doc/"third-party/rust-stdlib-#{rust.version}"
    runtime_notices.mkpath
    cp rust.opt_share/"doc/rustc/COPYRIGHT-library.html", runtime_notices
    cp [rust.opt_prefix/"LICENSE-APACHE", rust.opt_prefix/"LICENSE-MIT"], runtime_notices
  end

  def caveats
    <<~EOS
      Run goblin-purrfect in Kitty or another supported terminal.
      Saving always writes to the filesystem where the editor is running.
      PDF export needs LuaLaTeX; print preview also needs Poppler.
      Install those separately if desired; this formula does not install TeX Live.
      Check optional features with: goblin-purrfect doctor
      The command is goblin-purrfect, not gp (which belongs to PARI/GP).
      Setup, snapshot limitations and licenses:
        https://brew.goblinreactor.com/goblin-purrfect.html
    EOS
  end

  test do
    system bin/"goblin-purrfect", "create-demo", "demo.wp"
    assert_match "byte-identical full no-op round trip",
                 shell_output("#{bin}/goblin-purrfect verify-roundtrip demo.wp")
    system bin/"goblin-purrfect", "export-latex", "demo.wp", "demo.tex"
    assert_match "\\documentclass", (testpath/"demo.tex").read
    assert_match "\\frac", shell_output("#{bin}/goblin-purrfect equation-latex 'x OVER y'")
    assert_path_exists doc/"LICENSE"
    assert_path_exists doc/"NOTICE"
    assert_path_exists doc/"THIRD-PARTY-NOTICES.txt"
    assert_path_exists man1/"goblin-purrfect.1"
    ENV["PYTHONPATH"] = pkgshare/"python"
    system formula_opt_bin("python@3.14")/"python3.14", "-m", "wp51_tools.pty_exit", bin/"goblin-purrfect"
    system formula_opt_bin("python@3.14")/"python3.14", "-m", "wp51_tools.pty_document", bin/"goblin-purrfect"
  end
end
