// meter_ea777.c — Driver Modbus RTU para EARU EA777 (ESP-IDF)

#include "meter_events.h"
#include "modbus_params.h"
#include "mbcontroller.h"
#include "esp_log.h"
#include "driver/uart.h"
#include <stddef.h>
#include <string.h>
#include "meter_ea777.h"

#define TAG "serial_mdb_ea777"

// ===== UART / RS-485 =====
#define MB_PORT_NUM 1
#define MB_DEV_SPEED 9600

// Ajuste os pinos conforme seu hardware (evite GPIO2 para RTS/DE/RE se possível)
#define MB_UART_TXD 17
#define MB_UART_RXD 16
#define MB_UART_RTS 2 // pino DE/RE do transceiver RS-485

// ===== Timings =====
#define UPDATE_INTERVAL (5000 / portTICK_PERIOD_MS)
#define POLL_INTERVAL (200 / portTICK_PERIOD_MS)

// ===== Helpers =====
#define STR(fieldname) ((const char *)(fieldname))
#define OPTS(min_val, max_val, step_val) {.opt1 = min_val, .opt2 = max_val, .opt3 = step_val}

// ===== Estado =====
static bool is_initialized = false;
static TaskHandle_t meter_task = NULL;

// ============================================================================
// ============ MAPA DE REGISTROS EA777 (Holding 0x03) ========================
// Endereços zero-based. Tipos reais (engenharia) via fator de escala.
// Tensões (0.1 V)
#define EA777_L1VOLTAGE 0x0000
#define EA777_L2VOLTAGE 0x0001
#define EA777_L3VOLTAGE 0x0002
// Correntes (0.01 A)
#define EA777_L1CURRENT 0x0003
#define EA777_L2CURRENT 0x0004
#define EA777_L3CURRENT 0x0005
// Potência ativa total (W)
#define EA777_TOTAL_ACTIVE_P 0x0007
// (se quiser por fase, pode usar 0x0008/0x0009/0x000A)
// Fator de potência por fase (0.001)
#define EA777_PF_L1 0x0014
#define EA777_PF_L2 0x0015
#define EA777_PF_L3 0x0016
// Frequência (0.01 Hz)
#define EA777_FREQUENCY 0x001A
// Energia ativa total (U32 * 0.01 kWh, 2 registradores)
#define EA777_TOTAL_ACTIVE_E 0x001D
// ============================================================================

// ============ CIDs ============
enum
{
    CID_EA777_L1_VOLTAGE = 0,
    CID_EA777_L2_VOLTAGE,
    CID_EA777_L3_VOLTAGE,
    CID_EA777_L1_CURRENT,
    CID_EA777_L2_CURRENT,
    CID_EA777_L3_CURRENT,
    CID_EA777_TOTAL_ACTIVE_P,
    CID_EA777_PF_L1,
    CID_EA777_PF_L2,
    CID_EA777_PF_L3,
    CID_EA777_FREQUENCY,
    CID_EA777_TOTAL_ACTIVE_E,
};

// ======= Descritores (Holding registers) =======
// Nota: param_offset = 0 -> não usamos holding_reg_params_t aqui.
const mb_parameter_descriptor_t device_parameters_ea777[] = {
    // Tensões (0.1 V)
    {CID_EA777_L1_VOLTAGE, STR("L1 Voltage"), STR("V"), 1,
     MB_PARAM_HOLDING, EA777_L1VOLTAGE, 1,
     0, PARAM_TYPE_U16, 2, OPTS(0, 4000, 1), PAR_PERMS_READ},

    {CID_EA777_L2_VOLTAGE, STR("L2 Voltage"), STR("V"), 1,
     MB_PARAM_HOLDING, EA777_L2VOLTAGE, 1,
     0, PARAM_TYPE_U16, 2, OPTS(0, 4000, 1), PAR_PERMS_READ},

    {CID_EA777_L3_VOLTAGE, STR("L3 Voltage"), STR("V"), 1,
     MB_PARAM_HOLDING, EA777_L3VOLTAGE, 1,
     0, PARAM_TYPE_U16, 2, OPTS(0, 4000, 1), PAR_PERMS_READ},

    // Correntes (0.01 A)
    {CID_EA777_L1_CURRENT, STR("L1 Current"), STR("A"), 1,
     MB_PARAM_HOLDING, EA777_L1CURRENT, 1,
     0, PARAM_TYPE_U16, 2, OPTS(0, 10000, 1), PAR_PERMS_READ},

    {CID_EA777_L2_CURRENT, STR("L2 Current"), STR("A"), 1,
     MB_PARAM_HOLDING, EA777_L2CURRENT, 1,
     0, PARAM_TYPE_U16, 2, OPTS(0, 10000, 1), PAR_PERMS_READ},

    {CID_EA777_L3_CURRENT, STR("L3 Current"), STR("A"), 1,
     MB_PARAM_HOLDING, EA777_L3CURRENT, 1,
     0, PARAM_TYPE_U16, 2, OPTS(0, 10000, 1), PAR_PERMS_READ},

    // Potência ativa total (W)
    {CID_EA777_TOTAL_ACTIVE_P, STR("Total Active Power"), STR("W"), 1,
     MB_PARAM_HOLDING, EA777_TOTAL_ACTIVE_P, 1,
     0, PARAM_TYPE_U16, 2, OPTS(0, 60000, 1), PAR_PERMS_READ},

    // Fator de potência (0.001)
    {CID_EA777_PF_L1, STR("L1 PF"), STR(""), 1,
     MB_PARAM_HOLDING, EA777_PF_L1, 1,
     0, PARAM_TYPE_U16, 2, OPTS(0, 1000, 1), PAR_PERMS_READ},

    {CID_EA777_PF_L2, STR("L2 PF"), STR(""), 1,
     MB_PARAM_HOLDING, EA777_PF_L2, 1,
     0, PARAM_TYPE_U16, 2, OPTS(0, 1000, 1), PAR_PERMS_READ},

    {CID_EA777_PF_L3, STR("L3 PF"), STR(""), 1,
     MB_PARAM_HOLDING, EA777_PF_L3, 1,
     0, PARAM_TYPE_U16, 2, OPTS(0, 1000, 1), PAR_PERMS_READ},

    // Frequência (0.01 Hz)
    {CID_EA777_FREQUENCY, STR("Frequency"), STR("Hz"), 1,
     MB_PARAM_HOLDING, EA777_FREQUENCY, 1,
     0, PARAM_TYPE_U16, 2, OPTS(0, 10000, 1), PAR_PERMS_READ},

    // Energia ativa total (U32 * 0.01 kWh, 2 regs)
    {CID_EA777_TOTAL_ACTIVE_E, STR("Total Active Energy"), STR("kWh"), 1,
     MB_PARAM_HOLDING, EA777_TOTAL_ACTIVE_E, 2,
     0, PARAM_TYPE_U32, 4, OPTS(0, 0xFFFFFFFF, 1), PAR_PERMS_READ},
};

