#include "device_model.h"
#include <string.h>
#include "esp_log.h"

static const char *tag_s = "DEVICE_MODEL";

device_model_t device_model_from_full_id(const char *device_id)
{
    if (!device_id || device_id[0] == '\0') {
        ESP_LOGW(tag_s, "device_id 为空，按 1101");
        return DEVICE_MODEL_1101;
    }

    size_t len = strlen(device_id);
    if (len == DEVICE_MODEL_SLICE_CHAR_LEN) {
        if (strncmp(device_id, "1101", DEVICE_MODEL_SLICE_CHAR_LEN) == 0) {
            ESP_LOGI(tag_s, "短型号 1101");
            return DEVICE_MODEL_1101;
        }
        if (strncmp(device_id, "1102", DEVICE_MODEL_SLICE_CHAR_LEN) == 0) {
            ESP_LOGI(tag_s, "短型号 1102");
            return DEVICE_MODEL_1102;
        }
    }

    if (len < (size_t)(DEVICE_MODEL_SLICE_0BASE_START + DEVICE_MODEL_SLICE_CHAR_LEN)) {
        ESP_LOGW(tag_s, "device_id 长度不足，按 1101");
        return DEVICE_MODEL_1101;
    }

    const char *slice = device_id + DEVICE_MODEL_SLICE_0BASE_START;

    if (strncmp(slice, "1101", DEVICE_MODEL_SLICE_CHAR_LEN) == 0) {
        ESP_LOGI(tag_s, "片段 [3..6]=1101 → 型号 1101");
        return DEVICE_MODEL_1101;
    }
    if (strncmp(slice, "1102", DEVICE_MODEL_SLICE_CHAR_LEN) == 0) {
        ESP_LOGI(tag_s, "片段 [3..6]=1102 → 型号 1102");
        return DEVICE_MODEL_1102;
    }

    ESP_LOGW(tag_s, "片段 [3..6]=\"%.4s\" 未识别，默认 1101", slice);
    return DEVICE_MODEL_1101;
}
