#ifndef INTEGRATION_LAYER_H
#define INTEGRATION_LAYER_H

#include <stdbool.h>
#include <stdint.h>
#include "midea_protocol.h"
#include "zigbee_zcl.h"

/**
 * @brief Initialize the integration layer
 * @return true if initialization successful, false otherwise
 */
bool integration_layer_init(void);

/**
 * @brief Deinitialize the integration layer
 * @return true if deinitialization successful, false otherwise
 */
bool integration_layer_deinit(void);

/**
 * @brief Convert Zigbee ZCL attributes to Midea UART control structure
 * @param zcl_attrs Source Zigbee attributes
 * @param uart_cmd Destination UART command structure
 * @return true if conversion successful, false otherwise
 */
bool integration_layer_zigbee_to_uart(const zcl_thermostat_attrs_t* zcl_attrs,
                                     midea_control_t* uart_cmd);

/**
 * @brief Convert Midea UART control structure to Zigbee ZCL attributes
 * @param uart_cmd Source UART command structure
 * @param zcl_attrs Destination Zigbee attributes
 * @return true if conversion successful, false otherwise
 */
bool integration_layer_uart_to_zigbee(const midea_control_t* uart_cmd,
                                     zcl_thermostat_attrs_t* zcl_attrs);

/**
 * @brief Handle incoming Zigbee command and convert to UART message
 * @param endpoint Zigbee endpoint
 * @param cluster_id Zigbee cluster ID
 * @param command_id Zigbee command ID
 * @param zigbee_payload Incoming Zigbee payload
 * @param zigbee_length Length of incoming payload
 * @param uart_buffer Buffer to store encoded UART message
 * @param uart_length Pointer to store length of encoded UART message
 * @return true if handling successful, false otherwise
 */
bool integration_layer_handle_zigbee_command(uint8_t endpoint, uint16_t cluster_id,
                                            uint8_t command_id, const uint8_t* zigbee_payload,
                                            uint16_t zigbee_length, uint8_t* uart_buffer,
                                            size_t* uart_length);

/**
 * @brief Handle incoming UART response and update Zigbee attributes
 * @param uart_response Incoming UART response
 * @param uart_length Length of UART response
 * @param zcl_attrs Pointer to store updated Zigbee attributes
 * @return true if handling successful, false otherwise
 */
bool integration_layer_handle_uart_response(const uint8_t* uart_response,
                                           size_t uart_length,
                                           zcl_thermostat_attrs_t* zcl_attrs);

/**
 * @brief Handle schedule commands from Zigbee
 * @param command_id Schedule command ID
 * @param payload Schedule payload
 * @param payload_length Length of payload
 * @return true if handling successful, false otherwise
 */
bool integration_layer_handle_schedule_command(uint8_t command_id,
                                              const uint8_t* payload,
                                              uint16_t payload_length);

#endif // INTEGRATION_LAYER_H