const uint16_t num_device_parameters_ea777 =
    sizeof(device_parameters_ea777) / sizeof(device_parameters_ea777[0]);

// ===== Post do evento de medição =====
static void meter_ea777_post_event(float *voltage, float *current, int *power_w,
                                   float freq_hz, float pf_avg, float total_kwh)
{
    meter_event_data_t evt = {
        .source = "GRID",
        .frequency = freq_hz,
        .power_factor = pf_avg,
        .total_energy = total_kwh};

    memcpy(evt.vrms, voltage, sizeof(evt.vrms));
    memcpy(evt.irms, current, sizeof(evt.irms));
    memcpy(evt.watt, power_w, sizeof(evt.watt));

    esp_err_t err = esp_event_post(METER_EVENT, METER_EVENT_DATA_READY,
                                   &evt, sizeof(evt), portMAX_DELAY);
    if (err != ESP_OK)
    {
        ESP_LOGW(TAG, "Falha ao emitir evento: %s", esp_err_to_name(err));
    }
}

// ===== Task de polling =====
static void serial_mdb_ea777_task(void *param)
{
    esp_err_t err;
    const mb_parameter_descriptor_t *desc = NULL;

    float v[3] = {0};
    float i[3] = {0};
    float pf[3] = {0};
    float freq = 0.0f;
    float total_kwh = 0.0f;

    // pequeno settle antes da 1ª leitura
    vTaskDelay(pdMS_TO_TICKS(200));

    while (1)
    {
        for (uint16_t cid = 0; cid < num_device_parameters_ea777; cid++)
        {
            err = mbc_master_get_cid_info(cid, &desc);
            if (err != ESP_OK || !desc)
            {
                continue;
            }

            uint8_t type = 0;
            uint16_t raw16 = 0;
            uint32_t raw32 = 0;

            void *value_ptr = (cid == CID_EA777_TOTAL_ACTIVE_E) ? (void *)&raw32 : (void *)&raw16;

            // 1 retry simples em caso de timeout
            err = mbc_master_get_parameter(cid,
                                           (char *)desc->param_key,
                                           (uint8_t *)value_ptr,
                                           &type);
            if (err == ESP_ERR_TIMEOUT)
            {
                vTaskDelay(pdMS_TO_TICKS(60));
                err = mbc_master_get_parameter(cid,
                                               (char *)desc->param_key,
                                               (uint8_t *)value_ptr,
                                               &type);
            }

            if (err == ESP_OK)
            {
                switch (cid)
                {
                case CID_EA777_L1_VOLTAGE:
                    v[0] = ((float)raw16) * 0.1f;
                    break;
                case CID_EA777_L2_VOLTAGE:
                    v[1] = ((float)raw16) * 0.1f;
                    break;
                case CID_EA777_L3_VOLTAGE:
                    v[2] = ((float)raw16) * 0.1f;
                    break;

                case CID_EA777_L1_CURRENT:
                    i[0] = ((float)raw16) * 0.01f;
                    break;
                case CID_EA777_L2_CURRENT:
                    i[1] = ((float)raw16) * 0.01f;
                    break;
                case CID_EA777_L3_CURRENT:
                    i[2] = ((float)raw16) * 0.01f;
                    break;

                case CID_EA777_TOTAL_ACTIVE_P:
                    // guarda se quiser usar em debug; para o evento usamos
                    // aproximação por fase abaixo
                    // (poderia ser passado direto em power_w[0..2] também)
                    break;

                case CID_EA777_PF_L1:
                    pf[0] = ((float)raw16) * 0.001f;
                    break;
                case CID_EA777_PF_L2:
                    pf[1] = ((float)raw16) * 0.001f;
                    break;
                case CID_EA777_PF_L3:
                    pf[2] = ((float)raw16) * 0.001f;
                    break;

                case CID_EA777_FREQUENCY:
                    freq = ((float)raw16) * 0.01f;
                    break;

                case CID_EA777_TOTAL_ACTIVE_E:
                    total_kwh = ((float)raw32) * 0.01f;
                    break;

                default:
                    break;
                }

                ESP_LOGD(TAG, "%s (cid=%u) -> raw16=%u raw32=%u",
                         desc->param_key, cid,
                         (unsigned int)raw16,
                         (unsigned int)raw32);
            }
            else
            {
                ESP_LOGE(TAG, "CID %u (%s) read failed: %s",
                         cid, desc->param_key, esp_err_to_name(err));
            }

            vTaskDelay(POLL_INTERVAL);
        }

        // Potência por fase aproximada: P = V * I * PF
        int p_int[3] = {
            (int)(v[0] * i[0] * pf[0]),
            (int)(v[1] * i[1] * pf[1]),
            (int)(v[2] * i[2] * pf[2]),
        };

        // PF médio simples (ignora zeros)
        float pf_sum = 0.0f;
        int pf_cnt = 0;
        for (int k = 0; k < 3; ++k)
        {
            if (pf[k] != 0.0f)
            {
                pf_sum += pf[k];
                pf_cnt++;
            }
        }
        float pf_avg = (pf_cnt ? pf_sum / pf_cnt : 0.0f);

        meter_ea777_post_event(v, i, p_int, freq, pf_avg, total_kwh);
        vTaskDelay(UPDATE_INTERVAL);
    }
}

