# Copyright (c) 2026, Artelys (https://www.artelys.com)
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# SPDX-License-Identifier: MPL-2.0
#
#
# @author Léo Rossignol <leo.rossignol at artelys.com>
# @author Alexis Godefroy <alexis.godefroy at artelys.com>
#
import os
from emerge_res_integration_tool import config


# Default input and output data folder names
INPUT_DATA_FOLDER = "input"
OUTPUT_DATA_FOLDER = "output"
WORK_DIR_NAME = ".emerge_work"

# Default input file names for simulations
INPUT_NETWORK_FILE = "network.xiidm"
INPUT_MAPPING_FILE = "mapping.groovy"
INPUT_CONF_FILE = "conf.groovy"
INPUT_CONTINGENCIES_FILE = "contingencies.groovy"
INPUT_TIMESERIES = "timeseries.csv"

# Default output file names for simulations
OUTPUT_RESULTS_FILE = "results.csv"
OUTPUT_LOG_FILE = "logs"

# Temporary output file
TMP_NETWORK_FILE = "network_tmp.xiidm"
SAVED_NETWORK_FILE = "network_saved.xiidm"

# Relevant paths to be used by the tool
PROJECT_DATA_FOLDER = "project_data"

DATA_PATH = os.path.join(config.MOZAMBIQUE_DIR, "project_data")
BEST_RESULTS_PATH = "knitro_final_solution.csv"
INTEGRATION_EMERGE_FILE = "iterative_emerge_results.csv" 
OPTIM_TEST_FOLDER = "simple_optim_6_nodes_network_6_timesteps"
OPTIM_MOZAMBIQUE_FOLDER = "complex_mozambique_network_24_timesteps"
SENSITIVITY_TEST_FOLDER = "simple_sensitivity_analysis_6_nodes_6_timesteps"

SENSITIVITY_PARAMS_FILE = "knitro_battery_params_sweep_combined.csv"
SENSITIVITY_FINDIFF_FILE = "knitro_findiff_sweep_combined.csv"
SENSITIVITY_INITIAL_GUESS_FILE = "knitro_initial_guess_sweep_combined.csv"

TEMPLATE_MAPPING_FILE = "bat_mapping.groovy"
TEMPLATE_COSTS_FILE = "generator_costs.groovy"

# Generator types METRIX
BAT_TYPE = "BAT"
HYDRO_TYPE = "HYDRO"
SOLAR_TYPE = "SOLAR"
WIND_TYPE = "WIND"
OTHER_TYPE = "OTHER"

# All costs
ADEQUACY_BATTERY_DOWN = "bat_cost_down"
ADEQUACY_BATTERY_UP = "bat_cost_up"
ADEQUACY_HYDRO_DOWN = "hydro_cost_down"
ADEQUACY_HYDRO_UP = "hydro_cost_up"
ADEQUACY_SOLAR_DOWN = "solar_cost_down"
ADEQUACY_SOLAR_UP = "solar_cost_up"
ADEQUACY_THERMAL_DOWN = "others_cost_down"
ADEQUACY_THERMAL_UP = "others_cost_up"
ADEQUACY_WIND_DOWN = "wind_cost_down"
ADEQUACY_WIND_UP = "wind_cost_up"

REDISPATCHING_BATTERY_DOWN = "bat_redisp_cost_down"
REDISPATCHING_BATTERY_UP = "bat_redisp_cost_up"
REDISPATCHING_HYDRO_DOWN = "hydro_redisp_cost_down"
REDISPATCHING_HYDRO_UP = "hydro_redisp_cost_up"
REDISPATCHING_SOLAR_DOWN = "solar_redisp_cost_down"
REDISPATCHING_SOLAR_UP = "solar_cost_up"
REDISPATCHING_THERMAL_DOWN = "others_redisp_cost_down"
REDISPATCHING_THERMAL_UP = "others_redisp_cost_up"
REDISPATCHING_WIND_DOWN = "wind_redisp_cost_down"
REDISPATCHING_WIND_UP = "wind_cost_up"

BAT_COST_ADEQUACY_DOWN = 100_000
BAT_COST_ADEQUACY_UP = 100_000
BAT_COST_REDISPATCHING_DOWN = 200
BAT_COST_REDISPATCHING_UP = 50

# Battery settings
BAT_EMAX = 2000.0
BAT_PMAX = 500.0

# Outputs
OUTPUT_CUM_COST = "Total_cumulated_cost_optimised (e)"
OUTPUT_ADEQUACY_COST = "Adequacy_cost (e)"
OUTPUT_REDISP_COST = "Redispatching_cost (e)"
OUTPUT_INITIAL_COST = "Initial_system_cost (e)"
OUTPUT_CUM_SAVED_COST = "Total_cumulated_saved_cost (e)"
OUTPUT_CUM_CURTAILMENT = "Total_cumulated_avoided_curtailment (MW)"

# Knitro set up
CUSTOM_SETPOINTS = [
                -78.77309829,
                -166.4613231,
                40.66642273,
                -153.9822171,
                -44.52559887,
                -124.4650669,
                419.138102,
                -33.6685571,
                -3.650398123,
                14.56224767,
                69.85449485,
                -78.00341652,
                -459.7268942,
                -344.5948285,
                -276.8964582,
                -134.1603925,
                -80.41403708,
                -79.34888899,
                -201.316307,
                -52.32513786,
                -195.2468913,
                289.8047551,
                273.6134373,
                177.9761429,
            ]
CUSTOM_E0 = 1000

STEPSIZE_VALUES = [1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6]

# Integration with CIRCE
URL = "https://emerge.fcirce.es/run_osp"
SCENARIO_ID = "my_scenario_01"
NB_BATTERIES = 1
BATTERY_POWER = [
    10,
    10,
    10,
    10,
    10,
    10,
    10,
    10,
    10,
    10,
    10,
    10,
    10,
    10,
    10,
    10,
    10,
    10,
    10,
    10,
    10,
    10,
    10,
    10,
]