from snakemake.remote.HTTP import RemoteProvider as HTTPRemoteProvider
import sys

sys.path.append("./scripts")

from os.path import normpath, exists, isdir
from shutil import copyfile, move

HTTP = HTTPRemoteProvider()


configfile: "../config/config.yaml"

localrules: all

wildcard_constraints:
    country="[a-zA-Z]+",
    sweep="[a-zA-Z]+",

rule all:
    message: "Run entire analysis and compile report."
    input:
        "report/report.pdf",


rule plot_export:
    output:
        modelenergy = "../results/figures/modelenergy/export_comp.pdf",
    notebook:
        "notebooks/modelenergy.py.ipynb"

rule plot_threshold:
    output:
        threshold_diff = "../results/figures/rule/threshold_diff.pdf",
        threshold_diff_png = "../results/figures/rule/threshold_diff.png",
    notebook:
        "notebooks/threshold_diff.py.ipynb"

rule plot_export_quan:
    params:
        #sweep = ["expansion"] #["year", "rule", "expansion"]
    input:
        nz_goals = "../data/nz_goals/nz_goals.csv",
        edemand = "../data/owid/electricity-demand.csv",
        edemand_incr = "../resources/edemand_incr/edemand_incr.csv",
        reshares = "../data/owid/share-of-electricity-production-from-renewable-sources.csv",
        regrowth_data = "../resources/regrowth/renewable-growth.csv",
        electrolyzer_eff = "../resources/electrolyzer_eff.csv",
    output:
        # exportquan = "../results/figures/exportquan/export_quant_{sweep}.pdf",
        # exportquan_png = "../results/figures/exportquan/export_quant_{sweep}.png",
        exportquan_line = "../results/figures/exportquan/export_quant_line_{sweep}.pdf",
        exportquan_line_png = "../results/figures/exportquan/export_quant_line_{sweep}.png",
    notebook:
        "notebooks/export_quan.py.ipynb"

rule plot_export_quan_all:
    input:
        exportquan = expand("../results/figures/exportquan/export_quant_line_{sweep}.pdf", sweep=["rule", "expansion", "demand"]),

rule plot_reshares:
    input:
        reshares = "../data/owid/share-of-electricity-production-from-renewable-sources.csv",
    output:
        reshares = "../results/figures/reshares/nzl.pdf",
        reshares_png = "../results/figures/reshares/nzl.png",
    notebook:
        "notebooks/re_shares.py.ipynb"

rule get_demand_incr:
    input:
        edemand_raw = "../data/daalder2021/plot-data.csv",
    output:
        edemand_incr = "../resources/edemand_incr/edemand_incr.csv",
    notebook:
        "notebooks/dom-demand.py.ipynb"

rule plot_regrowth:
    input:
        reprod = "../data/owid/modern-renewable-prod.csv",
        mbie = "../data/mbie/electricity.xlsx", # Source: mbieelectricity2024
        edemand = "../data/owid/electricity-demand.csv",
    output:
        regrowth = "../results/figures/regrowth/nzl.pdf",
        regrowth_png = "../results/figures/regrowth/nzl.png",
        regrowth_data = "../resources/regrowth/renewable-growth.csv",
    notebook:
        "notebooks/re_growth.py.ipynb"

rule get_electrolyzer_eff:
    input:
        costs = expand("../resources/costs_{planning_horizons}.csv", planning_horizons=[2020,2025,2030,2035,2040,2045,2050]),
    output:
        electrolyzer_eff = "../resources/electrolyzer_eff.csv",
    notebook:
        "notebooks/electrolyzer_eff.py.ipynb"

rule retrieve_cost_data:
    input:
        HTTP.remote(
            f"raw.githubusercontent.com/PyPSA/technology-data/{config['costs']['version']}/outputs/costs"
            + "_{planning_horizons}.csv",
            keep_local=True,
        ),
    output:
        costs="../resources/costs_{planning_horizons}.csv",
    resources:
        mem_mb=5000,
    run:
        move(input[0], output[0])

