#pragma once

#include <Arduino.h>
#include "StartupSdManager.h"

// Convenience alias so sketches can use System.println(...) style logging.
// Arduino exposes Serial, not System, so map System -> Serial.
#ifndef System
#define System Serial
#endif

enum class SystemEvent : uint8_t {
  BOOTING = 0,
  SD_MISSING,
  SD_READY,
  SD_REMOVED
};

using SystemEventCallback = void (*)(SystemEvent event, const char* message);

struct SystemStartupConfig {
  uint32_t serialDelayMs = 5000;
  SdWatcherConfig sd{};
};

class SystemStartup {
 public:
  explicit SystemStartup(Print& serial = Serial);

  bool begin(const SystemStartupConfig& cfg = SystemStartupConfig{}, SystemEventCallback callback = nullptr);
  void update();

  bool isSdMounted() const { return sd_.isMounted(); }
  StartupSdManager& sdManager() { return sd_; }

 private:
  static void onSdEventThunk(SdEvent event, const char* message);
  void onSdEvent(SdEvent event, const char* message);
  void emit(SystemEvent event, const char* message);
  void oledShow3(const char* l1, const char* l2 = nullptr, const char* l3 = nullptr);

  Print& serial_;
  SystemStartupConfig cfg_{};
  SystemEventCallback callback_ = nullptr;
  StartupSdManager sd_;
};
