<?php
/**
 * Widget catalog for settings, shortcodes, blocks, and optional SiteOrigin.
 *
 * @package Rsmspark_Content_Widgets
 */

defined( 'ABSPATH' ) || exit;

/**
 * Rsmspark_Content_Widgets_Registry
 */
class Rsmspark_Content_Widgets_Registry {

	/**
	 * @return array<string, array<string, mixed>>
	 */
	public static function definitions() {
		$widgets = array(
			'hero'               => array(
				'label'        => __( 'Hero / Split Hero', 'rsm-content-widgets' ),
				'description'  => __( 'Image background with headline and CTA.', 'rsm-content-widgets' ),
				'shortcode'    => 'rsmspark_hero',
				'is_pro'       => false,
				'default_on'   => true,
				'phase'        => 1,
			),
			'features'           => array(
				'label'        => __( 'RSM Features (chip cards)', 'rsm-content-widgets' ),
				'description'  => __( 'Feature chip cards with per-item enable/disable and corner presets.', 'rsm-content-widgets' ),
				'shortcode'    => 'rsmspark_features',
				'is_pro'       => false,
				'default_on'   => true,
				'phase'        => 1,
			),
			'tabs'               => array(
				'label'        => __( 'Tabs', 'rsm-content-widgets' ),
				'description'  => __( 'Tabbed content panels.', 'rsm-content-widgets' ),
				'shortcode'    => 'rsmspark_tabs',
				'is_pro'       => false,
				'default_on'   => true,
				'phase'        => 1,
			),
			'accordion'          => array(
				'label'        => __( 'Accordion', 'rsm-content-widgets' ),
				'description'  => __( 'Collapsible content sections.', 'rsm-content-widgets' ),
				'shortcode'    => 'rsmspark_accordion',
				'is_pro'       => false,
				'default_on'   => true,
				'phase'        => 1,
			),
			'icon_box_grid'      => array(
				'label'        => __( 'Feature / Icon Box Grid', 'rsm-content-widgets' ),
				'description'  => __( 'Grid of icon, title, and description cards.', 'rsm-content-widgets' ),
				'shortcode'    => 'rsmspark_icon_box_grid',
				'is_pro'       => false,
				'default_on'   => true,
				'phase'        => 2,
			),
			'testimonials_team'  => array(
				'label'        => __( 'Testimonials / Team', 'rsm-content-widgets' ),
				'description'  => __( 'Profiles with modal details. Manual entries or admin-managed library.', 'rsm-content-widgets' ),
				'shortcode'    => 'rsmspark_testimonials_team',
				'is_pro'       => false,
				'default_on'   => true,
				'phase'        => 2,
			),
			'image'              => array(
				'label'        => __( 'RSM Image', 'rsm-content-widgets' ),
				'description'  => __( 'Media image with size, alignment, rounded corners, and optional border.', 'rsm-content-widgets' ),
				'shortcode'    => 'rsmspark_image',
				'is_pro'       => false,
				'default_on'   => true,
				'phase'        => 2,
			),
			'headline'           => array(
				'label'        => __( 'RSM Headline', 'rsm-content-widgets' ),
				'description'  => __( 'Overline, headline, and subheadline with per-line typography and container styling.', 'rsm-content-widgets' ),
				'shortcode'    => 'rsmspark_headline',
				'is_pro'       => false,
				'default_on'   => true,
				'phase'        => 2,
			),
			'buttons'            => array(
				'label'        => __( 'RSM Buttons', 'rsm-content-widgets' ),
				'description'  => __( 'Reusable styled buttons with saved library, shortcode, and live builder.', 'rsm-content-widgets' ),
				'shortcode'    => 'rsmspark_button',
				'is_pro'       => false,
				'default_on'   => true,
				'phase'        => 2,
			),
			'footer'             => array(
				'label'        => __( 'RSM Footer', 'rsm-content-widgets' ),
				'description'  => __( 'Multi-column footer with brand, social icons, and categorized link columns.', 'rsm-content-widgets' ),
				'shortcode'    => 'rsmspark_footer',
				'is_pro'       => false,
				'default_on'   => true,
				'phase'        => 2,
			),
			'logo_grid'          => array(
				'label'        => __( 'RSM Logo Grid', 'rsm-content-widgets' ),
				'description'  => __( 'Client or partner logos in a responsive grid.', 'rsm-content-widgets' ),
				'shortcode'    => 'rsmspark_logo_grid',
				'is_pro'       => false,
				'default_on'   => true,
				'phase'        => 3,
			),
			'stats_counter'      => array(
				'label'        => __( 'RSM Stats Counter', 'rsm-content-widgets' ),
				'description'  => __( 'Key metrics with labels.', 'rsm-content-widgets' ),
				'shortcode'    => 'rsmspark_stats_counter',
				'is_pro'       => false,
				'default_on'   => true,
				'phase'        => 3,
			),
			'event_countdown'    => array(
				'label'        => __( 'RSM Event Countdown', 'rsm-content-widgets' ),
				'description'  => __( 'Fixed-date countdown to an event — days, hours, minutes, and seconds.', 'rsm-content-widgets' ),
				'shortcode'    => 'rsmspark_event_countdown',
				'is_pro'       => false,
				'default_on'   => true,
				'phase'        => 3,
			),
			'svg_icon'           => array(
				'label'          => __( 'RSM SVG Icon', 'rsm-content-widgets' ),
				'description'    => __( 'Upload or paste a sanitized SVG icon (requires RSM Focus).', 'rsm-content-widgets' ),
				'shortcode'      => '',
				'is_pro'         => false,
				'default_on'     => true,
				'phase'          => 3,
				'requires_focus' => true,
			),
		);

		return apply_filters( 'rsmspark_content_widgets_registry', $widgets );
	}

