<?php
/**
 * Native Elementor renderer for Angie-style reference landing pages.
 *
 * @package Niwa_AI
 */

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

class Niwa_AI_Elementor_Angie_Reference_Page_Renderer {
	public function build( $blueprint ) {
		$sections = isset( $blueprint['sections'] ) && is_array( $blueprint['sections'] ) ? $blueprint['sections'] : array();
		$elements = array();

		foreach ( array_values( $sections ) as $section ) {
			$type = isset( $section['type'] ) ? sanitize_key( (string) $section['type'] ) : '';
			switch ( $type ) {
				case 'angie_hero':
					$elements[] = $this->hero( $section );
					break;
				case 'angie_capabilities':
					$elements[] = $this->capabilities( $section );
					break;
				case 'angie_confidence':
					$elements[] = $this->confidence( $section );
					break;
				case 'angie_super_admin':
					$elements[] = $this->super_admin( $section );
					break;
				case 'angie_steps':
					$elements[] = $this->steps( $section );
					break;
				case 'angie_quote':
					$elements[] = $this->quote( $section );
					break;
				case 'angie_resources':
					$elements[] = $this->resources( $section );
					break;
				case 'faq':
					$elements[] = $this->faq( $section );
					break;
				case 'angie_cta':
					$elements[] = $this->cta( $section );
					break;
			}
		}

		return array_values( array_filter( $elements ) );
	}

	private function hero( $section ) {
		$left = array_merge(
			$this->intro_widgets( $section, 'left', 'hero' ),
			$this->button_widgets( $section['buttons'], 'left' )
		);
		$right = array( $this->image_widget( $section['image'], 'full' ) );
		foreach ( array_slice( $this->cards( $section ), 0, 3 ) as $card ) {
			$right[] = $this->mini_card( $card );
		}

		return $this->section(
			'#eee7ff',
			array(
					$this->container(
						$this->row_settings(),
						array(
						$this->container( $this->column_settings( 45 ), $left ),
						$this->container( $this->column_settings( 48 ), $right ),
					)
				),
			),
			array( 'padding' => $this->dimensions( 112, 28, 92, 28 ) )
		);
	}

	private function capabilities( $section ) {
		$children = $this->intro_widgets( $section, 'center' );
		$cards    = array();
		foreach ( $this->cards( $section ) as $card ) {
			$cards[] = $this->container(
				array_merge( $this->card_settings( 31 ), array( 'min_height' => array( 'unit' => 'px', 'size' => 310 ) ) ),
				array_filter(
					array(
						! empty( $card['image']['url'] ) ? $this->image_widget( $card['image'], 'medium' ) : null,
						$this->heading_widget( $card['title'], 'h3', 'left', '#111827' ),
						$this->text_widget( $card['body'], 'left', '#4b5563' ),
					)
				)
			);
		}
		$children[] = $this->container( $this->grid_settings(), $cards );
		return $this->section( '#ffffff', $children );
	}

	private function confidence( $section ) {
		$children = $this->intro_widgets( $section, 'center' );
		$cards    = array();
		foreach ( $this->cards( $section ) as $index => $card ) {
			$cards[] = $this->container(
				array_merge(
					$this->card_settings( 31 ),
					array(
						'background_color' => 1 === $index ? '#111827' : '#ffffff',
						'padding'          => $this->dimensions( 30, 26, 30, 26 ),
					)
				),
				array(
					$this->heading_widget( $card['title'], 'h3', 'left', 1 === $index ? '#ffffff' : '#111827' ),
					$this->text_widget( $card['body'], 'left', 1 === $index ? 'rgba(255,255,255,.72)' : '#4b5563' ),
				)
			);
		}
		$children[] = $this->container( $this->grid_settings(), $cards );
		return $this->section( '#f7f4ff', $children );
	}

