#pragma once

#include <Arduino.h>
#include <array>

namespace cyd {

// Holds the latest decoded state from the ThermoPro thermometer.
struct ThermoproReading {
    bool connected = false;
    uint8_t battery = 0;
    char deviceUnit = 'C';      // unit reported by the device ('C' or 'F')
    uint8_t probeCount = 4;     // number of probe slots the device advertises
    std::array<float, 4> rawTemperatures = { -999.0f, -999.0f, -999.0f, -999.0f };
    unsigned long lastUpdateMillis = 0; // device-local millis of last notification
};

// Returns true if a raw temperature value is a real reading (not a sentinel).
inline bool isValidTemperature(float t) {
    return t != -999.0f && t != -100.0f && t != 666.0f;
}

// Convert a raw temperature between Celsius and Fahrenheit.
inline float convertTemperature(float t, char from, char to) {
    if (!isValidTemperature(t)) {
        return t;
    }
    char f = static_cast<char>(toupper(from));
    char tt = static_cast<char>(toupper(to));
    if (f == tt) {
        return t;
    }
    if (f == 'C' && tt == 'F') {
        return t * 9.0f / 5.0f + 32.0f;
    }
    if (f == 'F' && tt == 'C') {
        return (t - 32.0f) * 5.0f / 9.0f;
    }
    return t;
}

} // namespace cyd
