/*
 * OTA 状态管理器实现
 * 管理OTA更新状态，支持失败回退到上一版本
 */

#include "ota_manager.h"
#include "device_nvs.h"
#include "nvs.h"
#include "esp_log.h"
#include "esp_ota_ops.h"
#include "esp_app_desc.h"
#include "esp_system.h"
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include <string.h>
#include <stdio.h>

static const char *tag_s = "OTA_MGR";

#define OTA_MGR_NVS_NAMESPACE    "ota_mgr"
#define OTA_MGR_KEY_STATE        "state"
#define OTA_MGR_KEY_TYPE         "type"
#define OTA_MGR_KEY_TARGET_VER   "tgt_ver"
#define OTA_MGR_KEY_SOURCE_VER   "src_ver"
#define OTA_MGR_KEY_TIMESTAMP    "timestamp"
#define OTA_MGR_KEY_BOOT_COUNT   "boot_cnt"
#define OTA_MGR_KEY_MAX_ATTEMPTS "max_retry"
#define OTA_MGR_KEY_LAST_SUCCESS_VER "last_ok_ver"

#define OTA_MAX_BOOT_ATTEMPTS    3        /* 最大启动尝试次数 */
#define OTA_SESSION_MAGIC        0x4F544100u /* "OTA\0" */

static ota_session_info_t current_session_s;
static bool session_loaded_s = false;

/**
 * @brief 从NVS读取会话信息
 */
static esp_err_t load_session_from_nvs(void)
{
    nvs_handle_t handle;
    esp_err_t err = nvs_open(OTA_MGR_NVS_NAMESPACE, NVS_READONLY, &handle);
    if (err != ESP_OK) {
        return err;
    }

    uint32_t magic = 0;
    err = nvs_get_u32(handle, "magic", &magic);
    if (err != ESP_OK || magic != OTA_SESSION_MAGIC) {
        nvs_close(handle);
        memset(&current_session_s, 0, sizeof(current_session_s));
        session_loaded_s = false;
        return ESP_ERR_NOT_FOUND;
    }

    uint8_t state = 0;
    nvs_get_u8(handle, OTA_MGR_KEY_STATE, &state);
    current_session_s.state = (ota_state_t)state;

    uint8_t type = 0;
    nvs_get_u8(handle, OTA_MGR_KEY_TYPE, &type);
    current_session_s.type = (ota_type_t)type;

    size_t len = sizeof(current_session_s.target_version);
    nvs_get_str(handle, OTA_MGR_KEY_TARGET_VER, current_session_s.target_version, &len);

    len = sizeof(current_session_s.source_version);
    nvs_get_str(handle, OTA_MGR_KEY_SOURCE_VER, current_session_s.source_version, &len);

    nvs_get_u32(handle, OTA_MGR_KEY_TIMESTAMP, &current_session_s.timestamp);
    nvs_get_u8(handle, OTA_MGR_KEY_BOOT_COUNT, &current_session_s.boot_count);
    nvs_get_u8(handle, OTA_MGR_KEY_MAX_ATTEMPTS, &current_session_s.max_boot_attempts);

    nvs_close(handle);
    session_loaded_s = true;

    ESP_LOGI(tag_s, "Loaded session: state=%d, type=%d, target=%s, source=%s, boot_cnt=%d",
             current_session_s.state, current_session_s.type,
             current_session_s.target_version, current_session_s.source_version,
             current_session_s.boot_count);

    return ESP_OK;
}

/**
 * @brief 保存会话信息到NVS
 */