esp_err_t meter_ea777_init(void)
{
    if (is_initialized)
    {
        ESP_LOGW(TAG, "Already initialized");
        return ESP_ERR_INVALID_STATE;
    }

    if (uart_is_driver_installed(MB_PORT_NUM))
    {
        uart_driver_delete(MB_PORT_NUM);
        ESP_LOGI(TAG, "UART driver deleted");
    }

    (void)mbc_master_destroy();

    mb_communication_info_t comm = {
        .port = MB_PORT_NUM,
        .mode = MB_MODE_RTU,
        .baudrate = MB_DEV_SPEED,
        .parity = UART_PARITY_EVEN};

    void *handler = NULL;
    ESP_ERROR_CHECK(mbc_master_init(MB_PORT_SERIAL_MASTER, &handler));
    ESP_ERROR_CHECK(mbc_master_setup(&comm));

    // >>> RS-485 half duplex
    ESP_ERROR_CHECK(uart_set_pin(MB_PORT_NUM,
                                 MB_UART_TXD, MB_UART_RXD,
                                 MB_UART_RTS, UART_PIN_NO_CHANGE));

    ESP_ERROR_CHECK(mbc_master_start());
    ESP_ERROR_CHECK(uart_set_mode(MB_PORT_NUM, UART_MODE_RS485_HALF_DUPLEX));
    vTaskDelay(pdMS_TO_TICKS(50));

    ESP_ERROR_CHECK(mbc_master_set_descriptor(device_parameters_ea777,
                                              num_device_parameters_ea777));

    is_initialized = true;
    ESP_LOGI(TAG, "EA777 Modbus master initialized (9600 8E1, Holding Reg 0x03)");
    return ESP_OK;
}

esp_err_t meter_ea777_start(void)
{
    if (!is_initialized)
    {
        ESP_LOGE(TAG, "Not initialized");
        return ESP_ERR_INVALID_STATE;
    }
    if (meter_task == NULL)
    {
        xTaskCreate(serial_mdb_ea777_task,
                    "meter_ea777_task",
                    4096, NULL, 3, &meter_task);
        ESP_LOGI(TAG, "EA777 task started");
    }
    return ESP_OK;
}

void meter_ea777_stop(void)
{
    if (!is_initialized)
    {
        ESP_LOGW(TAG, "Not initialized, skipping stop");
        return;
    }

    if (meter_task)
    {
        vTaskDelete(meter_task);
        meter_task = NULL;
        ESP_LOGI(TAG, "EA777 task stopped");
    }

    (void)mbc_master_destroy();

    if (uart_is_driver_installed(MB_PORT_NUM))
    {
        uart_driver_delete(MB_PORT_NUM);
        ESP_LOGI(TAG, "UART driver deleted");
    }

    is_initialized = false;
    ESP_LOGI(TAG, "Meter EA777 cleaned up");
}
