#pragma once

struct SystemState {
    // --- PV ---
    float pv        = 0.0f;   // Tension PV (V)
    float pvCurrent = 0.0f;   // Courant PV (A)

    // --- Batterie ---
    float   battery         = 0.0f;   // Tension (V)
    float   batTemperature  = 0.0f;   // Température (°C)
    uint8_t batSOC          = 0;      // Charge restante (%)
    uint8_t batStatut       = 0;      // 0=arrêt 1=float 2=boost 3=égalisation
    bool    batSousVoltage  = false;
    bool    batSurVoltage   = false;

    // --- Sortie de charge (load) ---
    float loadVoltage = 0.0f;  // Tension (V)
    float loadCurrent = 0.0f;  // Courant (A)
    float loadPower   = 0.0f;  // Puissance (W)

    // --- Énergie (kWh, calculées par l'Epever) ---
    float energieGenJour   = 0.0f;   // Générée aujourd'hui
    float energieGenTotal  = 0.0f;   // Générée total
    float energieConJour   = 0.0f;   // Consommée aujourd'hui
    float energieConTotal  = 0.0f;   // Consommée total

    // --- Ensoleillement ---
    bool sun = false;  // true = jour
    bool sunHistoryValid = false;
    uint8_t sunHistoryCount = 0;
    uint8_t sunHistoryHead = 0;
    bool sunHistoryState[5] = {};
    char sunHistoryTime[5][20] = {};

    // --- Horloge interne Epever ---
    bool    epeverClockOk = false;
    uint8_t epeverSecond  = 0;
    uint8_t epeverMinute  = 0;
    uint8_t epeverHour    = 0;
    uint8_t epeverDay     = 0;
    uint8_t epeverMonth   = 0;
    uint16_t epeverYear   = 0;
    bool    espClockOk    = false;

    // --- Relais ---
    bool relay1 = false;
    bool relay2 = false;

    // --- Boutons DI ---
    bool di1 = false;
    bool di2 = false;

    // --- Mode ---
    bool autoMode = true;  // true = automatique, false = manuel

    // --- Santé RS485 ---
    bool          rs485_ok    = false;
    unsigned long last_update = 0;
};

extern SystemState state;