	private function super_admin( $section ) {
		$children = array(
					$this->container(
						$this->row_settings(),
						array(
					$this->container( $this->column_settings( 40 ), array_merge( $this->intro_widgets( $section, 'left', 'dark' ), $this->button_widgets( $section['buttons'], 'left', 'dark' ) ) ),
					$this->container(
						$this->column_settings( 50 ),
						array_map( array( $this, 'dark_feature_card' ), $this->cards( $section ) )
					),
				)
			),
		);
		return $this->section( '#111827', $children, array( 'padding' => $this->dimensions( 94, 28, 94, 28 ) ) );
	}

	private function steps( $section ) {
		$children = $this->intro_widgets( $section, 'center' );
		$steps    = array();
		foreach ( $this->cards( $section ) as $index => $card ) {
			$steps[] = $this->container(
				$this->card_settings( 31 ),
				array(
					$this->heading_widget( '0' . ( $index + 1 ), 'h6', 'left', '#7c3aed' ),
					$this->heading_widget( $card['title'], 'h3', 'left', '#111827' ),
					$this->text_widget( $card['body'], 'left', '#4b5563' ),
				)
			);
		}
		$children[] = $this->container( $this->grid_settings(), $steps );
		return $this->section( '#ffffff', $children );
	}

	private function quote( $section ) {
		return $this->section(
			'#eee7ff',
			array(
				$this->heading_widget( $section['heading'], 'h2', 'center', '#111827' ),
				$this->text_widget( $section['body'], 'center', '#6b7280' ),
			),
			array( 'boxed_width' => array( 'unit' => 'px', 'size' => 980 ) )
		);
	}

	private function resources( $section ) {
		$children = $this->intro_widgets( $section, 'center' );
		$cards    = array();
		foreach ( $this->cards( $section ) as $card ) {
			$cards[] = $this->container(
				$this->card_settings( 31 ),
				array(
					$this->heading_widget( $card['title'], 'h3', 'left', '#111827' ),
					$this->text_widget( $card['body'], 'left', '#4b5563' ),
					$this->button_widget( 'Learn more', '#', 'left', false ),
				)
			);
		}
		$children[] = $this->container( $this->grid_settings(), $cards );
		return $this->section( '#f8f7fb', $children );
	}

	private function faq( $section ) {
		$children = $this->intro_widgets( $section, 'center' );
		$tabs     = array();
		foreach ( $this->cards( $section ) as $card ) {
			$tabs[] = array(
				'_id'         => $this->element_id(),
				'tab_title'  => $card['title'],
				'tab_content' => wp_kses_post( wpautop( $card['body'] ) ),
			);
		}
		$children[] = $this->widget(
			'toggle',
			array(
				'tabs'              => $tabs,
				'icon'              => array( 'value' => 'fas fa-plus', 'library' => 'fa-solid' ),
				'active_icon'       => array( 'value' => 'fas fa-minus', 'library' => 'fa-solid' ),
				'tab_title_color'   => '#111827',
				'tab_active_color'  => '#7c3aed',
				'content_color'     => '#4b5563',
				'border_color'      => '#e5e7eb',
				'background_color'  => '#ffffff',
			)
		);
		return $this->section( '#ffffff', $children, array( 'boxed_width' => array( 'unit' => 'px', 'size' => 940 ) ) );
	}

	private function cta( $section ) {
		$children = array_merge( $this->intro_widgets( $section, 'center' ), $this->button_widgets( $section['buttons'], 'center' ) );
		return $this->section( '#111827', $children, array( 'boxed_width' => array( 'unit' => 'px', 'size' => 980 ), 'padding' => $this->dimensions( 96, 28, 96, 28 ), 'dark' => true ) );
	}

	private function mini_card( $card ) {
		return $this->container(
			array_merge( $this->card_settings( 100 ), array( 'padding' => $this->dimensions( 18, 18, 18, 18 ) ) ),
			array(
				$this->heading_widget( $card['title'], 'h4', 'left', '#111827' ),
				$this->text_widget( $card['body'], 'left', '#4b5563' ),
			)
		);
	}

