#include <dpp/dpp.h>
#include <iostream>
#include <fstream> // Required for file operations
#include <vector> // Required for std::vector
#include <string>

using namespace std;


#include "ImageProcessor.h"


int main() {

	// --- Load the bot token from a file ---
	string token_file_path = "token.txt";

	ifstream token_file(token_file_path);
	string bot_token;
	if (!token_file.is_open()) {
		// if not, try parent directory
		if (!token_file.is_open()) {
			token_file_path = "../token.txt";
			token_file.open(token_file_path);
			if (!token_file.is_open())
			{
				cerr << "ERROR: Could not open token.txt. Please create the file and add your bot token." << std::endl;
        		return 1;
			}
		}		
	}
	getline(token_file, bot_token);
    token_file.close();

	if (bot_token.empty()) {
        std::cerr << "ERROR: Token file is empty. Please add your bot token to token.txt." << std::endl;
        return 1;
    }

    dpp::cluster bot(bot_token, dpp::i_default_intents | dpp::i_message_content);

    bot.on_log(dpp::utility::cout_logger()); // Corrected: no 'true' argument

    std::cout<<"in main"<<endl;
	// Create an instance of your ImageProcessor class
    ImageProcessor image_processor(bot);

    bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
        std::cout << "Received slash cmd" << std::endl << std::flush; // Keep for testing
        bot.log(dpp::ll_info, "Received slash command: " + event.command.get_command_name());
        if (event.command.get_command_name() == "ping") {
            bot.log(dpp::ll_info, "Processing /ping command. Replying now!");
            event.reply("Pong!");
        } else {
            bot.log(dpp::ll_info, "Received unknown slash command: " + event.command.get_command_name());
        }
    });

    bot.on_ready([&bot](const dpp::ready_t& event) {
        bot.log(dpp::ll_info, "Bot ready. Logged in as: " + bot.me.username);

        if (dpp::run_once<struct register_bot_commands>()) {
            bot.log(dpp::ll_info, "Registering global /ping command...");
            bot.global_command_create(dpp::slashcommand("ping", "Ping pong!", bot.me.id));
        }
    });

	bot.on_message_create([&image_processor, &bot](const dpp::message_create_t& event) {
		// Check if the message has any attachments
		if (!event.msg.attachments.empty()) { //
			// Log that an attachment was found
			bot.log(dpp::ll_info, "Message from " + event.msg.author.username + " contains attachments!");
			
			// You can iterate through the attachments if you need details about each one
			for (const auto& attachment : event.msg.attachments) { //
				bot.log(dpp::ll_info, "  Attachment ID: " + std::to_string(attachment.id) + 
									", Filename: " + attachment.filename + 
									", Size: " + std::to_string(attachment.size) + " bytes");
				// You can also get the URL of the attachment: attachment.url
				// And a proxied URL: attachment.proxy_url
				std::string filename = attachment.filename;
				
				
				if (filename.substr(strlen(filename.c_str())-3,3) == "jpg")
				{
					bot.log(dpp::ll_info, "jpg detected");

					std::string local_filename = "temp_" + attachment.filename;

					image_processor.process_attachment(event,attachment);
					bot.log(dpp::ll_info, "image flipped horizontally");
					// Download the attachment first
					//download_file(bot, event, attachment.url, local_filename); //

					// Note: The download is asynchronous. You would typically do
					// further processing in the http_request_completion_t handler.
					// For a simple example, you might call load_image from there.
					
				}
			}

			// Example: Reply to the user saying you detected an attachment
			// event.reply("Thanks for the attachment!");
		} else {
			// Log if no attachments were found
			bot.log(dpp::ll_info, "Message from " + event.msg.author.username + " has no attachments.");
		}

		// You can also add logic for processing other types of messages or commands here
	});

    std::cerr << "--- Direct stderr test: Bot is about to start listening ---" << std::endl;

    bot.start(dpp::st_wait);
    return 0;
}