static esp_err_t save_session_to_nvs(void)
{
    nvs_handle_t handle;
    esp_err_t err = nvs_open(OTA_MGR_NVS_NAMESPACE, NVS_READWRITE, &handle);
    if (err != ESP_OK) {
        ESP_LOGE(tag_s, "Failed to open NVS: %s", esp_err_to_name(err));
        return err;
    }

    nvs_set_u32(handle, "magic", OTA_SESSION_MAGIC);
    nvs_set_u8(handle, OTA_MGR_KEY_STATE, (uint8_t)current_session_s.state);
    nvs_set_u8(handle, OTA_MGR_KEY_TYPE, (uint8_t)current_session_s.type);
    nvs_set_str(handle, OTA_MGR_KEY_TARGET_VER, current_session_s.target_version);
    nvs_set_str(handle, OTA_MGR_KEY_SOURCE_VER, current_session_s.source_version);
    nvs_set_u32(handle, OTA_MGR_KEY_TIMESTAMP, current_session_s.timestamp);
    nvs_set_u8(handle, OTA_MGR_KEY_BOOT_COUNT, current_session_s.boot_count);
    nvs_set_u8(handle, OTA_MGR_KEY_MAX_ATTEMPTS, current_session_s.max_boot_attempts);

    nvs_commit(handle);
    nvs_close(handle);

    return ESP_OK;
}

/**
 * @brief 清除会话信息
 */
static esp_err_t clear_session_nvs(void)
{
    nvs_handle_t handle;
    esp_err_t err = nvs_open(OTA_MGR_NVS_NAMESPACE, NVS_READWRITE, &handle);
    if (err != ESP_OK) {
        return err;
    }

    nvs_erase_all(handle);
    nvs_commit(handle);
    nvs_close(handle);

    memset(&current_session_s, 0, sizeof(current_session_s));
    session_loaded_s = false;

    ESP_LOGI(tag_s, "Session cleared");
    return ESP_OK;
}

/**
 * @brief 检查当前运行的分区是否是通过OTA更新的分区
 */
static bool is_running_ota_partition(void)
{
    const esp_partition_t *running = esp_ota_get_running_partition();
    if (running == NULL) {
        return false;
    }
    return (strcmp(running->label, "ota_0") == 0 ||
            strcmp(running->label, "ota_1") == 0);
}


esp_err_t ota_manager_init(void)
{
    ESP_LOGI(tag_s, "OTA Manager initializing...");

    /* 获取当前固件信息 */
    const esp_partition_t *running = esp_ota_get_running_partition();
    const esp_app_desc_t *app_desc = esp_app_get_description();

    if (running) {
        ESP_LOGI(tag_s, "Running partition: %s", running->label);
    }
    if (app_desc) {
        ESP_LOGI(tag_s, "Running version: %s", app_desc->version);
    }

    /* 从NVS加载会话信息 */
    esp_err_t err = load_session_from_nvs();

    if (err != ESP_OK) {
        /* 没有OTA会话，正常启动 */
        ESP_LOGI(tag_s, "No pending OTA session, normal boot");

        /* 如果运行的是OTA分区，说明是首次烧录或已稳定运行，保存版本信息 */
        if (is_running_ota_partition() && app_desc) {
            ota_manager_update_stored_version(app_desc->version);
        }
        return ESP_OK;
    }

    /* 有OTA会话，检查状态 */
    if (current_session_s.state == OTA_STATE_PENDING_VERIFY) {
        /* OTA已完成，这是新固件第一次启动 */
        ESP_LOGI(tag_s, "New firmware boot detected, verifying...");

        /* 增加启动计数 */
        current_session_s.boot_count++;
        save_session_to_nvs();

        /* 检查版本是否匹配 */
        if (app_desc && strcmp(app_desc->version, current_session_s.target_version) == 0) {
            ESP_LOGI(tag_s, "Version matches: %s", app_desc->version);

            /* 标记为成功 */
            current_session_s.state = OTA_STATE_SUCCESS;
            save_session_to_nvs();
            ota_manager_update_stored_version(app_desc->version);

            /* 延迟一段时间后清除会话（给应用层确认的时间） */
            /* 应用层需要调用 ota_manager_mark_success() 来最终确认 */
        } else {
            ESP_LOGW(tag_s, "Version mismatch! Expected: %s, Got: %s",
                     current_session_s.target_version,
                     app_desc ? app_desc->version : "unknown");

            /* 版本不匹配，可能是回退或其他问题，清除会话 */
            clear_session_nvs();
        }

    } else if (current_session_s.state == OTA_STATE_IN_PROGRESS) {
        /* OTA进行中但设备重启了，可能是断电或崩溃 */
        ESP_LOGW(tag_s, "OTA was in progress but interrupted");

        /* 标记为失败 */
        current_session_s.state = OTA_STATE_FAILED;
        save_session_to_nvs();

        /* 尝试回退 */
        if (current_session_s.boot_count >= 1) {
            ESP_LOGW(tag_s, "OTA interrupted, triggering rollback");
            ota_manager_rollback();
            return ESP_FAIL; /* 设备将重启 */
        }

    } else if (current_session_s.state == OTA_STATE_SUCCESS) {
        /* 新固件已启动但未收到应用层确认，重复重启则回退 */
        current_session_s.boot_count++;
        save_session_to_nvs();

        ESP_LOGI(tag_s, "OTA success pending confirm, boot %u/%u",
                 current_session_s.boot_count,
                 current_session_s.max_boot_attempts);

        if (current_session_s.boot_count >= current_session_s.max_boot_attempts) {
            ESP_LOGW(tag_s, "OTA not confirmed, rolling back");
            ota_manager_rollback();
            return ESP_FAIL;
        }

    } else if (current_session_s.state == OTA_STATE_FAILED) {
        /* 上次OTA已标记为失败 */
        ESP_LOGW(tag_s, "Previous OTA marked as failed");

        /* 检查是否应该回退 */
        if (is_running_ota_partition()) {
            ESP_LOGW(tag_s, "Triggering rollback to previous version");
            ota_manager_rollback();
            return ESP_FAIL; /* 设备将重启 */
        }
    }

    return ESP_OK;
}

