// image_utils.cpp
#define STB_IMAGE_IMPLEMENTATION
#include "../vendor/stb/stb_image.h"

#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "../vendor/stb/stb_image_write.h"

#include <iostream>
#include <string>

bool ConvertToThumbnailTGA(const std::string& input_path, const std::string& output_tga_path) {
    int width, height, channels;

    stbi_uc* data = stbi_load(input_path.c_str(), &width, &height, &channels, 4);
    if (!data) {
        std::cout << "[ERROR] Failed to decode image: " << input_path << std::endl;
        return false;
    }

    int result = stbi_write_tga(output_tga_path.c_str(), width, height, 4, data);

    stbi_image_free(data);

    if (!result) {
        std::cout << "[ERROR] Failed to save TGA thumbnail to: " << output_tga_path << std::endl;
        return false;
    }

    return true;
}

void ProcessUserThumbnail(int critter_id, const std::string& user_selected_file_path) {
    std::string target_tga_path = "./data/thumbnails/" + std::to_string(critter_id) + ".tga";

    if (ConvertToThumbnailTGA(user_selected_file_path, target_tga_path)) {
        std::cout << "[INFO] Thumbnail successfully generated for Critter #" << critter_id << std::endl;
    }
}