<?php
/**
 * Runtime for published shortcode/visual snippets.
 *
 * @package Niwa_AI
 */

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

class Niwa_AI_Shortcode_Runtime {
	private $repository;
	private $printed_assets = array();

	public function __construct( Niwa_AI_Snippet_Repository $repository ) {
		$this->repository = $repository;
	}

	public function register_hooks() {
		add_action( 'init', array( $this, 'register_shortcodes' ), 30 );
		add_action( 'init', array( $this, 'load_live_admin_snippets' ), 5 );
	}

	public function register_shortcodes() {
		add_shortcode( 'niwa_snippet', array( $this, 'render_shortcode' ) );
		foreach ( $this->repository->get_published_shortcode_snippets() as $snippet ) {
			add_shortcode(
				'niwa_snippet_' . intval( $snippet['id'] ),
				function () use ( $snippet ) {
					return $this->render_shortcode( array( 'id' => intval( $snippet['id'] ) ) );
				}
			);
		}
	}

	public function render_shortcode( $atts ) {
		$atts = shortcode_atts( array( 'id' => 0 ), is_array( $atts ) ? $atts : array(), 'niwa_snippet' );
		$snippet = $this->repository->get_snippet( intval( $atts['id'] ), true );
		if ( ! $snippet || 'live' !== (string) $snippet['status'] ) {
			return '';
		}

		$template = $this->get_file_content( $snippet, 'template.html' );
		if ( '' === trim( $template ) ) {
			return '';
		}

		$html  = '<div class="niwa-generated-snippet" data-niwa-snippet="' . esc_attr( (string) $snippet['id'] ) . '">';
		$html .= wp_kses_post( $template );
		$html .= '</div>';
		$html .= $this->render_inline_assets( $snippet );
		return $html;
	}

	public function load_live_admin_snippets() {
		if ( ! is_admin() || ! current_user_can( 'manage_options' ) ) {
			return;
		}
		foreach ( $this->repository->get_published_shortcode_snippets() as $snippet ) {
			if ( Niwa_AI_Template_Library_Types::TYPE_WORDPRESS_ADMIN_SNIPPET !== (string) $snippet['type'] ) {
				continue;
			}
			$path = trailingslashit( WP_CONTENT_DIR ) . 'niwa-snippets/prod/snippet-' . intval( $snippet['id'] ) . '/main.php';
			if ( file_exists( $path ) ) {
				include_once $path;
			}
		}
	}

	private function render_inline_assets( $snippet ) {
		$id = intval( $snippet['id'] );
		if ( isset( $this->printed_assets[ $id ] ) ) {
			return '';
		}
		$this->printed_assets[ $id ] = true;
		$css = $this->get_file_content( $snippet, 'style.css' );
		$js  = $this->get_file_content( $snippet, 'script.js' );

		$out = '';
		if ( '' !== trim( $css ) ) {
			$out .= '<style id="niwa-snippet-style-' . esc_attr( (string) $id ) . '">' . wp_strip_all_tags( $css ) . '</style>';
		}
		if ( '' !== trim( $js ) ) {
			$out .= '<script id="niwa-snippet-script-' . esc_attr( (string) $id ) . '">(function(){' . $js . '})();</script>';
		}
		return $out;
	}

	private function get_file_content( $snippet, $name ) {
		$files = isset( $snippet['files'] ) && is_array( $snippet['files'] ) ? $snippet['files'] : array();
		foreach ( $files as $file ) {
			if ( isset( $file['name'] ) && strtolower( (string) $file['name'] ) === strtolower( (string) $name ) ) {
				return isset( $file['content'] ) ? (string) $file['content'] : '';
			}
		}
		return '';
	}
}
