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

#include "unity_exporter.h"

void AeThexUnityExporter::_bind_methods() {
}

AeThexUnityExporter::AeThexUnityExporter() {
}

AeThexUnityExporter::~AeThexUnityExporter() {
}

AeThexPlatformExporter::ExportResult AeThexUnityExporter::compile_script(const String &p_source, String &r_output) {
	String output;
	
	output += "// Generated by AeThex Engine\n";
	output += "// Target: Unity/C#\n\n";
	output += "using UnityEngine;\n";
	output += "using System.Collections;\n";
	output += "using System.Collections.Generic;\n\n";
	
	String converted = _convert_to_csharp(p_source);
	output += converted;
	
	r_output = output;
	return RESULT_SUCCESS;
}

String AeThexUnityExporter::_convert_to_csharp(const String &p_source) {
	String result = p_source;
	
	// AeThex -> C# keyword mappings
	result = result.replace("reality ", "public class ");
	result = result.replace("journey ", "public void ");
	result = result.replace("beacon ", "public ");
	result = result.replace("artifact ", "var ");
	result = result.replace("reveal(", "Debug.Log(");
	result = result.replace("notify(", "// Event: ");
	
	// Types
	result = result.replace(": Number", " float");
	result = result.replace(": String", " string");
	result = result.replace(": Boolean", " bool");
	result = result.replace(": Array", " List<object>");
	result = result.replace(": Vector2", " Vector2");
	result = result.replace(": Vector3", " Vector3");
	
	// Control flow
	result = result.replace("sync across {", "StartCoroutine(AsyncOperation());");
	
	// Array methods
	result = result.replace(".add(", ".Add(");
	result = result.replace(".remove(", ".Remove(");
	result = result.replace(".length", ".Count");
	
	// Boolean values  
	result = result.replace("true", "true");
	result = result.replace("false", "false");
	result = result.replace(" and ", " && ");
	result = result.replace(" or ", " || ");
	result = result.replace(" not ", " !");
	
	return result;
}

String AeThexUnityExporter::_generate_csharp_class(const String &p_name, const String &p_body) {
	String output;
	output += "public class " + p_name + " : MonoBehaviour\n";
	output += "{\n";
	output += p_body;
	output += "}\n";
	return output;
}