	private function dark_feature_card( $card ) {
		return $this->container(
			array(
				'content_width'      => 'full',
				'padding'            => $this->dimensions( 24, 24, 24, 24 ),
				'background_background' => 'classic',
				'background_color'   => 'rgba(255,255,255,.08)',
				'border_radius'      => $this->dimensions( 18, 18, 18, 18 ),
				'border_border'      => 'solid',
				'border_width'       => $this->dimensions( 1, 1, 1, 1 ),
				'border_color'       => 'rgba(255,255,255,.12)',
				'margin'             => $this->dimensions( 0, 0, 16, 0 ),
			),
			array(
				$this->heading_widget( $card['title'], 'h3', 'left', '#ffffff' ),
				$this->text_widget( $card['body'], 'left', 'rgba(255,255,255,.72)' ),
			)
		);
	}

	private function section( $background, $children, $overrides = array() ) {
		$settings = array_merge(
			array(
				'content_width'      => 'boxed',
				'boxed_width'        => array( 'unit' => 'px', 'size' => 1200 ),
				'padding'            => $this->dimensions( 86, 28, 86, 28 ),
				'padding_mobile'     => $this->dimensions( 56, 18, 56, 18 ),
				'background_background' => 'classic',
				'background_color'   => $background,
				'flex_direction'     => 'column',
				'gap'                => array( 'unit' => 'px', 'size' => 26 ),
			),
			$overrides
		);

		if ( ! empty( $settings['dark'] ) ) {
			unset( $settings['dark'] );
			$children = $this->darken_intro_children( $children );
		}

		return $this->container( $settings, $children );
	}

	private function darken_intro_children( $children ) {
		foreach ( $children as &$child ) {
			if ( isset( $child['widgetType'] ) && in_array( $child['widgetType'], array( 'heading', 'text-editor' ), true ) ) {
				if ( 'heading' === $child['widgetType'] ) {
					$child['settings']['title_color'] = '#ffffff';
				} else {
					$child['settings']['text_color'] = 'rgba(255,255,255,.74)';
				}
			}
		}
		return $children;
	}

	private function intro_widgets( $section, $align = 'center', $mode = 'light' ) {
		$dark = in_array( $mode, array( 'dark' ), true );
		$hero = 'hero' === $mode;
		$items = array();
		if ( ! empty( $section['eyebrow'] ) ) {
			$items[] = $this->heading_widget( $section['eyebrow'], 'h6', $align, $dark ? '#c4b5fd' : '#7c3aed' );
		}
		if ( ! empty( $section['heading'] ) ) {
			$items[] = $this->heading_widget( $section['heading'], $hero ? 'h1' : 'h2', $align, $dark ? '#ffffff' : '#111827' );
		}
		if ( ! empty( $section['body'] ) ) {
			$items[] = $this->text_widget( $section['body'], $align, $dark ? 'rgba(255,255,255,.72)' : '#4b5563' );
		}
		return $items;
	}

	private function button_widgets( $buttons, $align = 'left', $mode = 'light' ) {
		$items = array();
		foreach ( is_array( $buttons ) ? $buttons : array() as $index => $button ) {
			$items[] = $this->button_widget(
				isset( $button['text'] ) ? (string) $button['text'] : 'Learn more',
				isset( $button['url'] ) ? (string) $button['url'] : '#',
				$align,
				0 === $index,
				'dark' === $mode
			);
		}
		return $items;
	}

	private function button_widget( $text, $url, $align, $primary = true, $dark = false ) {
		return $this->widget(
			'button',
			array(
				'text'              => $text,
				'link'              => array( 'url' => $url, 'is_external' => false, 'nofollow' => false ),
				'align'             => $align,
				'size'              => 'md',
				'button_type'       => 'info',
				'background_color'  => $primary ? '#7c3aed' : ( $dark ? 'rgba(255,255,255,.08)' : '#ffffff' ),
				'button_text_color' => $primary ? '#ffffff' : ( $dark ? '#ffffff' : '#111827' ),
				'border_border'     => 'solid',
				'border_width'      => $this->dimensions( 1, 1, 1, 1 ),
				'border_color'      => $primary ? '#7c3aed' : 'rgba(124,58,237,.24)',
				'border_radius'     => $this->dimensions( 999, 999, 999, 999 ),
			)
		);
	}

