# coding=utf-8
#
# Copyright 2021-2022 Romain BESSEAU <romain.besseau@ec.europa.eu>
# Copyright 2022-2024 Alejandra CUE GONZALEZ <alejandra.cue_gonzalez@minesparis.psl.eu>
# Copyright 2022-2024 Benoît GSCHWIND <benoit.gschwind@minesparis.psl.eu>
# Copyright 2022-2024 MINES Paris
#
# This file is part of "parasol-lca" and you can used under the
# term of European Union Public Licence.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# European Union Public Licence for more details.
#
# Version: February 2026
#
# Code authors: Romain BESSEAU, Benoît GSCHWIND, Alejandra CUE GONZALEZ
#
import lca_algebraic as agb

# Electricity dataset

# Switch parameter for electricity mix
def _ensure_manufacturing_electricity_mix(context):
    return agb.newEnumParam(
    dbname = context.target_database,
    name = 'manufacturing_electricity_mix',
    values = ['FR', 'EU', 'US', 'CN','World', 'PV', 'Nuclear','Coal', 'CO2_content'],
    default = 'CN',
    description = "Switch on electricty mix used for PV manufacture",
    group = 'PV manufacture')

def _ensure_electricity_mix_CO2_content(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "electricity_mix_CO2_content",
    default = 0.5, min = 0, max = 1,
    distrib = agb.params.DistributionType.TRIANGLE,
    group = "PV manufacture",
    unit = "ratio")

# Mounting System

# Define parameters for the mounting system
def _ensure_roof_vs_ground_ratio(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "roof_vs_ground_ratio",
    default = 1.0, min = 0.0, max = 1.0,
    distrib = agb.params.DistributionType.LINEAR,
    group = "PV installation",
    description = "Proportion of rooftop installations",
    unit = 'ratio')

def _ensure_mounting_system_weight_total(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "mounting_system_weight_total",
    default = 5, min = 2, max = 11.5,
    distrib = agb.params.DistributionType.TRIANGLE,
    group = "PV mounting",
    unit = "kg/(m**2)")

def _ensure_mounting_system_weight_alu(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "mounting_system_weight_alu",
    default = 1.5, min = 0.67, max = 2.4,
    distrib = agb.params.DistributionType.TRIANGLE,
    group = "PV mounting",
    unit = "kg/(m**2)")

def _ensure_mounting_system_weight_wood(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "mounting_system_weight_wood",
    default = 0, min = 0, max = 10,
    distrib = agb.params.DistributionType.TRIANGLE,
    group = "PV mounting",
    unit = "kg/(m**2)")

def _ensure_ground_coverage_ratio(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = 'ground_coverage_ratio',
    default = 0.4, min = 0.2, max = 0.5,
    distrib = agb.params.DistributionType.TRIANGLE,
    group = "PV installation",
    unit = "ratio",
    description = "Ground coverage ratio")

#XXX Modular mounting system
def _ensure_mounting_system_type(context):
    return agb.newEnumParam(
    dbname = context.target_database,
    name = 'mounting_system_type',
    values = ['ground','vertical_ground','floating_lake', 'vertical_building'],
    default = 'ground',
    description = "Switch on the mounting type",
    group = 'PV manufacture')

# PV plant lifetime
def _ensure_power_plant_lifetime(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = 'power_plant_lifetime',
    default = 30, min = 20, max = 40,
    distrib = agb.params.DistributionType.TRIANGLE,
    group = "PV operation",
    unit = "year",
    description = "Lifetime of the PV plant")

# Electrical installation
def _ensure_power_plant_installed_capacity(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "power_plant_installed_capacity",
    default = 100,
    min = 3, max = 10000,
    group = "PV install", 
    unit = "kilowatt")

def _ensure_recycling_rate(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "recycling_rate",
    default=0.9, min=0, max=1,
    group = "PV recycle",
    unit="fraction")

# Silicon production

#parameter to adjust the amount of electricity for silicon manufacturing
def _ensure_silicon_production_electricity_intensity(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "silicon_production_electricity_intensity",
    default = 30, min = 11, max = 180,
    distrib = agb.DistributionType.TRIANGLE,
    group = "PV silicon manufacture",
    unit = "kWh/kg")

def _ensure_silicon_production_heat_intensity(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "silicon_production_heat_intensity",
    default = 185, min = 0, max = 185,
    distrib = agb.DistributionType.TRIANGLE,
    group = "PV silicon manufacture",
    unit = "megajoule/kg")

def _ensure_silicon_casting_electricity_intensity(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "silicon_casting_electricity_intensity",
    default = 15, min = 10, max = 30,
    distrib = agb.DistributionType.TRIANGLE,
    group = "PV silicon manufacture",
    unit = "kWh/kg")

## Wafer

# Parameters
def _ensure_diamond_wiring(context):
    return agb.newBoolParam(
    dbname = context.target_database,
    name = "diamond_wiring",
    default = 1,
    distrib = agb.DistributionType.LINEAR,
    group = "PV wafer manufacture")

def _ensure_wafer_thickness(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "wafer_thickness",
    default=160, min=128, max=190,
    distrib = agb.DistributionType.TRIANGLE,
    group="PV wafer manufacture",
    unit="micrometer")

def _ensure_kerf_loss(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "kerf_loss",
    default = 0.3, min=0.3, max=0.50,
    distrib = agb.DistributionType.TRIANGLE,
    group="PV wafer manufacture",
    unit="ratio")

def _ensure_sic_recycled_share(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "sic_recycled_share",
    default = 0.0, min=0.0, max=0.9,
    distrib = agb.DistributionType.TRIANGLE,
    group="PV wafer install",
    unit="ratio")

