<?php
/**
 * Wires the local Niwa template/snippet library.
 *
 * @package Niwa_AI
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Niwa_AI_Template_Library_Module {
	private $templates;
	private $snippets;
	private $elementor;
	private $shortcode_runtime;
	private $rest_controller;
	private $template_page;
	private $snippets_page;
	private $seeder;
	private $generator;
	private $validator;
	private $filesystem;

	public function __construct() {
		$this->templates         = new Niwa_AI_Template_Repository();
		$this->snippets          = new Niwa_AI_Snippet_Repository();
		$this->generator         = new Niwa_AI_Template_AI_Generator();
		$this->validator         = new Niwa_AI_Snippet_Validator();
		$this->filesystem        = new Niwa_AI_Snippet_Filesystem();
		$this->elementor         = new Niwa_AI_Elementor_Adapter( $this->snippets );
		$this->shortcode_runtime = new Niwa_AI_Shortcode_Runtime( $this->snippets );
		$this->rest_controller   = new Niwa_AI_Template_Library_REST_Controller( $this->templates, $this->snippets, $this->generator, $this->validator, $this->filesystem );
		$this->template_page     = new Niwa_AI_Template_Library_Page( $this->templates, $this->elementor );
		$this->snippets_page     = new Niwa_AI_Snippets_Page( $this->snippets );
		$this->seeder            = new Niwa_AI_Template_Seeder( $this->templates );
	}

	public function set_llm_dependencies( $openai_client, $auth_manager, $model_router ) {
		$this->generator = new Niwa_AI_Template_AI_Generator( $openai_client, $auth_manager, $model_router, new Niwa_AI_Template_Elementor_Builder() );
		$this->rest_controller = new Niwa_AI_Template_Library_REST_Controller( $this->templates, $this->snippets, $this->generator, $this->validator, $this->filesystem );
	}

	public function register_hooks() {
		( new Niwa_AI_Template_Post_Type() )->register_hooks();
		( new Niwa_AI_Snippet_Post_Type() )->register_hooks();
		$this->seeder->register_hooks();
		$this->shortcode_runtime->register_hooks();
		$this->elementor->register_hooks();
		$this->rest_controller->register_hooks();
		if ( is_admin() ) {
			$this->template_page->register_hooks();
			$this->snippets_page->register_hooks();
		}
	}

	public function get_snippets() {
		return $this->snippets->list_snippets( '', 200, false );
	}

	public function get_repositories() {
		return array(
			'templates' => $this->templates,
			'snippets'  => $this->snippets,
		);
	}
}
