/**************************************************************************/
/*  roblox_exporter.cpp                                                   */
/**************************************************************************/
/*                         This file is part of:                          */
/*                          AETHEX ENGINE                                 */
/*                     https://aethex.foundation                          */
/**************************************************************************/
/* Copyright (c) 2026-present AeThex Labs.                                */
/**************************************************************************/

#include "roblox_exporter.h"

#include "core/io/file_access.h"

void AeThexRobloxExporter::_bind_methods() {
}

AeThexRobloxExporter::AeThexRobloxExporter() {
}

AeThexRobloxExporter::~AeThexRobloxExporter() {
}

AeThexPlatformExporter::ExportResult AeThexRobloxExporter::export_project(const String &p_output_path) {
	_log_info("Exporting to Roblox/Luau...");
	
	// Create Roblox-specific structure
	String scripts_path = p_output_path.path_join("src/server");
	String shared_path = p_output_path.path_join("src/shared");
	String client_path = p_output_path.path_join("src/client");
	
	// Call base export
	ExportResult result = AeThexPlatformExporter::export_project(scripts_path);
	
	// Generate Rojo project file
	if (result == RESULT_SUCCESS) {
		String rojo_content = _generate_rojo_project();
		Ref<FileAccess> rojo_file = FileAccess::open(p_output_path.path_join("default.project.json"), FileAccess::WRITE);
		if (rojo_file.is_valid()) {
			rojo_file->store_string(rojo_content);
		}
	}
	
	return result;
}

AeThexPlatformExporter::ExportResult AeThexRobloxExporter::compile_script(const String &p_source, String &r_output) {
	// Convert AeThex syntax to Luau
	String output;
	
	output += "-- Generated by AeThex Engine\n";
	output += "-- Target: Roblox/Luau\n\n";
	
	String converted = p_source;
	converted = _convert_keywords_to_luau(converted);
	converted = _convert_types_to_luau(converted);
	
	output += converted;
	
	r_output = output;
	return RESULT_SUCCESS;
}

String AeThexRobloxExporter::_generate_module_header(const String &p_name) {
	String header;
	header += "--!strict\n";
	header += "-- Module: " + p_name + "\n\n";
	header += "local " + p_name + " = {}\n\n";
	return header;
}

String AeThexRobloxExporter::_generate_module_footer() {
	return "\nreturn module\n";
}

String AeThexRobloxExporter::_convert_keywords_to_luau(const String &p_code) {
	String result = p_code;
	
	// AeThex -> Luau keyword mappings
	result = result.replace("reality ", "local ");
	result = result.replace("journey ", "function ");
	result = result.replace("beacon ", "local ");
	result = result.replace("artifact ", "local ");
	result = result.replace("reveal(", "print(");
	result = result.replace("notify(", "-- Event: ");
	
	// Control flow
	result = result.replace("sync across {", "task.spawn(function()");
	result = result.replace("}", "end)");
	
	// Types
	result = result.replace(": Number", ": number");
	result = result.replace(": String", ": string");
	result = result.replace(": Boolean", ": boolean");
	result = result.replace(": Array", ": {any}");
	result = result.replace(": Vector2", ": Vector2");
	result = result.replace(": Vector3", ": Vector3");
	
	// Method syntax
	result = result.replace(".add(", ":insert(");
	result = result.replace(".remove(", ":remove(");
	result = result.replace(".length", "#");
	
	return result;
}

String AeThexRobloxExporter::_convert_types_to_luau(const String &p_code) {
	String result = p_code;
	
	// Vector constructors
	result = result.replace("Vector2(", "Vector2.new(");
	result = result.replace("Vector3(", "Vector3.new(");
	result = result.replace("Color(", "Color3.new(");
	
	return result;
}

String AeThexRobloxExporter::_generate_rojo_project() {
	String json;
	json += "{\n";
	json += "  \"name\": \"" + (config.is_valid() ? config->get_project_name() : "AeThexGame") + "\",\n";
	json += "  \"tree\": {\n";
	json += "    \"$className\": \"DataModel\",\n";
	json += "    \"ReplicatedStorage\": {\n";
	json += "      \"$className\": \"ReplicatedStorage\",\n";
	json += "      \"Shared\": {\n";
	json += "        \"$path\": \"src/shared\"\n";
	json += "      }\n";
	json += "    },\n";
	json += "    \"ServerScriptService\": {\n";
	json += "      \"$className\": \"ServerScriptService\",\n";
	json += "      \"Server\": {\n";
	json += "        \"$path\": \"src/server\"\n";
	json += "      }\n";
	json += "    },\n";
	json += "    \"StarterPlayer\": {\n";
	json += "      \"$className\": \"StarterPlayer\",\n";
	json += "      \"StarterPlayerScripts\": {\n";
	json += "        \"$className\": \"StarterPlayerScripts\",\n";
	json += "        \"Client\": {\n";
	json += "          \"$path\": \"src/client\"\n";
	json += "        }\n";
	json += "      }\n";
	json += "    }\n";
	json += "  }\n";
	json += "}\n";
	return json;
}
