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

#ifndef AETHEX_TEMPLATE_MANAGER_H
#define AETHEX_TEMPLATE_MANAGER_H

#include "core/io/http_client.h"
#include "core/object/object.h"
#include "core/object/ref_counted.h"
#include "template_data.h"

// Manages AeThex project templates - loading, downloading, and instantiation
class AeThexTemplateManager : public Object {
	GDCLASS(AeThexTemplateManager, Object);

public:
	static const String TEMPLATES_API_URL;
	static const String TEMPLATES_CACHE_PATH;

private:
	static AeThexTemplateManager *singleton;
	
	Vector<Ref<AeThexTemplate>> builtin_templates;
	Vector<Ref<AeThexTemplate>> downloaded_templates;
	Vector<Ref<AeThexTemplate>> online_templates;
	
	HTTPClient *http_client = nullptr;
	bool is_fetching = false;
	
	void _init_builtin_templates();
	void _load_downloaded_templates();
	void _cache_template(const Ref<AeThexTemplate> &p_template);

protected:
	static void _bind_methods();

public:
	static AeThexTemplateManager *get_singleton();
	
	// Template retrieval
	Vector<Ref<AeThexTemplate>> get_all_templates() const;
	Vector<Ref<AeThexTemplate>> get_builtin_templates() const { return builtin_templates; }
	Vector<Ref<AeThexTemplate>> get_downloaded_templates() const { return downloaded_templates; }
	Vector<Ref<AeThexTemplate>> get_online_templates() const { return online_templates; }
	
	Ref<AeThexTemplate> get_template_by_id(const String &p_id) const;
	
	// Filtering
	Vector<Ref<AeThexTemplate>> get_templates_by_category(AeThexTemplate::TemplateCategory p_category) const;
	Vector<Ref<AeThexTemplate>> get_templates_by_platform(AeThexTemplate::TargetPlatform p_platform) const;
	Vector<Ref<AeThexTemplate>> get_templates_by_difficulty(AeThexTemplate::Difficulty p_difficulty) const;
	Vector<Ref<AeThexTemplate>> search_templates(const String &p_query) const;
	
	// Online operations
	void fetch_online_templates();
	void download_template(const String &p_template_id);
	bool is_template_downloaded(const String &p_template_id) const;
	
	// Project creation
	Error create_project_from_template(const Ref<AeThexTemplate> &p_template, 
									   const String &p_project_path,
									   const Dictionary &p_variables);
	
	// Variable processing
	String process_template_string(const String &p_template, const Dictionary &p_variables);
	
	AeThexTemplateManager();
	~AeThexTemplateManager();
};

// ===========================================================
// Built-in Template Definitions
// ===========================================================

namespace AeThexBuiltinTemplates {

// Create the starter game templates
Ref<AeThexTemplate> create_empty_project();
Ref<AeThexTemplate> create_2d_platformer();
Ref<AeThexTemplate> create_3d_fps();
Ref<AeThexTemplate> create_rpg_starter();
Ref<AeThexTemplate> create_multiplayer_game();
Ref<AeThexTemplate> create_roblox_experience();
Ref<AeThexTemplate> create_uefn_island();
Ref<AeThexTemplate> create_unity_mobile();
Ref<AeThexTemplate> create_web_game();
Ref<AeThexTemplate> create_crossplatform_game();

} // namespace AeThexBuiltinTemplates

#endif // AETHEX_TEMPLATE_MANAGER_H
