#ifndef EVSE_EVENTS_H
#define EVSE_EVENTS_H

#pragma once

#include <stdbool.h>
#include <stdint.h>
#include "esp_event.h"
#include "auth_types.h"

ESP_EVENT_DECLARE_BASE(EVSE_EVENTS);

typedef enum {
    EVSE_EVENT_INIT,
    EVSE_EVENT_STATE_CHANGED,
    EVSE_EVENT_CONFIG_UPDATED,
    EVSE_EVENT_ENABLE_UPDATED,
    EVSE_EVENT_AVAILABLE_UPDATED,
    EVSE_EVENT_SESSION,
    EVSE_EVENT_ERROR_CHANGED, 
} evse_event_id_t;

// -----------------
// Eventos de STATE
// -----------------
typedef enum {
    EVSE_STATE_EVENT_IDLE,
    EVSE_STATE_EVENT_WAITING,
    EVSE_STATE_EVENT_CHARGING,
    EVSE_STATE_EVENT_FAULT
} evse_state_event_t;

typedef struct {
    evse_state_event_t state;
} evse_state_event_data_t;

// -----------------
// Eventos de SESSÃO
// -----------------
typedef enum {
    EVSE_SESSION_EVENT_STARTED = 0,
    EVSE_SESSION_EVENT_FINISHED,
} evse_session_event_type_t;

typedef struct {
    evse_session_event_type_t type;   ///< STARTED / FINISHED

    uint32_t session_id;
    char     tag[AUTH_TAG_MAX_LEN];
    uint32_t duration_s;
    uint32_t energy_wh;
    uint32_t avg_power_w;

    bool     is_current;
} evse_session_event_data_t;

// -----------------
// Eventos de CONFIG
// -----------------
typedef struct {
    bool    charging;
    float   hw_max_current;
    float   runtime_current;
    int64_t timestamp_us;
} evse_config_event_data_t;

typedef struct {
    bool    enabled;
    int64_t timestamp_us;
} evse_enable_event_data_t;

typedef struct {
    bool    available;
    int64_t timestamp_us;
} evse_available_event_data_t;

// -----------------
// Eventos de ERRO
// -----------------
typedef struct {
    uint32_t error_bits;    ///< estado atual (todos os bits de erro)
    uint32_t changed_mask;  ///< bits que mudaram nesta notificação
    int64_t  timestamp_us;  ///< esp_timer_get_time()
} evse_error_event_data_t;

#endif // EVSE_EVENTS_H
