#pragma once

#include <Arduino.h>
#include <TBeamStorage.h>

namespace tbeam {

struct LoggerConfig {
  bool echoSerial = true;
  bool echoStorage = true;
  bool autoFlush = true;
  uint32_t flushIntervalMs = 2000;
};

class TBeamLogger : public Print {
 public:
  TBeamLogger() = default;

  bool begin(Print& serial, TBeamStorage* storage = nullptr, const LoggerConfig& config = LoggerConfig{});
  void update();

  bool openLog(const char* path);
  bool openUniqueLog(const char* prefix, const char* extension = ".log");
  const char* currentLogPath() const;
  bool storageReady() const;
  void flush();
  void closeLog();

  size_t write(uint8_t value) override;
  size_t write(const uint8_t* buffer, size_t size) override;

 private:
  Print* serial_ = nullptr;
  TBeamStorage* storage_ = nullptr;
  LoggerConfig config_{};
  uint32_t lastFlushMs_ = 0;
};

}  // namespace tbeam
