<?php
/**
 * Elementor widget for IGIT Related Posts.
 *
 * Renders the related posts block inside the Elementor page builder using
 * the same IGIT_RPWT_Renderer::render() pipeline as the rest of the plugin.
 *
 * @package IGIT_RPWT
 */

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

class IGIT_RPWT_Elementor_Widget extends \Elementor\Widget_Base {

	public function get_name() {
		return 'igit_related_posts';
	}

	public function get_title() {
		return __( 'Related Posts', 'igit-related-posts-with-thumb-images-after-posts' );
	}

	public function get_icon() {
		return 'eicon-posts-grid';
	}

	public function get_categories() {
		return array( 'general' );
	}

	public function get_keywords() {
		return array( 'related', 'posts', 'igit' );
	}

	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array(
				'label' => __( 'Related Posts', 'igit-related-posts-with-thumb-images-after-posts' ),
				'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'post_count',
			array(
				'label'   => __( 'Number of Posts', 'igit-related-posts-with-thumb-images-after-posts' ),
				'type'    => \Elementor\Controls_Manager::NUMBER,
				'min'     => 1,
				'max'     => 20,
				'default' => IGIT_RPWT_Settings::get( 'post_count' ),
			)
		);

		$this->add_control(
			'layout',
			array(
				'label'   => __( 'Layout', 'igit-related-posts-with-thumb-images-after-posts' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'options' => array(
					'grid'     => __( 'Grid', 'igit-related-posts-with-thumb-images-after-posts' ),
					'list'     => __( 'List', 'igit-related-posts-with-thumb-images-after-posts' ),
					'compact'  => __( 'Compact', 'igit-related-posts-with-thumb-images-after-posts' ),
					'masonry'  => __( 'Masonry', 'igit-related-posts-with-thumb-images-after-posts' ),
					'carousel' => __( 'Carousel', 'igit-related-posts-with-thumb-images-after-posts' ),
					'chip'     => __( 'Chip / Pill', 'igit-related-posts-with-thumb-images-after-posts' ),
				),
				'default' => IGIT_RPWT_Settings::get( 'layout' ),
			)
		);

		$this->add_control(
			'relate_by',
			array(
				'label'   => __( 'Find Related By', 'igit-related-posts-with-thumb-images-after-posts' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'options' => array(
					'both'     => __( 'Category & Tag', 'igit-related-posts-with-thumb-images-after-posts' ),
					'category' => __( 'Category only', 'igit-related-posts-with-thumb-images-after-posts' ),
					'tag'      => __( 'Tag only', 'igit-related-posts-with-thumb-images-after-posts' ),
					'fulltext' => __( 'Content Similarity', 'igit-related-posts-with-thumb-images-after-posts' ),
					'author'   => __( 'Same Author', 'igit-related-posts-with-thumb-images-after-posts' ),
					'series'   => __( 'Series', 'igit-related-posts-with-thumb-images-after-posts' ),
				),
				'default' => IGIT_RPWT_Settings::get( 'relate_by' ),
			)
		);

		$this->add_control(
			'show_thumbnail',
			array(
				'label'        => __( 'Show Thumbnail', 'igit-related-posts-with-thumb-images-after-posts' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => __( 'Yes', 'igit-related-posts-with-thumb-images-after-posts' ),
				'label_off'    => __( 'No', 'igit-related-posts-with-thumb-images-after-posts' ),
				'return_value' => '1',
				'default'      => IGIT_RPWT_Settings::get( 'show_thumbnail' ) ? '1' : '',
			)
		);

		$this->add_control(
			'show_excerpt',
			array(
				'label'        => __( 'Show Excerpt', 'igit-related-posts-with-thumb-images-after-posts' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => __( 'Yes', 'igit-related-posts-with-thumb-images-after-posts' ),
				'label_off'    => __( 'No', 'igit-related-posts-with-thumb-images-after-posts' ),
				'return_value' => '1',
				'default'      => IGIT_RPWT_Settings::get( 'show_excerpt' ) ? '1' : '',
			)
		);

		$this->end_controls_section();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		$post_id  = get_the_ID();

		if ( ! $post_id ) {
			echo '<p>' . esc_html__( 'Related posts will appear here on a single post.', 'igit-related-posts-with-thumb-images-after-posts' ) . '</p>';
			return;
		}

		$args = array(
			'post_count'     => absint( $settings['post_count'] ),
			'layout'         => sanitize_key( $settings['layout'] ),
			'relate_by'      => sanitize_key( $settings['relate_by'] ),
			'show_thumbnail' => ! empty( $settings['show_thumbnail'] ),
			'show_excerpt'   => ! empty( $settings['show_excerpt'] ),
		);

		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo IGIT_RPWT_Renderer::render( $post_id, $args );
	}
}
