#!/usr/bin/env python3

# Copyright (C) 2021-2025 IGM authors
# Published under the GNU GPL (Version 3), check at the LICENSE file

from omegaconf import DictConfig

from igm.common import State

from .utils import compute_pmp_tf, compute_T_tf, compute_omega_tf


def compute_temperature(cfg: DictConfig, state: State) -> None:
    """
    Compute temperature and water content fields from enthalpy.

    Converts the enthalpy field to temperature and water content using
    the pressure melting point as the threshold between cold and temperate ice.

    Updates state.T (K), state.omega (-), state.T_b (K), and state.T_s (K).
    """
    cfg_thermal = cfg.processes.enthalpy.thermal

    c_ice = cfg_thermal.c_ice
    L_ice = cfg_thermal.L_ice
    T_ref = cfg_thermal.T_ref

    state.T = compute_T_tf(state.E, state.E_pmp, state.T_pmp, T_ref, c_ice)
    state.omega = compute_omega_tf(state.E, state.E_pmp, L_ice)

    state.T_b = state.T[0]
    state.T_s = state.T[-1]


def compute_pmp(cfg: DictConfig, state: State) -> None:
    """
    Compute the pressure melting point temperature and enthalpy fields.

    Calculates the pressure-dependent melting point throughout the ice column
    using the Clausius-Clapeyron relation.

    Updates state.T_pmp (K) and state.E_pmp (J kg^-1).
    """
    cfg_physics = cfg.processes.iceflow.physics
    cfg_thermal = cfg.processes.enthalpy.thermal

    rho_ice = cfg_physics.ice_density
    g = cfg_physics.gravity_cst

    beta = cfg_thermal.beta
    c_ice = cfg_thermal.c_ice
    T_pmp_ref = cfg_thermal.T_pmp_ref
    T_ref = cfg_thermal.T_ref

    depth_ice = state.iceflow.discr_v.enthalpy.depth * state.thk[None, ...]

    state.T_pmp, state.E_pmp = compute_pmp_tf(
        rho_ice, g, depth_ice, beta, c_ice, T_pmp_ref, T_ref
    )
