#ifndef METER_MANAGER_H
#define METER_MANAGER_H

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

/**
 * @brief Supported meter types for EVSE and Grid.
 */
typedef enum {
    METER_TYPE_NONE,           // No meter
    METER_TYPE_ADE7758,        // ADE7758 meter
    METER_TYPE_ORNO513,        // ORNO-513
    METER_TYPE_ORNO516,        // ORNO-516
    METER_TYPE_ORNO526,        // ORNO-516
    METER_TYPE_DDS661,         // DDS-661
    METER_TYPE_DTS6619,        // dts6619
    METER_TYPE_MONO_ZIGBEE,    // Zigbee single-phase
    METER_TYPE_TRIF_ZIGBEE,     // Zigbee three-phase
    METER_TYPE_EA777,          // EA777
    METER_TYPE_DTS024M,
} meter_type_t;

/**
 * @brief Initializes the meter manager system.
 *
 * Registers network event handlers and initializes both EVSE and GRID meters.
 */
esp_err_t meter_manager_init(void);


/**
 * @brief Starts all configured meters (EVSE and GRID).
 * 
 * @return esp_err_t ESP_OK on success, or an error code from one of the start calls.
 */
esp_err_t meter_manager_start(void);


/**
 * @brief Stops all meters and unregisters network event handlers.
 */
esp_err_t meter_manager_stop(void);

/**
 * @brief EVSE Meter Management
 */

/**
 * @brief Initializes the EVSE meter based on configured type.
 */
esp_err_t meter_manager_evse_init(void);

/**
 * @brief Starts the EVSE meter.
 */
esp_err_t meter_manager_evse_start(void);

/**
 * @brief Stops the EVSE meter.
 */
esp_err_t meter_manager_evse_stop(void);

/**
 * @brief Returns true if an EVSE meter is configured (not NONE).
 */
bool meter_manager_evse_is_enabled(void);

/**
 * @brief Sets the EVSE meter type and saves it to NVS.
 */
esp_err_t meter_manager_evse_set_model(meter_type_t meter_type);

/**
 * @brief Gets the current EVSE meter type.
 */
meter_type_t meter_manager_evse_get_model(void);

/**
 * @brief Grid Meter Management
 */

/**
 * @brief Initializes the Grid meter based on configured type.
 */
esp_err_t meter_manager_grid_init(void);

/**
 * @brief Starts the Grid meter.
 */
esp_err_t meter_manager_grid_start(void);

/**
 * @brief Stops the Grid meter.
 */
esp_err_t meter_manager_grid_stop(void);

/**
 * @brief Sets the Grid meter type and saves it to NVS.
 */
esp_err_t meter_manager_grid_set_model(meter_type_t meter_type);

/**
 * @brief Gets the current Grid meter type.
 */
meter_type_t meter_manager_grid_get_model(void);

/**
 * @brief Utility functions
 */

/**
 * @brief Converts a meter_type_t to a human-readable string.
 */
const char* meter_type_to_str(meter_type_t type);

/**
 * @brief Converts a string to a meter_type_t.
 */
meter_type_t string_to_meter_type(const char *str);

#endif // METER_MANAGER_H
