"""
Real workflow test for GrapheonRL plugin.

DAG structure (mirrors a bioinformatics-style pipeline):

  preprocess_A ─┐
  preprocess_B ─┤─► align_A ─┐
                │             ├─► merge ─► postprocess ─► report
  preprocess_C ─┘  align_B ─┘
                    align_C ─┘

With additional quality-control jobs running in parallel:
  qc_A, qc_B, qc_C  (parallel, each depends on respective preprocess)
  qc_merge           (depends on all qc_* jobs)

Total: 12 jobs with realistic heterogeneous durations.
Each rule has a benchmark: directive so the plugin can calibrate
from actual wall-clock measurements.

Resource requests deliberately chosen so scheduling order matters:
- align rules need 4 threads (only 2 can run simultaneously on 8 cores)
- preprocess rules need 2 threads
- qc rules need 1 thread
"""

SAMPLES = ["A", "B", "C"]

rule all:
    input:
        "results/report.txt",
        "results/qc_report.txt"

# ── Preprocessing (2 threads, ~2s each) ──────────────────────────────────────

rule preprocess:
    output: "results/preprocessed_{sample}.txt"
    benchmark: "benchmarks/preprocess_{sample}.txt"
    threads: 2
    resources: mem_mb=2048, runtime=2
    shell:
        """
        sleep 2
        echo "preprocessed {wildcards.sample} at $(date +%s%N)" > {output}
        """

# ── Alignment (4 threads, ~4s each - critical path) ──────────────────────────

rule align:
    input: "results/preprocessed_{sample}.txt"
    output: "results/aligned_{sample}.txt"
    benchmark: "benchmarks/align_{sample}.txt"
    threads: 4
    resources: mem_mb=8192, runtime=4
    shell:
        """
        sleep 4
        wc -c {input} >> {output}
        echo "aligned {wildcards.sample} at $(date +%s%N)" >> {output}
        """

# ── Merge (2 threads, ~2s) ───────────────────────────────────────────────────

rule merge:
    input: expand("results/aligned_{sample}.txt", sample=SAMPLES)
    output: "results/merged.txt"
    benchmark: "benchmarks/merge.txt"
    threads: 2
    resources: mem_mb=4096, runtime=2
    shell:
        """
        sleep 2
        cat {input} > {output}
        echo "merged at $(date +%s%N)" >> {output}
        """

# ── Postprocess (2 threads, ~3s) ─────────────────────────────────────────────

rule postprocess:
    input: "results/merged.txt"
    output: "results/postprocessed.txt"
    benchmark: "benchmarks/postprocess.txt"
    threads: 2
    resources: mem_mb=4096, runtime=3
    shell:
        """
        sleep 3
        wc -l {input} > {output}
        echo "postprocessed at $(date +%s%N)" >> {output}
        """

# ── Report (1 thread, ~1s) ────────────────────────────────────────────────────

rule report:
    input: "results/postprocessed.txt"
    output: "results/report.txt"
    benchmark: "benchmarks/report.txt"
    threads: 1
    resources: mem_mb=512, runtime=1
    shell:
        """
        sleep 1
        echo "=== Final Report ===" > {output}
        cat {input} >> {output}
        echo "completed at $(date +%s%N)" >> {output}
        """

# ── QC branch (parallel to alignment, 1 thread, ~1s each) ───────────────────

rule qc:
    input: "results/preprocessed_{sample}.txt"
    output: "results/qc_{sample}.txt"
    benchmark: "benchmarks/qc_{sample}.txt"
    threads: 1
    resources: mem_mb=512, runtime=1
    shell:
        """
        sleep 1
        wc -c {input} > {output}
        echo "qc {wildcards.sample} at $(date +%s%N)" >> {output}
        """

# ── QC merge (1 thread, ~1s) ─────────────────────────────────────────────────

rule qc_merge:
    input: expand("results/qc_{sample}.txt", sample=SAMPLES)
    output: "results/qc_report.txt"
    benchmark: "benchmarks/qc_merge.txt"
    threads: 1
    resources: mem_mb=512, runtime=1
    shell:
        """
        sleep 1
        cat {input} > {output}
        echo "qc_merge at $(date +%s%N)" >> {output}
        """
