/**
 * Web Server - HTTP Server and File Management
 * Serves web interface from SD card and provides REST API
 */

#ifndef WEB_SERVER_H
#define WEB_SERVER_H

#include <esp_err.h>
#include <esp_http_server.h>

#ifdef __cplusplus
extern "C" {
#endif

// Web server configuration
#define WEB_SERVER_PORT         80
#define WEB_SERVER_MAX_URI_LEN  512
#define WEB_SERVER_MAX_HDR_LEN  1024
#define WEB_SERVER_STACK_SIZE   8192

// Authentication configuration
#define WEB_AUTH_ENABLED        true
#define WEB_AUTH_USERNAME       "admin"
#define WEB_AUTH_PASSWORD       "audit123"
#define WEB_SESSION_TIMEOUT     3600  // 1 hour in seconds

// Wi-Fi AP configuration
#define WIFI_AP_SSID            "ESP32-P4-Bridge"
#define WIFI_AP_IP              "192.168.4.1"
#define WIFI_AP_CHANNEL         1

// File upload limits
#define MAX_FILE_SIZE           (1024 * 1024)  // 1MB
#define MAX_FILENAME_LEN        64

/**
 * Initialize HTTP web server
 * @return ESP_OK on success, error code otherwise
 */
esp_err_t web_server_init(void);

/**
 * Stop HTTP web server
 * @return ESP_OK on success
 */
esp_err_t web_server_stop(void);

/**
 * Update system status for web interface
 */
void web_server_update_status(void);

/**
 * Register custom URI handlers
 * @param server HTTP server handle
 * @return ESP_OK on success
 */
esp_err_t web_server_register_handlers(httpd_handle_t server);

/**
 * Authenticate HTTP request
 * @param req HTTP request handle
 * @return true if authenticated, false otherwise
 */
bool web_server_authenticate(httpd_req_t *req);

/**
 * Send JSON response
 * @param req HTTP request handle
 * @param json_string JSON string to send
 * @return ESP_OK on success
 */
esp_err_t web_server_send_json(httpd_req_t *req, const char *json_string);

/**
 * Send file from SD card
 * @param req HTTP request handle
 * @param filepath Path to file on SD card
 * @return ESP_OK on success
 */
esp_err_t web_server_send_file(httpd_req_t *req, const char *filepath);

/**
 * Handle file upload
 * @param req HTTP request handle
 * @param destination Destination path on SD card
 * @return ESP_OK on success
 */
esp_err_t web_server_handle_upload(httpd_req_t *req, const char *destination);

/**
 * Get MIME type from file extension
 * @param filename File name
 * @return MIME type string
 */
const char* web_server_get_mime_type(const char *filename);

// REST API endpoints
#define API_PREFIX              "/api"
#define API_SCAN_START          API_PREFIX "/scan/start"
#define API_SCAN_STOP           API_PREFIX "/scan/stop"
#define API_SCAN_STATUS         API_PREFIX "/scan/status"
#define API_SCAN_RESULTS        API_PREFIX "/scan/results"
#define API_SYSTEM_STATUS       API_PREFIX "/system/status"
#define API_SYSTEM_REBOOT       API_PREFIX "/system/reboot"
#define API_LOGS_LIST           API_PREFIX "/logs/list"
#define API_LOGS_DOWNLOAD       API_PREFIX "/logs/download"
#define API_CONFIG_GET          API_PREFIX "/config/get"
#define API_CONFIG_SET          API_PREFIX "/config/set"
#define API_UPLOAD_FILE         API_PREFIX "/upload"

#ifdef __cplusplus
}
#endif

#endif // WEB_SERVER_H