	/**
	 * Registry ids (no translated strings — safe before init).
	 *
	 * @return string[]
	 */
	public static function widget_ids() {
		return array(
			'hero',
			'features',
			'tabs',
			'accordion',
			'icon_box_grid',
			'testimonials_team',
			'image',
			'headline',
			'buttons',
			'footer',
			'logo_grid',
			'stats_counter',
			'event_countdown',
			'svg_icon',
		);
	}

	/**
	 * @return array<string, bool>
	 */
	public static function default_enabled_map() {
		// Settings::get() runs on plugins_loaded; definitions() uses __() and must wait for init.
		if ( ! did_action( 'init' ) ) {
			return array_fill_keys( self::widget_ids(), true );
		}

		$map = array();
		foreach ( self::definitions() as $id => $widget ) {
			$map[ $id ] = ! empty( $widget['default_on'] );
		}

		return $map;
	}

	/**
	 * @param string $widget_id Widget id.
	 * @return bool
	 */
	public static function is_pro_widget( $widget_id ) {
		$defs = self::definitions();
		$id   = sanitize_key( (string) $widget_id );
		return ! empty( $defs[ $id ]['is_pro'] );
	}

	/**
	 * @param string $widget_id Widget id.
	 * @return string
	 */
	public static function required_feature( $widget_id ) {
		$defs = self::definitions();
		$id   = sanitize_key( (string) $widget_id );
		return sanitize_key( (string) ( $defs[ $id ]['feature'] ?? 'base_pro' ) );
	}

	/**
	 * @param string $widget_id Widget id.
	 * @return string
	 */
	public static function shortcode_tag( $widget_id ) {
		$defs = self::definitions();
		$id   = sanitize_key( (string) $widget_id );
		return sanitize_key( (string) ( $defs[ $id ]['shortcode'] ?? 'rsmspark_' . $id ) );
	}

	/**
	 * SiteOrigin folder id for a registry id.
	 *
	 * @param string $registry_id Registry id.
	 * @return string
	 */
	public static function siteorigin_folder_id( $registry_id ) {
		$map = array(
			'icon_box_grid'      => 'icon-box-grid',
			'testimonials_team'  => 'testimonials-team',
			'buttons'            => 'buttons',
			'logo_grid'          => 'logo-grid',
			'stats_counter'      => 'stats-counter',
			'event_countdown'    => 'event-countdown',
			'svg_icon'           => 'svg-icon',
			'image'              => 'rsm-image',
		);

		$id = sanitize_key( (string) $registry_id );
		return $map[ $id ] ?? $id;
	}

	/**
	 * Map a Gutenberg block slug (hyphenated) to a registry id (underscored).
	 *
	 * @param string $block_slug Block slug without namespace.
	 * @return string
	 */
	public static function registry_id_from_block_slug( $block_slug ) {
		$slug = sanitize_key( str_replace( '-', '_', (string) $block_slug ) );
		$defs = self::definitions();

		return isset( $defs[ $slug ] ) ? $slug : sanitize_key( (string) $block_slug );
	}
}
