from abi.exporters import NextflowExporter
from abi.plugins import get_plugin


def test_nextflow_exporter_renders_processes_from_abi_plan(tmp_path):
    plugin = get_plugin("metatranscriptomics")
    config = plugin.load_config(
        overrides={
            "outdir": str(tmp_path / "results"),
            "log_dir": str(tmp_path / "log"),
        }
    )
    plan = plugin.build_plan(config)

    script = NextflowExporter().export(plan, config, plugin.registry())

    assert "nextflow.enable.dsl=2" in script
    assert "process RNA1_QC_FASTP" in script
    assert "process RNA1_ALIGNMENT_STAR" in script
    assert "process RNA1_EXPRESSION_FEATURECOUNTS" in script
    assert "fastp -i" in script
    assert "/examples/fixtures/tiny_R1.fastq" in script
    assert "STAR --runThreadN 4" in script
    assert "featureCounts -T 4" in script
    assert "workflow {" in script
    assert "abi_root = Channel.value('abi_nextflow_start')" in script
    assert "ch_RNA1_QC_FASTP = RNA1_QC_FASTP(abi_root)" in script
    assert "ch_RNA1_ALIGNMENT_STAR = RNA1_ALIGNMENT_STAR(ch_RNA1_QC_FASTP)" in script
    assert (
        "ch_RNA1_EXPRESSION_FEATURECOUNTS = RNA1_EXPRESSION_FEATURECOUNTS(ch_RNA1_ALIGNMENT_STAR)"
    ) in script


def test_nextflow_exporter_smoke_mode_does_not_call_real_tools(tmp_path):
    plugin = get_plugin("metatranscriptomics")
    config = plugin.load_config(overrides={"outdir": str(tmp_path / "results")})
    plan = plugin.build_plan(config)

    script = NextflowExporter().export(plan, config, plugin.registry(), smoke=True)

    assert "// Export mode: smoke" in script
    assert "ABI Nextflow smoke step" in script
    assert "fastp -i" not in script
    assert "STAR --runThreadN" not in script
    assert "featureCounts -T" not in script


def test_nextflow_exporter_writes_script(tmp_path):
    plugin = get_plugin("metatranscriptomics")
    config = plugin.load_config(overrides={"outdir": str(tmp_path / "results")})
    plan = plugin.build_plan(config)
    output = tmp_path / "workflow.nf"

    path = NextflowExporter().write(plan, config, plugin.registry(), output)

    assert path == output
    assert output.exists()
    assert output.read_text(encoding="utf-8").startswith("// Generated by ABI NextflowExporter.")


def test_nextflow_exporter_serializes_metagenomic_plasmid_steps_without_path_edges(tmp_path):
    plugin = get_plugin("metagenomic_plasmid")
    config = plugin.load_config(
        "examples/config_minimal.yaml",
        overrides={"outdir": str(tmp_path / "plasmid"), "log_dir": str(tmp_path / "log")},
    )
    plan = plugin.build_plan(config)

    script = NextflowExporter().export(plan, config, plugin.registry())

    # DAG-driven planner generates steps from pipeline_dag.yaml.
    # FastQC is optional — not generated unless explicitly enabled.
    # Verify the core pipeline structure is present.
    assert "process S1_QC_FASTP" in script
    assert "process S1_ASSEMBLY_MEGAHIT" in script
    assert "process S1_ASSEMBLY_QC_QUAST" in script
    assert "process S1_PLASMID_DETECT_GENOMAD" in script
    # Verify channel wiring exists for key steps.
    # The DAG-driven planner inserts host_prediction before fastp,
    # so fastp channels depend on the upstream host_prediction process.
    assert "ch_S1_QC_FASTP" in script
    assert "= S1_QC_FASTP(" in script
    # The DAG should export without errors
    assert "workflow {" in script
