/**************************************************************************/
/*  ai_code_completion.cpp                                                */
/**************************************************************************/

#include "ai_code_completion.h"

#include "ai_assistant.h"

void AICodeCompletion::_bind_methods() {
	ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &AICodeCompletion::set_enabled);
	ClassDB::bind_method(D_METHOD("is_enabled"), &AICodeCompletion::is_enabled);
	
	ClassDB::bind_method(D_METHOD("set_suggestion_delay", "delay"), &AICodeCompletion::set_suggestion_delay);
	ClassDB::bind_method(D_METHOD("get_suggestion_delay"), &AICodeCompletion::get_suggestion_delay);
	
	ClassDB::bind_method(D_METHOD("get_suggestions", "code", "cursor_position"), &AICodeCompletion::get_suggestions);

	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
	ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "suggestion_delay"), "set_suggestion_delay", "get_suggestion_delay");
}

void AICodeCompletion::set_enabled(bool p_enabled) {
	enabled = p_enabled;
}

bool AICodeCompletion::is_enabled() const {
	return enabled;
}

void AICodeCompletion::set_suggestion_delay(float p_delay) {
	suggestion_delay = MAX(0.1, p_delay);
}

float AICodeCompletion::get_suggestion_delay() const {
	return suggestion_delay;
}

PackedStringArray AICodeCompletion::get_suggestions(const String &p_code, int p_cursor_position) {
	PackedStringArray suggestions;

	if (!enabled) {
		return suggestions;
	}

	// Extract context around cursor
	String context = p_code.substr(0, p_cursor_position);
	
	// Get AI suggestions
	AIAssistant *ai = AIAssistant::get_singleton();
	if (ai && ai->is_initialized()) {
		PackedStringArray recent_code;
		recent_code.push_back(context);
		
		String completion = ai->get_code_completion(context, recent_code);
		if (!completion.is_empty()) {
			suggestions.push_back(completion);
		}
	}

	return suggestions;
}

AICodeCompletion::AICodeCompletion() {
}

AICodeCompletion::~AICodeCompletion() {
}