rule retrieve_cost_data_all:
    input:
        costs = expand("../resources/costs_{planning_horizons}.csv", planning_horizons=[2020,2025,2030,2035,2040,2045,2050]),


rule prepare_sensitivity:
    input:
        network = "../results/nz_2035_lowfix_lowcagr/postnetworks/elec_s_10_ec_lc3.0_Co2L_3H_2035_0.071_BU_10export.nc",
    output:
        network = "../results/nz_2035_lowfix_lowcagr/sensitivity/prenetworks/elec_s_10_ec_lc3.0_CO2L_3H_2035_0.071_BU_10export_sens{cost_var}_tech{tech}.nc",
    script:
        "scripts/prepare_sensitivity.py"

rule solve_sensitivity:
    params:
        solving=config["solving"],
        # augmented_line_connection=config["augmented_line_connection"],
        limit_max_growth=config["limit_max_growth"],
    input:
        network = "../results/nz_2035_lowfix_lowcagr/sensitivity/prenetworks/elec_s_10_ec_lc3.0_CO2L_3H_2035_0.071_BU_10export_sens{cost_var}_tech{tech}.nc",
        overrides="../data/override_component_attrs",
    output:
        network = "../results/nz_2035_lowfix_lowcagr/sensitivity/postnetworks/elec_s_10_ec_lc3.0_CO2L_3H_2035_0.071_BU_10export_sens{cost_var}_tech{tech}.nc"
    log:
        solver=os.path.normpath(
            "logs/"
            + "solve_network/elec_s{cost_var}_{tech}_solver.log"
        ),
        python="logs/"
            + "solve_network/elec_s{cost_var}_{tech}_python.log",
    script:
        "scripts/solve_sensitivity.py"


rule solve_sensitivity_all:
    input:
        network = expand("../results/nz_2035_lowfix_lowcagr/sensitivity/postnetworks/elec_s_10_ec_lc3.0_CO2L_3H_2035_0.071_BU_10export_sens{cost_var}_tech{tech}.nc", cost_var=config["sensitivity"]["cost_var"], tech=config["sensitivity"]["techs"], allow_missing=True)



rule collect_figures:
    input:
        modelenergy = "../results/figures/modelenergy/export_comp.pdf",
        exportquan = expand("../results/figures/exportquan/export_quant_{sweep}.pdf", sweep=["year", "rule", "expansion"]),
        exportquan_line = expand("../results/figures/exportquan/export_quant_line_{sweep}.pdf", sweep=["rule", "expansion", "demand"]),
        reshares = "../results/figures/reshares/nzl.pdf",
        regrowth = "../results/figures/regrowth/nzl.pdf",
        threshold_diff = "../results/figures/rule/threshold_diff.pdf",
        prices = "../results/figures/prices/el-price-avg.pdf",



# rule report:
#     message: "Compile report."
#     input:
#         tex="report/report.tex",
#         bib="report/references.bib"
#     output: "report/report.pdf"
#     shell:
#         """
#         pdflatex {input.tex}
#         bibtex {input.bib})
#         pdflatex {input.tex}
#         pdflatex {input.tex}
#         """


# rule dag:
#      message: "Plot dependency graph of the workflow."
#      output:
#          dot="resources/dag.dot",
#          pdf="resources/dag.pdf"
#      shell:
#          """
#          snakemake --rulegraph > {output.dot}
#          dot -Tpdf -o {output.pdf} {output.dot}
#          """


# rule clean:
#     message: "Remove all build results but keep downloaded data."
#     run:
#          import shutil

#          shutil.rmtree("resources")
#          shutil.rmtree("results")
#          print("Data downloaded to data/ has not been cleaned.")

        
# rule sync:
#     params:
#         cluster=config["cluster"],
#     shell:
#         """
#         rsync -uvarh --no-g --exclude-from=.syncignore-send . {params.cluster}
#         rsync -uvarh --no-g --exclude-from=.syncignore-receive {params.cluster} .
#         """