def _ensure_manufacturing_efficiency_gains(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "manufacturing_efficiency_gains",
    default = 0, min=0, max=1,
    distrib = agb.DistributionType.TRIANGLE,
    group="PV wafer manufacture",
    description="Manufacturing efficiency gain (elec for wafer)",
    unit="fraction")

# PV Cell manufacturing

def _ensure_silver_content(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "silver_content",
    default = 9.6, min=2, max=9.6,
    distrib = agb.DistributionType.TRIANGLE,
    group="PV cell manufacture",
    description="Silver amount for PV cell", 
    unit="g/(m**2)")

# PV Panel
def _ensure_aluminium_frame_surfacic_weight(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "aluminium_frame_surfacic_weight",
    default = 1.5, min=0, max=2.63,
    distrib = agb.DistributionType.TRIANGLE,
    group="PV panel manufacture",
    unit="kg/(m**2)")

#TODO: is this parameter pertinent? To rethink in the context of ICV-PV
# if we have a switch between PV panels perhaps it is no longer pertinent
def _ensure_has_bifacial_modules(context):
    return agb.newBoolParam(
    dbname = context.target_database,
    name = "has_bifacial_modules",
    default = False,
    group="PV panel manufacture")

def _ensure_recycling_rate_Al(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "recycling_rate_Al",
    default = 0.96, min=0.56, max=1,
    distrib = agb.DistributionType.TRIANGLE,
    group="PV panel manufacture",
    unit="ratio")

def _ensure_recycling_rate_Cu(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "recycling_rate_Cu",
    default = 0.75, min=0.44, max=0.96,
    distrib = agb.DistributionType.TRIANGLE,
    group="PV panel manufacture",
    unit="ratio")

def _ensure_recycling_rate_glass(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "recycling_rate_glass",
    default = 0.9, min=0.6, max=1,
    distrib = agb.DistributionType.TRIANGLE,
    group = "PV panel manufacture",
    unit="ratio")

def _ensure_electricity_consumption_for_recycling(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "electricity_consumption_for_recycling",
    default = 50, min=0, max=250,
    distrib = agb.DistributionType.TRIANGLE,
    group = "PV panel manufacture",
    description="elec used for recycling", 
    unit="kWh/t") # kWh per tonne

def _ensure_heat_consumption_for_recycling(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "heat_consumption_for_recycling",
    default = 76, min=0, max=150,
    distrib = agb.DistributionType.TRIANGLE,
    group = "PV panel manufacture",
    description="Heat for recycling", 
    unit="megajoule/t") # MJ per tonne

def _ensure_glass_thickness(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "glass_thickness",
    default = 4, min=2, max=4,
    distrib = agb.DistributionType.TRIANGLE,
    group="PV panel manufacture",
    unit = "millimeter")

# TODO update switch, review mono-Si hypotheses
def _ensure_module_pv_technology(context):
    return agb.newEnumParam(
    dbname = context.target_database,
    name = 'module_pv_technology',
    values = ['multiSi', 'polySi', 'monoSi', 'singleSi'],
    default = 'multiSi',
    description = "Switch on PV technologies",
    group = 'PV panel manufacture')

# PV System
def _ensure_pv_module_efficiency(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "pv_module_efficiency",
    default=0.22, min=0.15, max=0.23,
    distrib = agb.DistributionType.TRIANGLE,
    group = "PV panel manufacture",
    unit="kilowatt/(m**2)")

# Ratio of installed capacity of inverter relative to pv installed capacity.
# This ratio is often below 1.0 because the full production is never reached.
def _ensure_inverter_installed_capacity_ratio(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "inverter_installed_capacity_ratio",
    group = "PV inverter",
    default=1.0, min=0.7, max=1.0,
    distrib = agb.DistributionType.LINEAR,
    unit="dimensionless")

# Selected inverter capacity
#FIXME : parameter not used in units. need to fix interpolation
def _ensure_inverter_power_capacity(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "inverter_power_capacity",
    group = "PV inverter",
    default=500.0, min=2.5, max=500.0,
    distrib = agb.DistributionType.LINEAR,
    unit="kW",#kilowatt
    description= "inverter power capacity in kW")

def _ensure_inverter_lifetime(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "inverter_lifetime",
    group = "PV inverter",
    default=15, min=10, max=30,
    distrib = agb.DistributionType.TRIANGLE,
    unit="year")

def _ensure_inverter_weight_per_kW(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "inverter_weight_per_kW",
    default=2, min=1, max=6,
    group = 'PV inverter',
    label_fr="poids onduleur",
    unit="kg/kilowatt")

def _ensure_transport_distance_lorry(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "transport_distance_lorry",
    default = 1000, min=40, max=2000,
    unit="km",
    group="PV transport")

def _ensure_transport_distance_train(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "transport_distance_train",
    default=500, min=100, max=600,
    unit="km",
    group="PV transport")

def _ensure_transport_distance_boat(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "transport_distance_boat",
    default=5000, min=2000, max=6000,
    unit="km",
    group="PV transport")

def _ensure_electrical_installation_specific_weight(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = "electrical_installation_specific_weight",
    default = 3, min=2.15, max=4.6,
    unit="kg/kilowatt",
    group="PV electric installation")

def _ensure_normalised_annual_PV_production_kWh_per_kWp(context):
    return agb.newFloatParam(
    dbname = context.target_database,
    name = 'normalised_annual_PV_production_kWh_per_kWp',
    default = 1300, min = 900, max = 1700,
    distrib = agb.params.DistributionType.TRIANGLE,
    group = "PV operation",
    unit = "kWh/kilowatt/year",
    description = "Durée de vie")