esp_err_t ota_manager_begin_session(ota_type_t type, const char *target_version)
{
    if (target_version == NULL || strlen(target_version) == 0) {
        return ESP_ERR_INVALID_ARG;
    }

    const esp_app_desc_t *app_desc = esp_app_get_description();

    memset(&current_session_s, 0, sizeof(current_session_s));
    current_session_s.state = OTA_STATE_IN_PROGRESS;
    current_session_s.type = type;
    strncpy(current_session_s.target_version, target_version, sizeof(current_session_s.target_version) - 1);
    if (app_desc) {
        strncpy(current_session_s.source_version, app_desc->version, sizeof(current_session_s.source_version) - 1);
    }
    current_session_s.timestamp = (uint32_t)(esp_timer_get_time() / 1000000ULL);
    current_session_s.boot_count = 0;
    current_session_s.max_boot_attempts = OTA_MAX_BOOT_ATTEMPTS;

    session_loaded_s = true;

    ESP_LOGI(tag_s, "OTA session started: type=%d, %s -> %s",
             type, current_session_s.source_version, target_version);

    return save_session_to_nvs();
}

esp_err_t ota_manager_end_session(void)
{
    if (!session_loaded_s || current_session_s.state != OTA_STATE_IN_PROGRESS) {
        return ESP_ERR_INVALID_STATE;
    }

    current_session_s.state = OTA_STATE_PENDING_VERIFY;
    ESP_LOGI(tag_s, "OTA session ended, waiting for verification");

    return save_session_to_nvs();
}

esp_err_t ota_manager_mark_success(void)
{
    if (!session_loaded_s) {
        return ESP_OK; /* 没有会话，不需要标记 */
    }

    /* 清除会话，OTA完全成功 */
    const esp_app_desc_t *app_desc = esp_app_get_description();
    if (app_desc) {
        ota_manager_update_stored_version(app_desc->version);
    }

    clear_session_nvs();
    ESP_LOGI(tag_s, "OTA marked as successful");

    return ESP_OK;
}

esp_err_t ota_manager_mark_failed_and_rollback(const char *reason)
{
    if (!session_loaded_s) {
        /* 没有会话，直接回退 */
        return ota_manager_rollback();
    }

    current_session_s.state = OTA_STATE_FAILED;
    save_session_to_nvs();

    ESP_LOGE(tag_s, "OTA marked as failed: %s", reason ? reason : "unknown");

    return ota_manager_rollback();
}

