<?php
/**
 * Testimonials / Team widget.
 *
 * @package Rsmspark_Content_Widgets
 */

/*
Widget Name: RSM Testimonials / Team
Description: Team or testimonial profiles with modal details.
Author: RSMSpark
Author URI: https://rsmspark.com/
*/

defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'SiteOrigin_Widget', false ) ) {
	return;
}

/**
 * Rsmspark_Content_Widgets_Testimonials_Team_Widget
 */
class Rsmspark_Content_Widgets_Testimonials_Team_Widget extends SiteOrigin_Widget {

	/**
	 * Constructor.
	 */
	public function __construct() {
		parent::__construct(
			'testimonials-team',
			__( 'RSM Testimonials / Team', 'rsm-content-widgets' ),
			array(
				'description' => __( 'Profiles in a grid or carousel. Manual entries or admin library.', 'rsm-content-widgets' ),
				'panels_icon' => 'dashicons dashicons-groups',
				'has_preview' => true,
				'keywords'    => array( 'team', 'testimonials', 'people', 'RSM' ),
			),
			array(),
			array(),
			plugin_dir_path( __FILE__ )
		);
	}

	/**
	 * @return array<string, mixed>
	 */
	public function get_widget_form() {
		$content_fields = array(
			'title' => array(
				'type'  => 'text',
				'label' => __( 'Section title', 'rsm-content-widgets' ),
			),
			'layout' => array(
				'type'    => 'select',
				'label'   => __( 'Layout', 'rsm-content-widgets' ),
				'default' => 'grid',
				'options' => array(
					'grid'     => __( 'Grid', 'rsm-content-widgets' ),
					'carousel' => __( 'Carousel', 'rsm-content-widgets' ),
				),
			),
			'source' => array(
				'type'        => 'select',
				'label'       => __( 'Data source', 'rsm-content-widgets' ),
				'default'     => 'manual',
				'options'     => array(
					'manual'  => __( 'Manual profiles (below)', 'rsm-content-widgets' ),
					'library' => __( 'Admin testimonial library', 'rsm-content-widgets' ),
				),
				'description' => __( 'Manage library entries under RSM → Testimonials.', 'rsm-content-widgets' ),
			),
			'profiles' => array(
				'type'      => 'repeater',
				'label'     => __( 'Profiles', 'rsm-content-widgets' ),
				'item_name' => __( 'Profile', 'rsm-content-widgets' ),
				'fields'    => array(
					'photo' => array(
						'type'  => 'media',
						'label' => __( 'Photo', 'rsm-content-widgets' ),
					),
					'name' => array(
						'type'  => 'text',
						'label' => __( 'Name', 'rsm-content-widgets' ),
					),
					'role' => array(
						'type'  => 'text',
						'label' => __( 'Role / title', 'rsm-content-widgets' ),
					),
					'quote' => array(
						'type'  => 'textarea',
						'label' => __( 'Short quote or summary', 'rsm-content-widgets' ),
					),
					'bio' => array(
						'type'  => 'tinymce',
						'label' => __( 'Full bio (modal)', 'rsm-content-widgets' ),
					),
				),
				'default'   => Rsmspark_Content_Widgets_Widget_Testimonials_Team::default_profiles(),
			),
		);

		$settings_fields = array(
			'library_limit' => array(
				'type'    => 'number',
				'label'   => __( 'Library limit', 'rsm-content-widgets' ),
				'default' => 12,
			),
			'library_orderby' => array(
				'type'    => 'select',
				'label'   => __( 'Library order by', 'rsm-content-widgets' ),
				'default' => 'date',
				'options' => array(
					'date'       => __( 'Date added', 'rsm-content-widgets' ),
					'menu_order' => __( 'Menu order', 'rsm-content-widgets' ),
					'title'      => __( 'Name', 'rsm-content-widgets' ),
				),
			),
			'library_order' => array(
				'type'    => 'select',
				'label'   => __( 'Library order', 'rsm-content-widgets' ),
				'default' => 'DESC',
				'options' => array(
					'DESC' => __( 'Newest first', 'rsm-content-widgets' ),
					'ASC'  => __( 'Oldest first', 'rsm-content-widgets' ),
				),
			),
		);

		return Rsmspark_Content_Widgets_SiteOrigin::prepend_import_export_form(
			'testimonials_team',
			array(
			'content'  => array(
				'type'   => 'section',
				'label'  => __( 'Profiles', 'rsm-content-widgets' ),
				'fields' => $content_fields,
			),
			'settings' => array(
				'type'   => 'section',
				'label'  => __( 'Library', 'rsm-content-widgets' ),
				'fields' => $settings_fields,
			),
		)
		);
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 * @return string
	 */
	public function get_template_dir( $instance ) {
		unset( $instance );
		return '';
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 * @return string
	 */
	public function get_style_name( $instance ) {
		unset( $instance );
		return '';
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 * @return string
	 */
	public function get_template_name( $instance ) {
		unset( $instance );
		return 'testimonials-team.tpl';
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 * @param array<string, mixed> $args     Args.
	 * @return array<string, mixed>
	 */
	public function get_template_variables( $instance, $args ) {
		unset( $args );
		return Rsmspark_Content_Widgets_Widget_Testimonials_Team::prepare_template_vars(
			Rsmspark_Content_Widgets_Widget_Testimonials_Team::prepare_from_instance(
				array(
					'content'  => Rsmspark_Content_Widgets_Loader::section_instance( $instance, 'content' ),
					'settings' => Rsmspark_Content_Widgets_Loader::section_instance( $instance, 'settings' ),
				)
			)
		);
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 * @param array<string, mixed> $args     Args.
	 * @return string
	 */
	public function render_template( $instance, $args ) {
		unset( $args );
		$vars = $this->get_template_variables( $instance, $args );
		return Rsmspark_Content_Widgets_Widget_Testimonials_Team::render( $vars );
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 */
	public function enqueue_admin_scripts() {
		Rsmspark_Content_Widgets_Loader::enqueue_shared_assets( $this );
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 */
	public function enqueue_frontend_scripts( $instance ) {
		unset( $instance );
		Rsmspark_Content_Widgets_Loader::enqueue_shared_assets( $this );
	}
}

if ( function_exists( 'siteorigin_widget_register' ) ) {
	siteorigin_widget_register( 'testimonials-team', __FILE__, 'Rsmspark_Content_Widgets_Testimonials_Team_Widget' );
}
