<?php
/**
 * Shared constants and labels for the Niwa template library.
 *
 * @package Niwa_AI
 */

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

class Niwa_AI_Template_Library_Types {
	const CPT_TEMPLATE = 'niwa_ai_template';
	const CPT_SNIPPET  = 'niwa_ai_snippet';
	const TAXONOMY     = 'niwa_template_type';

	const TYPE_FEATURED                = 'featured';
	const TYPE_ELEMENTOR_WIDGET        = 'elementor-widget';
	const TYPE_WORDPRESS_ADMIN_SNIPPET = 'wordpress-admin-snippet';
	const TYPE_WEBSITE_SNIPPET         = 'website-snippet';
	const TYPE_VISUAL_APP              = 'visual-app';
	const TYPE_SHORTCODE               = 'shortcode';

	const META_TYPE              = '_niwa_template_type';
	const META_SUBTITLE          = '_niwa_template_subtitle';
	const META_PROMPT            = '_niwa_template_prompt';
	const META_AGENT_PROMPT      = '_niwa_template_agent_prompt';
	const META_PREVIEW_ID        = '_niwa_template_preview_attachment_id';
	const META_PREVIEW_URL       = '_niwa_template_preview_url';
	const META_PREVIEW_PROMPT    = '_niwa_template_preview_prompt';
	const META_OUTPUT_MODE       = '_niwa_template_output_mode';
	const META_ELEMENTOR_SCHEMA  = '_niwa_template_elementor_schema';
	const META_MANIFEST          = '_niwa_template_manifest';
	const META_ORIGIN            = '_niwa_template_origin';
	const META_SNIPPET_FILES     = '_niwa_snippet_files';
	const META_SNIPPET_TYPE      = '_niwa_snippet_type';
	const META_SNIPPET_STATUS    = '_niwa_snippet_deployment_status';
	const META_SNIPPET_TEMPLATE  = '_niwa_snippet_template_id';
	const META_SNIPPET_SHORTCODE = '_niwa_snippet_shortcode';
	const META_SNIPPET_ERROR     = '_niwa_snippet_last_error';
	const META_SNIPPET_VERSION   = '_niwa_snippet_version';

	public static function template_types() {
		return array(
			self::TYPE_FEATURED,
			self::TYPE_ELEMENTOR_WIDGET,
			self::TYPE_WORDPRESS_ADMIN_SNIPPET,
			self::TYPE_WEBSITE_SNIPPET,
			self::TYPE_VISUAL_APP,
		);
	}

	public static function snippet_types() {
		return array(
			self::TYPE_ELEMENTOR_WIDGET,
			self::TYPE_WORDPRESS_ADMIN_SNIPPET,
			self::TYPE_WEBSITE_SNIPPET,
			self::TYPE_VISUAL_APP,
			self::TYPE_SHORTCODE,
		);
	}

	public static function labels() {
		return array(
			self::TYPE_FEATURED                => __( 'Featured', 'niwa-ai' ),
			self::TYPE_ELEMENTOR_WIDGET        => __( 'Elementor Widgets', 'niwa-ai' ),
			self::TYPE_WORDPRESS_ADMIN_SNIPPET => __( 'WordPress Admin Snippets', 'niwa-ai' ),
			self::TYPE_WEBSITE_SNIPPET         => __( 'Website Snippets', 'niwa-ai' ),
			self::TYPE_VISUAL_APP              => __( 'Visual Apps', 'niwa-ai' ),
			self::TYPE_SHORTCODE               => __( 'Shortcodes', 'niwa-ai' ),
		);
	}

	public static function label( $type ) {
		$labels = self::labels();
		$type   = sanitize_key( (string) $type );
		return isset( $labels[ $type ] ) ? $labels[ $type ] : $type;
	}

	public static function normalize_template_type( $type ) {
		$type = sanitize_key( (string) $type );
		return in_array( $type, self::template_types(), true ) ? $type : self::TYPE_FEATURED;
	}

	public static function normalize_snippet_type( $type ) {
		$type = sanitize_key( (string) $type );
		return in_array( $type, self::snippet_types(), true ) ? $type : self::TYPE_SHORTCODE;
	}

	public static function default_output_mode_for_type( $type ) {
		$type = self::normalize_template_type( $type );
		if ( self::TYPE_ELEMENTOR_WIDGET === $type ) {
			return 'elementor_widget';
		}
		if ( self::TYPE_WORDPRESS_ADMIN_SNIPPET === $type ) {
			return 'admin_snippet';
		}
		if ( self::TYPE_VISUAL_APP === $type ) {
			return 'visual_app';
		}
		return 'shortcode';
	}
}