bool ota_manager_should_rollback(void)
{
    if (!session_loaded_s) {
        return false;
    }

    if (current_session_s.state != OTA_STATE_PENDING_VERIFY) {
        return false;
    }

    /* 增加启动计数 */
    current_session_s.boot_count++;
    save_session_to_nvs();

    ESP_LOGW(tag_s, "Boot count: %d/%d",
             current_session_s.boot_count,
             current_session_s.max_boot_attempts);

    /* 如果启动次数超过阈值，应该回退 */
    if (current_session_s.boot_count >= current_session_s.max_boot_attempts) {
        ESP_LOGE(tag_s, "Max boot attempts reached, rollback required");
        return true;
    }

    return false;
}

esp_err_t ota_manager_rollback(void)
{
    ESP_LOGW(tag_s, "Performing rollback...");

    /* 获取另一个OTA分区（上一版本） */
    const esp_partition_t *prev_partition = esp_ota_get_next_update_partition(NULL);

    if (prev_partition == NULL) {
        ESP_LOGE(tag_s, "No previous partition found, cannot rollback");
        return ESP_ERR_NOT_FOUND;
    }

    ESP_LOGI(tag_s, "Rollback to partition: %s", prev_partition->label);

    /* 设置启动分区为上一版本 */
    esp_err_t err = esp_ota_set_boot_partition(prev_partition);
    if (err != ESP_OK) {
        ESP_LOGE(tag_s, "Failed to set boot partition: %s", esp_err_to_name(err));
        return err;
    }

    /* 更新会话状态 */
    if (session_loaded_s) {
        current_session_s.state = OTA_STATE_ROLLBACK;
        save_session_to_nvs();
    }

    ESP_LOGI(tag_s, "Rollback set, rebooting...");
    vTaskDelay(pdMS_TO_TICKS(1000));
    esp_restart();

    return ESP_OK; /* 实际上不会执行到这里 */
}

ota_state_t ota_manager_get_state(void)
{
    if (!session_loaded_s) {
        return OTA_STATE_IDLE;
    }
    return current_session_s.state;
}

esp_err_t ota_manager_get_session_info(ota_session_info_t *info)
{
    if (!session_loaded_s || info == NULL) {
        return ESP_ERR_INVALID_STATE;
    }
    memcpy(info, &current_session_s, sizeof(ota_session_info_t));
    return ESP_OK;
}

const char* ota_manager_get_current_partition_label(void)
{
    const esp_partition_t *running = esp_ota_get_running_partition();
    if (running == NULL) {
        return NULL;
    }
    return running->label;
}

esp_err_t ota_manager_get_stored_version(char *buf, size_t len)
{
    if (buf == NULL || len == 0) {
        return ESP_ERR_INVALID_ARG;
    }

    nvs_handle_t handle;
    esp_err_t err = nvs_open(OTA_MGR_NVS_NAMESPACE, NVS_READONLY, &handle);
    if (err != ESP_OK) {
        return err;
    }

    size_t required_len = len;
    err = nvs_get_str(handle, OTA_MGR_KEY_LAST_SUCCESS_VER, buf, &required_len);
    nvs_close(handle);

    if (err == ESP_ERR_NVS_NOT_FOUND) {
        buf[0] = '\0';
        return ESP_OK;
    }

    return err;
}

esp_err_t ota_manager_update_stored_version(const char *version)
{
    if (version == NULL) {
        return ESP_ERR_INVALID_ARG;
    }

    nvs_handle_t handle;
    esp_err_t err = nvs_open(OTA_MGR_NVS_NAMESPACE, NVS_READWRITE, &handle);
    if (err != ESP_OK) {
        return err;
    }

    err = nvs_set_str(handle, OTA_MGR_KEY_LAST_SUCCESS_VER, version);
    if (err == ESP_OK) {
        nvs_commit(handle);
    }
    nvs_close(handle);

    ESP_LOGI(tag_s, "Stored version updated: %s", version);
    return err;
}
