import sys
import os

# This gets the directory where the Snakefile (and your code) is located.
BASE_DIR = Path(workflow.basedir).parent

if sys.platform.startswith("linux"):
    wepp_env_file = str(BASE_DIR / "workflow/envs/wepp_linux.yml")
elif sys.platform == "darwin":
    wepp_env_file = str(BASE_DIR / "workflow/envs/wepp_osx.yml")
else:
    raise OSError("Unsupported operating system")

# Load default config from ./config/config.yaml
configfile: str(BASE_DIR / "config/config.yaml")

include: str(BASE_DIR / "workflow/rules/qc.smk")
include: str(BASE_DIR / "workflow/rules/freyja.smk")
include: str(BASE_DIR / "workflow/rules/preprocess.smk")
include: str(BASE_DIR / "workflow/rules/sam2pb.smk")
include: str(BASE_DIR / "workflow/rules/filter.smk")
include: str(BASE_DIR / "workflow/rules/dashboard.smk")

DIR = config.get("DIR")
FILE_PREFIX = config.get("FILE_PREFIX")

# Extract rules from command-line arguments
requested_rules = set()
for arg in sys.argv[1:]:
    if not arg.startswith("-"):
        requested_rules.add(arg)

if "help" not in requested_rules:
    if not DIR or not FILE_PREFIX:
        raise ValueError("Please provide DIR and FILE_PREFIX in --config argument")
    
    print(f"""\n\n
            DIR={DIR}, FILE_PREFIX={FILE_PREFIX}
            TREE={config.get("TREE")}
            REF={config.get("REF")}
            SEQUENCING_TYPE={config.get("SEQUENCING_TYPE")}
            PRIMER_BED={config.get("PRIMER_BED")}
            MIN_AF={config.get("MIN_AF")}
            MIN_DEPTH={config.get("MIN_DEPTH")}
            MIN_Q={config.get("MIN_Q")}
            MIN_PROP={config.get("MIN_PROP")}
            MIN_LEN={config.get("MIN_LEN")}
            MAX_READS={config.get("MAX_READS")}
            CLADE_LIST={config.get("CLADE_LIST")}
            CLADE_IDX={config.get("CLADE_IDX")}
            DASHBOARD_ENABLED={config.get("DASHBOARD_ENABLED")}
            TAXONIUM_FILE={config.get("TAXONIUM_FILE")}
        \n\n""")

rule all:
    input:
        f"results/{DIR}/{FILE_PREFIX}_run.txt"

rule help:
    input:
        str(BASE_DIR /"build/wepp")
    shell:
        "{input} help"