#ifndef GRID_LIMITER_H_
#define GRID_LIMITER_H_

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdbool.h>
#include <stdint.h>
#include "esp_err.h"
#include "meter_events.h"

    void grid_limiter_init(void);

    void grid_limiter_set_enabled(bool en);
    bool grid_limiter_is_enabled(void);

    esp_err_t grid_limiter_set_max_import_a(uint8_t a);
    uint8_t grid_limiter_get_max_import_a(void);

    /**
     * @brief Calcula um novo "total_budget_a" (<= current_total_a) para respeitar max_import_a.
     *
     * Preferência:
     *  - Usa watt_total (+import / -export) se existir
     *  - Caso watt_total==0, usa fallback_grid_current_a (magnitude)
     *
     * @param grid_evt                último evento do GRID
     * @param fallback_grid_current_a corrente filtrada (magnitude) como fallback
     * @param current_total_a         total atual a atribuir aos EVSE (A)
     * @return total_budget_a (<= current_total_a)
     */
    float grid_limiter_limit_total_a(const meter_event_data_t *grid_evt,
                                     float fallback_grid_current_a,
                                     float current_total_a);

#ifdef __cplusplus
}
#endif

#endif /* GRID_LIMITER_H_ */
