/*
 * Ballu AC ESP32-C6 Controller - application entry point.
 *
 * Wires the full stack together via the application controller and runs the
 * main service loop: forward Zigbee commands to the AC and periodically poll
 * AC status back into the Zigbee ZCL thermostat cluster.
 *
 * The main() below is compiled only for the firmware/app build. Unit-test
 * binaries define UNIT_TEST and provide their own main(), so it is excluded
 * there to avoid a duplicate-symbol clash when all source files are linked in.
 */

#include "app_controller.h"

#ifndef UNIT_TEST

#include <stdio.h>

// Provided by the platform layer on real hardware (ESP-IDF: esp_timer /
// xTaskGetTickCount). Weakly stubbed here so the app links standalone.
__attribute__((weak)) uint32_t platform_now_ms(void) {
    return 0;
}

__attribute__((weak)) void platform_sleep_ms(uint32_t ms) {
    (void)ms;
}

int main(void) {
    app_controller_t app;

    if (!app_controller_init(&app, NULL)) {
        printf("app_controller_init failed\n");
        return 1;
    }

    printf("Ballu AC controller started (UART %d baud)\n",
           app.config.uart_config.baud_rate);

    // Service loop: poll status on the configured interval and let the Zigbee
    // stack deliver commands via app_controller_process_zigbee_command().
    for (;;) {
        uint32_t now = platform_now_ms();

        app_controller_poll_status(&app, now);
        app_controller_recover_if_needed(&app, now);

        platform_sleep_ms(app.status_monitor.config.poll_interval_ms);
    }

    // Not reached.
    app_controller_deinit(&app);
    return 0;
}

#endif // UNIT_TEST
