<?php
/**
 * Curated native-Elementor blueprints for complex public reference pages.
 *
 * @package Niwa_AI
 */

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

class Niwa_AI_Elementor_Reference_Blueprints {
	public static function for_url( $url, $instructions = '' ) {
		$parts = wp_parse_url( (string) $url );
		$host  = isset( $parts['host'] ) ? strtolower( preg_replace( '/^www\./', '', (string) $parts['host'] ) ) : '';
		$path  = isset( $parts['path'] ) ? strtolower( (string) $parts['path'] ) : '';

		if ( 'elementor.com' === $host && false !== strpos( $path, '/products/angie-ai-for-wordpress' ) ) {
			return self::angie_ai_for_wordpress( $url, $instructions );
		}

		return array();
	}

	private static function angie_ai_for_wordpress( $url, $instructions ) {
		return array(
			'source_url'        => esc_url_raw( (string) $url ),
			'title'             => 'Niwa AI for WordPress',
			'description'       => 'Niwa-branded native Elementor rebuild of the Angie AI reference page structure.',
			'instructions'      => self::trim( $instructions, 500 ),
			'style_profile'     => 'angie_ai_product',
			'specialized_blueprint' => true,
			'llm_used'          => false,
			'llm_error'         => '',
			'sections'          => array(
				array(
					'type'    => 'angie_hero',
					'eyebrow' => 'Agentic AI for WordPress',
					'heading' => 'Niwa: AI built for WordPress sites',
					'body'    => 'Build custom WordPress assets and manage your site through one chat interface. Describe the page, widget, snippet, app, or admin task, and Niwa turns it into editable WordPress output.',
					'image'   => self::image( 'niwa-angie-hero.svg', 'Niwa AI WordPress builder interface mockup' ),
					'buttons' => array(
						array( 'text' => 'Open Niwa AI', 'url' => admin_url( 'admin.php?page=niwa-ai' ) ),
						array( 'text' => 'Build with Elementor', 'url' => admin_url( 'admin.php?page=niwa-ai-template-library' ) ),
					),
					'cards'   => array(
						self::card( 'Build with Niwa Code', 'Create production-ready Elementor widgets, admin snippets, visual apps, and structured content tools.' ),
						self::card( 'Manage with Super Admin Mode', 'Run bulk operations, inspect errors, and improve SEO or content workflows from chat.' ),
						self::card( 'Built for your site context', 'Niwa reads active plugins, theme styling, page structure, WooCommerce data, and Elementor content before editing.' ),
					),
				),
				array(
					'type'    => 'angie_capabilities',
					'eyebrow' => 'Niwa Code',
					'heading' => 'Your AI-powered expert WordPress developer',
					'body'    => 'From custom widgets to visual WordPress apps, Niwa builds specialized site assets that remain editable inside Elementor and reusable across workflows.',
					'cards'   => array(
						self::card( 'Elementor widgets', 'Create custom sections and widgets with native Elementor controls, repeaters, editable copy, media, spacing, and style settings.', 'niwa-angie-widgets.svg' ),
						self::card( 'WordPress admin snippets', 'Create dashboard views, settings panels, bulk tools, and admin-side utilities that match the current WordPress stack.', 'niwa-angie-admin.svg' ),
						self::card( 'Website snippets', 'Add interactive behaviours, hover effects, light animations, content blocks, and UX helpers where Elementor is not enough.', 'niwa-angie-website.svg' ),
						self::card( 'Visual apps', 'Build calculators, quizzes, trackers, finders, games, and other front-end mini apps with usable state and responsive layouts.', 'niwa-angie-visual-app.svg' ),
						self::card( 'Custom post types and fields', 'Generate structured content models with fields, admin lists, and frontend display patterns for real site data.', 'niwa-angie-cpt.svg' ),
					),
				),
				array(
					'type'    => 'angie_confidence',
					'eyebrow' => 'Built for confidence and scale',
					'heading' => 'Create, refine, reuse, and roll back without losing control',
					'body'    => 'Niwa keeps changes inspectable through Elementor data, page snapshots, and rollback-aware tool results so generated work can be tested and refined.',
					'cards'   => array(
						self::card( 'Build with rollback', 'Preview generated sections, inspect the structure, and undo page-data changes from Niwa history.' ),
						self::card( 'Reuse your creations', 'Save reusable templates and snippets, then adapt them to different pages or sites with new copy and styling.' ),
						self::card( 'Talk to Niwa your way', 'Use chat, admin UI, Telegram, WhatsApp, screenshots, or URLs as input when directing real WordPress work.' ),
					),
				),
				array(
					'type'    => 'angie_super_admin',
					'eyebrow' => 'Super Admin Mode',
					'heading' => 'Take full control of the WordPress stack',
					'body'    => 'Niwa can inspect real site data before acting, then execute precise Elementor, WooCommerce, content, SEO, cache, and maintenance actions through approved tools.',
					'buttons' => array(
						array( 'text' => 'Run in Niwa chat', 'url' => admin_url( 'admin.php?page=niwa-ai' ) ),
					),
					'cards'   => array(
						self::card( 'Bulk operations at scale', 'Update content, products, metadata, taxonomy, or layout structure across many records after reading the current data model.' ),
						self::card( 'Debug errors', 'Trace plugin conflicts, runtime logs, failed webhooks, styling mismatches, and broken Elementor sections from one workflow.' ),
						self::card( 'SEO and maintenance at scale', 'Create missing metadata, redirect plans, internal linking improvements, and cleanup routines that fit the live site.' ),
					),
				),
				array(
					'type'    => 'angie_steps',
					'eyebrow' => 'Get started in minutes',
					'heading' => 'Describe the outcome and let Niwa build the first version',
					'body'    => 'The same flow works for one widget, one section, or a complete multi-section reference page rebuild.',
					'cards'   => array(
						self::card( 'Step 1 - Choose the target', 'Give Niwa a page, template, product, URL, screenshot, or blank goal.' ),
						self::card( 'Step 2 - Prompt and build', 'Niwa translates the request into native WordPress and Elementor operations.' ),
						self::card( 'Step 3 - Review and refine', 'Edit generated Elementor widgets directly or ask Niwa to adjust spacing, copy, hierarchy, and behavior.' ),
					),
				),
				array(
					'type'    => 'angie_quote',
					'eyebrow' => 'Agency workflow',
					'heading' => '"The result must be editable, inspectable, and refinable like real Elementor work."',
					'body'    => 'Niwa reference build QA',
				),
				array(
					'type'    => 'angie_resources',
					'eyebrow' => 'More ways to grow with AI',
					'heading' => 'Give Niwa more context, then let it operate inside WordPress',
					'cards'   => array(
						self::card( 'Prompt library', 'Store practical build prompts for templates, widgets, snippets, and admin tools.' ),
						self::card( 'MCP-ready workflows', 'Expose site capabilities as callable tools so Niwa can work with real data and safer boundaries.' ),
						self::card( 'AI-ready sites', 'Prepare themes, content structure, and page systems so future builds are faster and more accurate.' ),
					),
				),
				array(
					'type'    => 'faq',
					'eyebrow' => 'FAQ',
					'heading' => 'Questions about Niwa Elementor builds',
					'cards'   => array(
						self::card( 'Can Niwa rebuild a full reference page?', 'Yes. For supported URLs Niwa now uses a curated section blueprint and writes native Elementor containers and widgets instead of a single HTML block.' ),
						self::card( 'Are the sections editable in Elementor?', 'Yes. The output uses headings, text editors, buttons, images, toggles, dividers, and nested containers that Elementor can edit.' ),
						self::card( 'Does Niwa copy protected third-party assets?', 'No. The builder recreates structure and interaction patterns with Niwa-branded copy and local mockup assets.' ),
						self::card( 'Can Niwa refine the page after creation?', 'Yes. After the first build, Niwa can inspect page structure and update individual widgets, containers, images, buttons, and copy.' ),
						self::card( 'Can this be reused for other reference pages?', 'Yes. Unknown URLs still use the generic URL extractor, while complex known pages can gain curated blueprints when exact section mapping matters.' ),
					),
				),
				array(
					'type'    => 'angie_cta',
					'eyebrow' => 'Build what is next',
					'heading' => 'Use Niwa to turn a prompt, URL, or screenshot into real Elementor work',
					'body'    => 'This page was generated as a native Elementor reference build on dentelevate.com.',
					'buttons' => array(
						array( 'text' => 'Open Niwa AI', 'url' => admin_url( 'admin.php?page=niwa-ai' ) ),
					),
				),
			),
		);
	}

	private static function card( $title, $body, $image = '' ) {
		$card = array(
			'title' => (string) $title,
			'body'  => (string) $body,
			'image' => array(),
		);
		if ( '' !== $image ) {
			$card['image'] = self::image( $image, $title );
		}
		return $card;
	}

	private static function image( $file, $alt ) {
		return array(
			'url' => ( defined( 'NIWA_AI_URL' ) ? NIWA_AI_URL : '' ) . 'assets/img/template-library/reference/' . ltrim( (string) $file, '/' ),
			'alt' => (string) $alt,
		);
	}

	private static function trim( $text, $max ) {
		$text = preg_replace( '/\s+/u', ' ', wp_strip_all_tags( (string) $text ) );
		$text = trim( (string) $text );
		if ( strlen( $text ) > $max ) {
			return substr( $text, 0, $max - 3 ) . '...';
		}
		return $text;
	}
}
