// Visual hardware test for sync-fail / no-WiFi border rendering.
//
// Built only by env:sim-yellow and env:sim-red in platformio.ini. Reads the
// last cached image from LittleFS, draws it through epd_draw_image_with_border
// with the configured color, and sets NVS_KEY_ERR_BORDER=1 so that flashing
// the normal firmware afterwards exercises the recovery path (next 304
// repaints the image clean).
//
// Usage:
//   pio run -e sim-yellow --target upload
//   pio run -e sim-red    --target upload
//   pio run -e esp32dev   --target upload   # back to normal — verifies recovery

#ifdef SIM_BORDER

#include <Arduino.h>
#include <SPI.h>
#include <LittleFS.h>
#include <Preferences.h>
#include "config.h"
#include "epd.h"

#ifndef SIM_BORDER_COLOR
#error "sim_border.cpp built without SIM_BORDER_COLOR — set it in platformio.ini"
#endif

void setup() {
    Serial.begin(115200);
    Serial.println("[sim_border] boot");

    pinMode(PIN_BTN_RESET, INPUT_PULLUP);
    epd_setup_pins();
    LittleFS.begin(true);

    epd_init();

    File f = LittleFS.open(IMAGE_PATH, "r");
    if (f) {
        Serial.printf("[sim_border] /img.bin %u bytes — drawing with border color 0x%X\n",
                      (unsigned)f.size(), (unsigned)SIM_BORDER_COLOR);
        epd_draw_image_with_border(f, SIM_BORDER_COLOR, BORDER_THICKNESS_PX);
        f.close();
    } else {
        Serial.println("[sim_border] no /img.bin — falling back to full fill");
        epd_fill(SIM_BORDER_COLOR);
    }

    epd_sleep();

    // Mark err_border so the next normal-firmware boot will trigger the
    // 304-based clean repaint and we can confirm recovery end-to-end.
    Preferences prefs;
    prefs.begin(NVS_NAMESPACE, false);
    prefs.putInt(NVS_KEY_ERR_BORDER, 1);
    prefs.end();

    Serial.println("[sim_border] done — halting (deep sleep, button-reset to wake)");
    esp_deep_sleep_start();
}

void loop() {}

#endif // SIM_BORDER
