#pragma once
#include "Arduino.h"
#include <map>
#include <vector>

struct File {
    std::string* _buf = nullptr;
    bool _valid = false;
    bool _write = false;
    size_t _pos = 0;

    explicit operator bool() const { return _valid; }
    void close() { _valid = false; }
    size_t write(const uint8_t* data, size_t len) {
        if (_buf && _write) { _buf->append((const char*)data, len); return len; }
        return 0;
    }
    int read() {
        if (_buf && _pos < _buf->size()) return (uint8_t)(*_buf)[_pos++];
        return -1;
    }
    size_t size() { return _buf ? _buf->size() : 0; }
};

struct LittleFSClass {
    std::map<std::string, std::string> files;

    bool begin(bool) { return true; }

    File open(const char* path, const char* mode, bool create = false) {
        File f;
        f._write = (mode[0] == 'w');
        f._pos = 0;
        if (f._write) {
            f._buf   = &files[path];
            f._buf->clear();
            f._valid = true;
        } else {
            // Read mode: behave like real LittleFS — return invalid when file
            // doesn't exist (do NOT create an empty entry via operator[]).
            auto it = files.find(path);
            if (it == files.end()) {
                f._valid = false;
                f._buf   = nullptr;
            } else {
                f._buf   = &it->second;
                f._valid = true;
            }
        }
        return f;
    }
} LittleFS;
