/*******************************************************************************
 * @file    dr_mem.h
 * @brief   Name-based Memory API (dr_memWrite / dr_memRead)
 * @author  Charles KWON <charleskwon@medithings.co.kr>
 * @date    2026-02-19
 * @copyright (c) 2026 Medithings Inc. All rights reserved.
 *
 * @details Provides name-based memory access that automatically routes to
 *          the correct backend storage:
 *          - FDS (internal flash) for device config
 *          - W25Q32 (external flash) for calibration & data
 *
 *          Usage:
 *            dr_memWrite("serial_no", data, 12);
 *            dr_memRead("serial_no", buf, 12);
 ******************************************************************************/

#ifndef DR_MEM_H
#define DR_MEM_H

#include <stdint.h>
#include <stdbool.h>
#include "sdk_errors.h"

/*==============================================================================
 * BACKEND TYPES
 *============================================================================*/

typedef enum {
    MEM_BACKEND_FDS,       /**< Internal Flash (FDS) - device config */
    MEM_BACKEND_W25Q32     /**< External Flash (W25Q32RV) - calibration/data */
} mem_backend_t;

/*==============================================================================
 * LOOKUP TABLE ENTRY
 *============================================================================*/

typedef struct {
    const char    *key;         /**< Name key (e.g., "hw_no", "agc_gain") */
    mem_backend_t  backend;     /**< Storage backend */
    uint32_t       offset;      /**< FDS: offsetof in config_data_t, W25Q32: address */
    uint16_t       size;        /**< Data size in bytes */
    bool           encrypted;   /**< AES-128 CBC encryption */
    const void    *default_val; /**< Default value (NULL = no default) */
} dr_mem_entry_t;

/*==============================================================================
 * PUBLIC API
 *============================================================================*/

/**
 * @brief Initialize memory subsystem (FDS + W25Q32)
 * @return NRF_SUCCESS on success
 */
ret_code_t dr_memInit(void);

/**
 * @brief Write data by name
 * @param key   Name key (e.g., "serial_no", "agc_gain")
 * @param data  Data to write
 * @param len   Data length in bytes
 * @return NRF_SUCCESS on success, NRF_ERROR_NOT_FOUND if key unknown
 */
ret_code_t dr_memWrite(const char *key, const void *data, uint16_t len);

/**
 * @brief Read data by name
 * @param key   Name key (e.g., "serial_no", "agc_gain")
 * @param data  Output buffer
 * @param len   Buffer length in bytes
 * @return NRF_SUCCESS on success, NRF_ERROR_NOT_FOUND if key unknown
 */
ret_code_t dr_memRead(const char *key, void *data, uint16_t len);

/**
 * @brief Get entry info by name (for debugging)
 * @param key   Name key
 * @return Pointer to entry, or NULL if not found
 */
const dr_mem_entry_t *dr_memFind(const char *key);

/**
 * @brief Write default values to all entries (when hw_no is uninitialized)
 * @return NRF_SUCCESS on success
 */
ret_code_t dr_memSetDefaults(void);

#endif /* DR_MEM_H */