	private function heading_widget( $title, $size, $align, $color ) {
		return $this->widget(
			'heading',
			array(
				'title'       => wp_kses_post( (string) $title ),
				'header_size' => $size,
				'align'       => $align,
				'title_color' => $color,
			)
		);
	}

	private function text_widget( $text, $align, $color ) {
		return $this->widget(
			'text-editor',
			array(
				'editor'     => wp_kses_post( wpautop( (string) $text ) ),
				'align'      => $align,
				'text_color' => $color,
			)
		);
	}

	private function image_widget( $image, $size = 'large' ) {
		return $this->widget(
			'image',
			array(
				'image'      => array( 'id' => '', 'url' => isset( $image['url'] ) ? (string) $image['url'] : '' ),
				'image_size' => $size,
				'alt'        => isset( $image['alt'] ) ? (string) $image['alt'] : '',
			)
		);
	}

	private function cards( $section ) {
		return isset( $section['cards'] ) && is_array( $section['cards'] ) ? array_values( $section['cards'] ) : array();
	}

	private function row_settings() {
		return array(
			'content_width'          => 'full',
			'flex_direction'         => 'row',
			'flex_direction_mobile'  => 'column',
			'flex_wrap'              => 'nowrap',
			'flex_wrap_mobile'       => 'wrap',
			'justify_content'        => 'space-between',
			'align_items'            => 'center',
			'gap'                    => array( 'unit' => 'px', 'size' => 24 ),
			'gap_mobile'             => array( 'unit' => 'px', 'size' => 18 ),
		);
	}

	private function column_settings( $width ) {
		return array(
			'content_width'  => 'full',
			'width'          => array( 'unit' => '%', 'size' => $width ),
			'width_mobile'   => array( 'unit' => '%', 'size' => 100 ),
			'flex_direction' => 'column',
			'gap'            => array( 'unit' => 'px', 'size' => 18 ),
		);
	}

	private function grid_settings() {
		return array(
			'content_width'  => 'full',
			'flex_direction' => 'row',
			'flex_wrap'      => 'wrap',
			'gap'            => array( 'unit' => 'px', 'size' => 20 ),
			'padding'        => $this->dimensions( 22, 0, 0, 0 ),
		);
	}

	private function card_settings( $width ) {
		return array(
			'content_width'      => 'full',
			'width'              => array( 'unit' => '%', 'size' => $width ),
			'width_mobile'       => array( 'unit' => '%', 'size' => 100 ),
			'padding'            => $this->dimensions( 24, 22, 24, 22 ),
			'background_background' => 'classic',
			'background_color'   => '#ffffff',
			'border_radius'      => $this->dimensions( 18, 18, 18, 18 ),
			'border_border'      => 'solid',
			'border_width'       => $this->dimensions( 1, 1, 1, 1 ),
			'border_color'       => '#e8e3f8',
		);
	}

	private function container( $settings, $elements ) {
		return array(
			'id'       => $this->element_id(),
			'elType'   => 'container',
			'settings' => is_array( $settings ) ? $settings : array(),
			'elements' => is_array( $elements ) ? array_values( $elements ) : array(),
			'isInner'  => false,
		);
	}

	private function widget( $type, $settings ) {
		return array(
			'id'         => $this->element_id(),
			'elType'     => 'widget',
			'settings'   => is_array( $settings ) ? $settings : array(),
			'elements'   => array(),
			'widgetType' => sanitize_key( (string) $type ),
		);
	}

	private function dimensions( $top, $right, $bottom, $left, $unit = 'px' ) {
		return array(
			'unit'     => $unit,
			'top'      => (string) $top,
			'right'    => (string) $right,
			'bottom'   => (string) $bottom,
			'left'     => (string) $left,
			'isLinked' => false,
		);
	}

	private function element_id() {
		return substr( md5( uniqid( 'niwa_angie_', true ) ), 0, 7 );
	}
}
