from __future__ import annotations

from pathlib import Path

from gokit.cli.main import main


def _write(path: Path, text: str) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(text, encoding="utf-8")


def test_grouped_summary_single_written(tmp_path: Path) -> None:
    study = tmp_path / "study.txt"
    population = tmp_path / "population.txt"
    assoc = tmp_path / "assoc.txt"
    obo = tmp_path / "go-basic.obo"

    _write(study, "gene1\ngene2\n")
    _write(population, "gene1\ngene2\ngene3\ngene4\n")
    _write(assoc, "gene1 GO:0000001\ngene2 GO:0000001\ngene3 GO:0000002\ngene4 GO:0000002\n")
    _write(
        obo,
        "\n".join(
            [
                "format-version: 1.2",
                "",
                "[Term]",
                "id: GO:0000001",
                "namespace: biological_process",
                "",
                "[Term]",
                "id: GO:0000002",
                "namespace: molecular_function",
                "",
            ]
        ),
    )

    out_prefix = tmp_path / "out" / "goea"
    rc = main(
        [
            "enrich",
            "--study",
            str(study),
            "--population",
            str(population),
            "--assoc",
            str(assoc),
            "--assoc-format",
            "id2gos",
            "--obo",
            str(obo),
            "--out",
            str(out_prefix),
            "--out-formats",
            "tsv",
        ]
    )
    assert rc == 0
    summary = out_prefix.with_suffix(".summary.tsv")
    assert summary.exists()
    text = summary.read_text(encoding="utf-8")
    assert "namespace" in text


def test_parquet_without_pyarrow_fails_cleanly(tmp_path: Path) -> None:
    study = tmp_path / "study.txt"
    population = tmp_path / "population.txt"
    assoc = tmp_path / "assoc.txt"
    obo = tmp_path / "go-basic.obo"

    _write(study, "gene1\ngene2\n")
    _write(population, "gene1\ngene2\ngene3\ngene4\n")
    _write(assoc, "gene1 GO:0000001\ngene2 GO:0000001\ngene3 GO:0000002\ngene4 GO:0000002\n")
    _write(
        obo,
        "\n".join(
            [
                "format-version: 1.2",
                "",
                "[Term]",
                "id: GO:0000001",
                "namespace: biological_process",
                "",
                "[Term]",
                "id: GO:0000002",
                "namespace: molecular_function",
                "",
            ]
        ),
    )

    out_prefix = tmp_path / "out" / "goea"
    rc = main(
        [
            "enrich",
            "--study",
            str(study),
            "--population",
            str(population),
            "--assoc",
            str(assoc),
            "--assoc-format",
            "id2gos",
            "--obo",
            str(obo),
            "--out",
            str(out_prefix),
            "--out-formats",
            "parquet",
        ]
    )
    parquet_path = out_prefix.with_suffix(".parquet")
    if rc == 0:
        assert parquet_path.exists()
    else:
        assert not parquet_path.exists()
