/**
 * SD Card Manager - File System Operations
 * Manages FAT32 SD card for web assets, logs, and configuration
 */

#ifndef SD_MANAGER_H
#define SD_MANAGER_H

#include <esp_err.h>
#include <esp_vfs_fat.h>
#include <sdmmc_cmd.h>

#ifdef __cplusplus
extern "C" {
#endif

// SD card configuration
#define SD_MOUNT_POINT          "/sdcard"
#define SD_MAX_FILES            10
#define SD_FORMAT_IF_MOUNT_FAIL true
#define SD_ALLOC_UNIT_SIZE      16 * 1024

// Directory structure
#define SD_WWW_DIR              SD_MOUNT_POINT "/www"
#define SD_LOGS_DIR             SD_MOUNT_POINT "/logs"
#define SD_CONFIG_DIR           SD_MOUNT_POINT "/config"
#define SD_SCRIPTS_DIR          SD_MOUNT_POINT "/scripts"

// File paths
#define SD_CONFIG_FILE          SD_CONFIG_DIR "/config.json"
#define SD_WIFI_CONFIG_FILE     SD_CONFIG_DIR "/wifi.json"
#define SD_SCAN_CONFIG_FILE     SD_CONFIG_DIR "/scan.json"
#define SD_INDEX_HTML           SD_WWW_DIR "/index.html"

// Log file naming
#define LOG_FILE_PREFIX         "audit_"
#define LOG_FILE_EXTENSION      ".log"
#define LOG_MAX_SIZE            (1024 * 1024)  // 1MB per log file
#define LOG_MAX_FILES           100

/**
 * Initialize SD card and mount file system
 * @return ESP_OK on success, error code otherwise
 */
esp_err_t sd_manager_init(void);

/**
 * Deinitialize SD card
 * @return ESP_OK on success
 */
esp_err_t sd_manager_deinit(void);

/**
 * Check if SD card is mounted and accessible
 * @return true if available, false otherwise
 */
bool sd_manager_is_available(void);

/**
 * Create directory structure on SD card
 * @return ESP_OK on success
 */
esp_err_t sd_manager_create_directories(void);

/**
 * Write data to file
 * @param filepath Full path to file
 * @param data Data to write
 * @param size Size of data
 * @param append Whether to append or overwrite
 * @return ESP_OK on success
 */
esp_err_t sd_manager_write_file(const char *filepath, const void *data, size_t size, bool append);

/**
 * Read data from file
 * @param filepath Full path to file
 * @param buffer Buffer to read into
 * @param size Size of buffer
 * @param bytes_read Pointer to store actual bytes read
 * @return ESP_OK on success
 */
esp_err_t sd_manager_read_file(const char *filepath, void *buffer, size_t size, size_t *bytes_read);

/**
 * Delete file
 * @param filepath Full path to file
 * @return ESP_OK on success
 */
esp_err_t sd_manager_delete_file(const char *filepath);

/**
 * Check if file exists
 * @param filepath Full path to file
 * @return true if file exists, false otherwise
 */
bool sd_manager_file_exists(const char *filepath);

/**
 * Get file size
 * @param filepath Full path to file
 * @param size Pointer to store file size
 * @return ESP_OK on success
 */
esp_err_t sd_manager_get_file_size(const char *filepath, size_t *size);

/**
 * List files in directory
 * @param dir_path Directory path
 * @param file_list Buffer to store file list (JSON format)
 * @param buffer_size Size of buffer
 * @return ESP_OK on success
 */
esp_err_t sd_manager_list_files(const char *dir_path, char *file_list, size_t buffer_size);

/**
 * Create new log file with timestamp
 * @param log_type Type of log (e.g., "scan", "system", "error")
 * @param filepath Buffer to store created filepath
 * @param filepath_size Size of filepath buffer
 * @return ESP_OK on success
 */
esp_err_t sd_manager_create_log_file(const char *log_type, char *filepath, size_t filepath_size);

/**
 * Write log entry with timestamp
 * @param filepath Log file path
 * @param level Log level (INFO, WARN, ERROR)
 * @param message Log message
 * @return ESP_OK on success
 */
esp_err_t sd_manager_write_log(const char *filepath, const char *level, const char *message);

/**
 * Get SD card information
 * @param info Pointer to sdmmc_card_t structure
 * @return ESP_OK on success
 */
esp_err_t sd_manager_get_card_info(sdmmc_card_t **info);

/**
 * Get free space on SD card
 * @param free_bytes Pointer to store free bytes
 * @param total_bytes Pointer to store total bytes
 * @return ESP_OK on success
 */
esp_err_t sd_manager_get_space_info(uint64_t *free_bytes, uint64_t *total_bytes);

/**
 * Create directory
 * @param path Directory path to create
 * @return ESP_OK on success
 */
esp_err_t sd_manager_mkdir(const char *path);

/**
 * Append data to file
 * @param filepath Full path to file
 * @param data Data to append
 * @param size Size of data
 * @return ESP_OK on success
 */
esp_err_t sd_manager_append_file(const char *filepath, const void *data, size_t size);

/**
 * Rename file
 * @param old_path Current file path
 * @param new_path New file path
 * @return ESP_OK on success
 */
esp_err_t sd_manager_rename_file(const char *old_path, const char *new_path);

#ifdef __cplusplus
}
#endif

#endif // SD_MANAGER_H 