<?php
/**
 * @package Softech_Addons_for_Elementor
 */

namespace Softech_Addons_for_Elementor\Widgets;

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

use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Background;

class sitsel_post_navigation  extends \Elementor\Widget_Base {

    // ── Identity ──────────────────────────────────────────────────────────
    public function get_name()      { return 'sitsel_post_navigation'; }
    public function get_title()    {     return __('SITS Post Navigation', 'softech-addons-for-elementor'); }
    public function get_icon()      { return 'eicon-post-navigation'; }
    public function get_categories(): array { return [ 'sits-category' ]; }
    public function get_keywords(): array   { return [ 'navigation', 'post', 'next', 'previous', 'nav', 'pagination' ]; }

    
    public function get_style_depends(): array {
        if ( ! wp_style_is( 'sitsel-post-nav-css', 'registered' ) ) {
            wp_register_style(
                'sitsel-post-nav-css',
                SITS_ELEMENTOR_ADDONS_PLUGIN_URL . 'assets/css/sitsel-post-navigation.css',
                [],
                SITS_ELEMENTOR_ADDONS_VERSION
            );
        }
        return [ 'sitsel-post-nav-css' ];
    }

    // ── Controls ──────────────────────────────────────────────────────────
    protected function register_controls(): void {
        $this->register_content_tab();
        $this->register_style_tab();
    }

    // ══════════════════════════════════════════════════════════════════════
    //  CONTENT TAB
    // ══════════════════════════════════════════════════════════════════════
    private function register_content_tab(): void {
        $this->register_layout_section();
        $this->register_display_section();
    }

    private function register_layout_section(): void {
        $this->start_controls_section( 'section_layout', [
            'label' => esc_html__( 'Layout & Style', 'softech-addons-for-elementor' ),
            'tab'   => Controls_Manager::TAB_CONTENT,
        ] );

        $this->add_control( 'nav_style', [
            'label'   => esc_html__( 'Navigation Style', 'softech-addons-for-elementor' ),
            'type'    => Controls_Manager::SELECT,
            'options' => [
                'card-thumb' => esc_html__( 'Style 1 – Card with Thumbnail', 'softech-addons-for-elementor' ),
                'minimal'    => esc_html__( 'Style 2 – Minimal Underline',   'softech-addons-for-elementor' ),
            ],
            'default' => 'minimal',
        ] );

        $this->add_control( 'alignment', [
            'label'     => esc_html__( 'Alignment', 'softech-addons-for-elementor' ),
            'type'      => Controls_Manager::CHOOSE,
            'options'   => [
                'flex-start'    => [ 'title' => esc_html__( 'Left',          'softech-addons-for-elementor' ), 'icon' => 'eicon-text-align-left'   ],
                'center'        => [ 'title' => esc_html__( 'Center',        'softech-addons-for-elementor' ), 'icon' => 'eicon-text-align-center' ],
                'flex-end'      => [ 'title' => esc_html__( 'Right',         'softech-addons-for-elementor' ), 'icon' => 'eicon-text-align-right'  ],
                'space-between' => [ 'title' => esc_html__( 'Space Between', 'softech-addons-for-elementor' ), 'icon' => 'eicon-h-align-stretch'   ],
            ],
            'default'   => 'space-between',
            'selectors' => [ '{{WRAPPER}} .sitsel-pnav-wrapper' => 'justify-content: {{VALUE}};' ],
        ] );

        $this->add_control( 'same_category', [
            'label'        => esc_html__( 'Same Category Only', 'softech-addons-for-elementor' ),
            'type'         => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default'      => '',
        ] );

        $this->end_controls_section();
    }

    private function register_display_section(): void {
        $this->start_controls_section( 'section_display', [
            'label' => esc_html__( 'Display Options', 'softech-addons-for-elementor' ),
            'tab'   => Controls_Manager::TAB_CONTENT,
        ] );

        $this->add_control( 'show_label', [
            'label'        => esc_html__( 'Show Prev / Next Label', 'softech-addons-for-elementor' ),
            'type'         => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default'      => 'yes',
        ] );

        $this->add_control( 'prev_label', [
            'label'     => esc_html__( 'Previous Label', 'softech-addons-for-elementor' ),
            'type'      => Controls_Manager::TEXT,
            'default'   => esc_html__( 'Previous Post', 'softech-addons-for-elementor' ),
            'condition' => [ 'show_label' => 'yes' ],
        ] );

        $this->add_control( 'next_label', [
            'label'     => esc_html__( 'Next Label', 'softech-addons-for-elementor' ),
            'type'      => Controls_Manager::TEXT,
            'default'   => esc_html__( 'Next Post', 'softech-addons-for-elementor' ),
            'condition' => [ 'show_label' => 'yes' ],
        ] );

        $this->add_control( 'show_title', [
            'label'        => esc_html__( 'Show Post Title', 'softech-addons-for-elementor' ),
            'type'         => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default'      => 'yes',
        ] );

        $this->add_control( 'title_words', [
            'label'     => esc_html__( 'Title Word Limit', 'softech-addons-for-elementor' ),
            'type'      => Controls_Manager::NUMBER,
            'default'   => 6,
            'min'       => 1,
            'max'       => 20,
            'condition' => [ 'show_title' => 'yes' ],
        ] );

        $this->add_control( 'show_thumbnail', [
            'label'        => esc_html__( 'Show Thumbnail', 'softech-addons-for-elementor' ),
            'type'         => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default'      => 'yes',
            'condition'    => [ 'nav_style' => 'card-thumb' ],
        ] );

        $this->add_control( 'thumbnail_size', [
            'label'     => esc_html__( 'Thumbnail Size', 'softech-addons-for-elementor' ),
            'type'      => Controls_Manager::SELECT,
            'default'   => 'thumbnail',
            'options'   => [
                'thumbnail' => esc_html__( 'Thumbnail', 'softech-addons-for-elementor' ),
                'medium'    => esc_html__( 'Medium',    'softech-addons-for-elementor' ),
                'large'     => esc_html__( 'Large',     'softech-addons-for-elementor' ),
                'full'      => esc_html__( 'Full',      'softech-addons-for-elementor' ),
            ],
            'condition' => [ 'nav_style' => 'card-thumb', 'show_thumbnail' => 'yes' ],
        ] );

        $this->add_control( 'show_arrow', [
            'label'        => esc_html__( 'Show Arrow Icons', 'softech-addons-for-elementor' ),
            'type'         => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default'      => 'yes',
        ] );

        $this->add_control( 'prev_icon', [
            'label'     => esc_html__( 'Previous Icon', 'softech-addons-for-elementor' ),
            'type'      => Controls_Manager::ICONS,
            'default'   => [ 'value' => 'fas fa-arrow-left', 'library' => 'fa-solid' ],
            'condition' => [ 'show_arrow' => 'yes' ],
        ] );

        $this->add_control( 'next_icon', [
            'label'     => esc_html__( 'Next Icon', 'softech-addons-for-elementor' ),
            'type'      => Controls_Manager::ICONS,
            'default'   => [ 'value' => 'fas fa-arrow-right', 'library' => 'fa-solid' ],
            'condition' => [ 'show_arrow' => 'yes' ],
        ] );

        $this->add_control( 'show_divider', [
            'label'        => esc_html__( 'Show Divider', 'softech-addons-for-elementor' ),
            'type'         => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default'      => 'yes',
            'condition'    => [ 'nav_style!' => 'minimal' ],
        ] );

        $this->end_controls_section();
    }

    // ══════════════════════════════════════════════════════════════════════
    //  STYLE TAB
    // ══════════════════════════════════════════════════════════════════════
    private function register_style_tab(): void {
        $this->register_wrapper_style_section();
        $this->register_item_style_section();
        $this->register_label_style_section();
        $this->register_title_style_section();
        $this->register_arrow_style_section();
        $this->register_thumbnail_style_section();
        $this->register_divider_style_section();
    }

    private function register_wrapper_style_section(): void {
        $this->start_controls_section( 'style_wrapper', [
            'label' => esc_html__( 'Wrapper', 'softech-addons-for-elementor' ),
            'tab'   => Controls_Manager::TAB_STYLE,
        ] );

        $this->add_responsive_control( 'wrapper_gap', [
            'label'     => esc_html__( 'Gap Between Items', 'softech-addons-for-elementor' ),
            'type'      => Controls_Manager::SLIDER,
            'range'     => [ 'px' => [ 'min' => 0, 'max' => 100 ] ],
            'default'   => [ 'unit' => 'px', 'size' => 16 ],
            'selectors' => [ '{{WRAPPER}} .sitsel-pnav-wrapper' => 'gap: {{SIZE}}{{UNIT}};' ],
        ] );

        $this->end_controls_section();
    }

    private function register_item_style_section(): void {
        $this->start_controls_section( 'style_item', [
            'label' => esc_html__( 'Nav Item', 'softech-addons-for-elementor' ),
            'tab'   => Controls_Manager::TAB_STYLE,
        ] );

        $this->add_responsive_control( 'item_padding', [
            'label'      => esc_html__( 'Padding', 'softech-addons-for-elementor' ),
            'type'       => Controls_Manager::DIMENSIONS,
            'size_units' => [ 'px', 'em', '%' ],
            'default'    => [ 'top' => '16', 'right' => '20', 'bottom' => '16', 'left' => '20', 'unit' => 'px', 'isLinked' => false ],
            'selectors'  => [ '{{WRAPPER}} .sitsel-pnav-item' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ],
        ] );

        $this->add_responsive_control( 'item_radius', [
            'label'      => esc_html__( 'Border Radius', 'softech-addons-for-elementor' ),
            'type'       => Controls_Manager::DIMENSIONS,
            'size_units' => [ 'px', '%' ],
            'default'    => [ 'top' => '8', 'right' => '8', 'bottom' => '8', 'left' => '8', 'unit' => 'px', 'isLinked' => true ],
            'selectors'  => [ '{{WRAPPER}} .sitsel-pnav-item' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ],
        ] );

        $this->start_controls_tabs( 'item_tabs' );

            $this->start_controls_tab( 'item_tab_normal', [ 'label' => esc_html__( 'Normal', 'softech-addons-for-elementor' ) ] );
            $this->add_group_control( Group_Control_Background::get_type(), [
                'name'     => 'item_background',
                'types'    => [ 'classic', 'gradient' ],
                'selector' => '{{WRAPPER}} .sitsel-pnav-item',
                'fields_options' => [
                    'background' => [ 'default' => 'classic' ],
                    'color'      => [ 'default' => '#ffffff' ],
                ],
            ] );
            $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'item_border', 'selector' => '{{WRAPPER}} .sitsel-pnav-item',
                'fields_options' => [
                    'border' => [ 'default' => 'solid' ],
                    'width'  => [ 'default' => [ 'top' => '1', 'right' => '1', 'bottom' => '1', 'left' => '1', 'unit' => 'px' ] ],
                    'color'  => [ 'default' => '#e5e7eb' ],
                ],
            ] );
            $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'item_shadow', 'selector' => '{{WRAPPER}} .sitsel-pnav-item' ] );
            $this->end_controls_tab();

            $this->start_controls_tab( 'item_tab_hover', [ 'label' => esc_html__( 'Hover', 'softech-addons-for-elementor' ) ] );
            $this->add_control( 'item_bg_hover', [
                'label'     => esc_html__( 'Background', 'softech-addons-for-elementor' ),
                'type'      => Controls_Manager::COLOR,
                'default'   => '#f9fafb',
                'selectors' => [ '{{WRAPPER}} .sitsel-pnav-item:hover' => 'background-color: {{VALUE}};' ],
            ] );
            $this->add_control( 'item_border_hover', [
                'label'     => esc_html__( 'Border Color', 'softech-addons-for-elementor' ),
                'type'      => Controls_Manager::COLOR,
                'default'   => '#d1d5db',
                'selectors' => [ '{{WRAPPER}} .sitsel-pnav-item:hover' => 'border-color: {{VALUE}};' ],
            ] );
            $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'item_shadow_hover', 'selector' => '{{WRAPPER}} .sitsel-pnav-item:hover' ] );
            $this->end_controls_tab();

        $this->end_controls_tabs();

        $this->add_control( 'item_transition', [
            'label'     => esc_html__( 'Transition Duration (s)', 'softech-addons-for-elementor' ),
            'type'      => Controls_Manager::SLIDER,
            'default'   => [ 'size' => 0.3 ],
            'range'     => [ 'px' => [ 'min' => 0, 'max' => 2, 'step' => 0.05 ] ],
            'selectors' => [ '{{WRAPPER}} .sitsel-pnav-item' => 'transition: all {{SIZE}}s ease;' ],
            'separator' => 'before',
        ] );

        $this->end_controls_section();
    }

    private function register_label_style_section(): void {
        $this->start_controls_section( 'style_label', [
            'label'     => esc_html__( 'Label Text', 'softech-addons-for-elementor' ),
            'tab'       => Controls_Manager::TAB_STYLE,
            'condition' => [ 'show_label' => 'yes' ],
        ] );

        $this->add_group_control( Group_Control_Typography::get_type(), [
            'name'     => 'label_typography',
            'selector' => '{{WRAPPER}} .sitsel-pnav-label',
        ] );

        $this->start_controls_tabs( 'label_tabs' );
            $this->start_controls_tab( 'label_normal', [ 'label' => esc_html__( 'Normal', 'softech-addons-for-elementor' ) ] );
            $this->add_control( 'label_color', [
                'label'     => esc_html__( 'Color', 'softech-addons-for-elementor' ),
                'type'      => Controls_Manager::COLOR,
                'default'   => '#6b7280',
                'selectors' => [ '{{WRAPPER}} .sitsel-pnav-label' => 'color: {{VALUE}};' ],
            ] );
            $this->end_controls_tab();
            $this->start_controls_tab( 'label_hover', [ 'label' => esc_html__( 'Hover', 'softech-addons-for-elementor' ) ] );
            $this->add_control( 'label_color_hover', [
                'label'     => esc_html__( 'Color', 'softech-addons-for-elementor' ),
                'type'      => Controls_Manager::COLOR,
                 'default'   => '#374151',
                'selectors' => [ '{{WRAPPER}} .sitsel-pnav-item:hover .sitsel-pnav-label' => 'color: {{VALUE}};' ],
            ] );
            $this->end_controls_tab();
        $this->end_controls_tabs();

        $this->add_responsive_control( 'label_spacing', [
            'label'     => esc_html__( 'Bottom Spacing', 'softech-addons-for-elementor' ),
            'type'      => Controls_Manager::SLIDER,
            'range'     => [ 'px' => [ 'min' => 0, 'max' => 30 ] ],
            'default'   => [ 'unit' => 'px', 'size' => 4 ],
            'selectors' => [ '{{WRAPPER}} .sitsel-pnav-label' => 'margin-bottom: {{SIZE}}{{UNIT}};' ],
            'separator' => 'before',
        ] );

        $this->end_controls_section();
    }

    private function register_title_style_section(): void {
        $this->start_controls_section( 'style_title', [
            'label'     => esc_html__( 'Post Title', 'softech-addons-for-elementor' ),
            'tab'       => Controls_Manager::TAB_STYLE,
            'condition' => [ 'show_title' => 'yes' ],
        ] );

        $this->add_group_control( Group_Control_Typography::get_type(), [
            'name'     => 'title_typography',
            'selector' => '{{WRAPPER}} .sitsel-pnav-title',
        ] );

        $this->start_controls_tabs( 'title_tabs' );
            $this->start_controls_tab( 'title_normal', [ 'label' => esc_html__( 'Normal', 'softech-addons-for-elementor' ) ] );
            $this->add_control( 'title_color', [
                'label'     => esc_html__( 'Color', 'softech-addons-for-elementor' ),
                'type'      => Controls_Manager::COLOR,
                'default'   => '#111827',
                'selectors' => [ '{{WRAPPER}} .sitsel-pnav-title' => 'color: {{VALUE}};' ],
            ] );
            $this->end_controls_tab();
            $this->start_controls_tab( 'title_hover', [ 'label' => esc_html__( 'Hover', 'softech-addons-for-elementor' ) ] );
            $this->add_control( 'title_color_hover', [
                'label'     => esc_html__( 'Color', 'softech-addons-for-elementor' ),
                'type'      => Controls_Manager::COLOR,
                'default'   => '#0f3d4a',
                'selectors' => [ '{{WRAPPER}} .sitsel-pnav-item:hover .sitsel-pnav-title' => 'color: {{VALUE}};' ],
            ] );
            $this->end_controls_tab();
        $this->end_controls_tabs();

        $this->end_controls_section();
    }

    private function register_arrow_style_section(): void {
        $this->start_controls_section( 'style_arrow', [
            'label'     => esc_html__( 'Arrow Icon', 'softech-addons-for-elementor' ),
            'tab'       => Controls_Manager::TAB_STYLE,
            'condition' => [ 'show_arrow' => 'yes' ],
        ] );

        $this->add_responsive_control( 'arrow_icon_size', [
            'label'     => esc_html__( 'Icon Size', 'softech-addons-for-elementor' ),
            'type'      => Controls_Manager::SLIDER,
            'range'     => [ 'px' => [ 'min' => 8, 'max' => 48 ] ],
            'default'   => [ 'size' => 14, 'unit' => 'px' ],
            'selectors' => [
                '{{WRAPPER}} .sitsel-pnav-arrow i'   => 'font-size: {{SIZE}}{{UNIT}};',
                '{{WRAPPER}} .sitsel-pnav-arrow svg' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
            ],
        ] );

        $this->add_responsive_control( 'arrow_box_size', [
            'label'     => esc_html__( 'Box Size', 'softech-addons-for-elementor' ),
            'type'      => Controls_Manager::SLIDER,
            'range'     => [ 'px' => [ 'min' => 20, 'max' => 80 ] ],
            'default'   => [ 'size' => 40, 'unit' => 'px' ],
            'selectors' => [ '{{WRAPPER}} .sitsel-pnav-arrow' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};' ],
        ] );

        $this->add_responsive_control( 'arrow_radius', [
            'label'      => esc_html__( 'Border Radius', 'softech-addons-for-elementor' ),
            'type'       => Controls_Manager::DIMENSIONS,
            'size_units' => [ 'px', '%' ],
            'default'    => [ 'top' => '50', 'right' => '50', 'bottom' => '50', 'left' => '50', 'unit' => '%', 'isLinked' => true ],
            'selectors'  => [ '{{WRAPPER}} .sitsel-pnav-arrow' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ],
        ] );

        $this->start_controls_tabs( 'arrow_tabs' );
            $this->start_controls_tab( 'arrow_normal', [ 'label' => esc_html__( 'Normal', 'softech-addons-for-elementor' ) ] );
            $this->add_control( 'arrow_color', [
                'label'     => esc_html__( 'Icon Color', 'softech-addons-for-elementor' ),
                'type'      => Controls_Manager::COLOR,
                'default'   => '#374151',
                'selectors' => [
                    '{{WRAPPER}} .sitsel-pnav-arrow'     => 'color: {{VALUE}};',
                    '{{WRAPPER}} .sitsel-pnav-arrow svg' => 'fill: {{VALUE}};',
                ],
            ] );
            $this->add_control( 'arrow_bg', [
                'label'     => esc_html__( 'Background', 'softech-addons-for-elementor' ),
                'type'      => Controls_Manager::COLOR,
                'default'   => '#f3f4f6',
                'selectors' => [ '{{WRAPPER}} .sitsel-pnav-arrow' => 'background-color: {{VALUE}};' ],
            ] );
            $this->add_control( 'arrow_border_color', [
                'label'     => esc_html__( 'Border Color', 'softech-addons-for-elementor' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [ '{{WRAPPER}} .sitsel-pnav-arrow' => 'border-color: {{VALUE}};' ],
            ] );
            $this->end_controls_tab();
            $this->start_controls_tab( 'arrow_hover', [ 'label' => esc_html__( 'Hover', 'softech-addons-for-elementor' ) ] );
            $this->add_control( 'arrow_color_hover', [
                'label'     => esc_html__( 'Icon Color', 'softech-addons-for-elementor' ),
                'type'      => Controls_Manager::COLOR,
                'default'   => '#ffffff',
                'selectors' => [
                    '{{WRAPPER}} .sitsel-pnav-item:hover .sitsel-pnav-arrow'     => 'color: {{VALUE}};',
                    '{{WRAPPER}} .sitsel-pnav-item:hover .sitsel-pnav-arrow svg' => 'fill: {{VALUE}};',
                ],
            ] );
            $this->add_control( 'arrow_bg_hover', [
                'label'     => esc_html__( 'Background', 'softech-addons-for-elementor' ),
                'type'      => Controls_Manager::COLOR,
                'default'   => '#0f3d4a',
                'selectors' => [ '{{WRAPPER}} .sitsel-pnav-item:hover .sitsel-pnav-arrow' => 'background-color: {{VALUE}};' ],
            ] );
            $this->end_controls_tab();
        $this->end_controls_tabs();

        $this->end_controls_section();
    }

    private function register_thumbnail_style_section(): void {
        $this->start_controls_section( 'style_thumbnail', [
            'label'     => esc_html__( 'Thumbnail', 'softech-addons-for-elementor' ),
            'tab'       => Controls_Manager::TAB_STYLE,
            'condition' => [ 'nav_style' => 'card-thumb', 'show_thumbnail' => 'yes' ],
        ] );

        $this->add_responsive_control( 'thumb_width', [
            'label'     => esc_html__( 'Width', 'softech-addons-for-elementor' ),
            'type'      => Controls_Manager::SLIDER,
            'range'     => [ 'px' => [ 'min' => 40, 'max' => 200 ] ],
            'default'   => [ 'unit' => 'px', 'size' => 80 ],
            'selectors' => [ '{{WRAPPER}} .sitsel-pnav-thumb' => 'width: {{SIZE}}{{UNIT}}; min-width: {{SIZE}}{{UNIT}};' ],
        ] );

        $this->add_responsive_control( 'thumb_height', [
            'label'     => esc_html__( 'Height', 'softech-addons-for-elementor' ),
            'type'      => Controls_Manager::SLIDER,
            'range'     => [ 'px' => [ 'min' => 40, 'max' => 200 ] ],
            'default'   => [ 'unit' => 'px', 'size' => 60 ],
            'selectors' => [ '{{WRAPPER}} .sitsel-pnav-thumb' => 'height: {{SIZE}}{{UNIT}};' ],
        ] );

        $this->add_responsive_control( 'thumb_radius', [
            'label'      => esc_html__( 'Border Radius', 'softech-addons-for-elementor' ),
            'type'       => Controls_Manager::DIMENSIONS,
            'size_units' => [ 'px', '%' ],
            'default'    => [ 'top' => '6', 'right' => '6', 'bottom' => '6', 'left' => '6', 'unit' => 'px', 'isLinked' => true ],
            'selectors'  => [ '{{WRAPPER}} .sitsel-pnav-thumb' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ],
        ] );

        $this->end_controls_section();
    }

    private function register_divider_style_section(): void {
        $this->start_controls_section( 'style_divider', [
            'label'     => esc_html__( 'Divider', 'softech-addons-for-elementor' ),
            'tab'       => Controls_Manager::TAB_STYLE,
            'condition' => [ 'show_divider' => 'yes', 'nav_style!' => 'minimal' ],
        ] );

        $this->add_control( 'divider_color', [
            'label'     => esc_html__( 'Color', 'softech-addons-for-elementor' ),
            'type'      => Controls_Manager::COLOR,
            'default'   => '#e5e7eb',
            'selectors' => [ '{{WRAPPER}} .sitsel-pnav-divider' => 'background-color: {{VALUE}};' ],
        ] );

        $this->add_responsive_control( 'divider_width', [
            'label'     => esc_html__( 'Thickness', 'softech-addons-for-elementor' ),
            'type'      => Controls_Manager::SLIDER,
            'range'     => [ 'px' => [ 'min' => 1, 'max' => 10 ] ],
            'default'   => [ 'unit' => 'px', 'size' => 1 ],
            'selectors' => [ '{{WRAPPER}} .sitsel-pnav-divider' => 'width: {{SIZE}}{{UNIT}};' ],
        ] );

        $this->end_controls_section();
    }

    // ══════════════════════════════════════════════════════════════════════
    //  RENDER
    // ══════════════════════════════════════════════════════════════════════
    protected function render(): void {
        $settings  = $this->get_settings_for_display();
        $style     = sanitize_html_class( $settings['nav_style'] ?? 'arrow-label' );
        $same_cat  = ( 'yes' === ( $settings['same_category'] ?? '' ) );

        $prev_post = $this->get_adjacent_post( 'prev', $same_cat );
        $next_post = $this->get_adjacent_post( 'next', $same_cat );

        // Show placeholder in editor when no adjacent posts exist
        if ( ! $prev_post && ! $next_post ) {
            if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
                echo '<p class="sitsel-pnav-empty-notice">'
                    . esc_html__( 'No adjacent posts found. Visit a single post to preview navigation.', 'softech-addons-for-elementor' )
                    . '</p>';
            }
            return;
        }

        $wrapper_classes = implode( ' ', array_filter( [
            'sitsel-pnav-wrapper',
            'sitsel-pnav-style-' . $style,
        ] ) );

        printf( '<div class="%s">', esc_attr( $wrapper_classes ) );

        $this->render_nav_item( $prev_post, 'prev', $style, $settings );

        if ( 'yes' === ( $settings['show_divider'] ?? 'yes' ) && 'minimal' !== $style ) {
            echo '<div class="sitsel-pnav-divider" aria-hidden="true"></div>';
        }

        $this->render_nav_item( $next_post, 'next', $style, $settings );

        echo '</div><!-- .sitsel-pnav-wrapper -->';
    }

    // ── Single nav item ───────────────────────────────────────────────────
    private function render_nav_item( $post, string $direction, string $style, array $settings ): void {
        $is_prev     = ( 'prev' === $direction );
        $show_label  = ( 'yes' === ( $settings['show_label']  ?? 'yes' ) );
        $show_title  = ( 'yes' === ( $settings['show_title']  ?? 'yes' ) );
        $show_arrow  = ( 'yes' === ( $settings['show_arrow']  ?? 'yes' ) );
        $show_thumb  = ( 'yes' === ( $settings['show_thumbnail'] ?? 'yes' ) ) && ( 'card-thumb' === $style );
        $title_words = absint( $settings['title_words'] ?? 6 );
        $label_text  = esc_html( $is_prev ? ( $settings['prev_label'] ?? __( 'Previous Post', 'softech-addons-for-elementor' ) ) : ( $settings['next_label'] ?? __( 'Next Post', 'softech-addons-for-elementor' ) ) );

        // Empty placeholder — maintains layout
        if ( ! $post ) {
            printf(
                '<div class="sitsel-pnav-item sitsel-pnav-%s sitsel-pnav-empty"></div>',
                esc_attr( $direction )
            );
            return;
        }

       
        $title = esc_html( wp_trim_words( get_the_title( $post ), $title_words, '…' ) );

        $item_classes = implode( ' ', array_filter( [
            'sitsel-pnav-item',
            'sitsel-pnav-' . $direction,
        ] ) );

        printf( '<a href="%s" class="%s">', esc_url( get_permalink( $post ) ), esc_attr( $item_classes ) );

        switch ( $style ) {
           case 'card-thumb': $this->render_style_card_thumb( $settings, $is_prev, $label_text, $title, $show_arrow, $show_label, $show_title, $show_thumb, $post ); break;
           case 'minimal':    $this->render_style_minimal(    $settings, $is_prev, $label_text, $title, $show_arrow, $show_label, $show_title ); break;
        }

        echo '</a>';
    }

    // ── Style renderers ───────────────────────────────────────────────────
 /**
     * Style 1 – Card with thumbnail.
     */
    private function render_style_card_thumb(
        array $settings, bool $is_prev,
        string $label, string $title,
        bool $show_arrow, bool $show_label, bool $show_title,
        bool $show_thumb, $post
    ): void {
        if ( $is_prev && $show_arrow ) $this->render_arrow_box( $settings, 'prev' );
        if ( ! $is_prev && $show_arrow ) $this->render_arrow_box( $settings, 'next' );

        if ( $show_thumb && has_post_thumbnail( $post ) ) {
            $thumb_size = sanitize_key( $settings['thumbnail_size'] ?? 'thumbnail' );
            $thumb_url  = get_the_post_thumbnail_url( $post, $thumb_size );
            if ( $thumb_url ) {
                printf(
                    '<div class="sitsel-pnav-thumb" style="background-image:url(%s);" role="img" aria-label="%s"></div>',
                    esc_url( $thumb_url ),
                    esc_attr( get_the_title( $post ) )
                );
            }
        }

        echo '<div class="sitsel-pnav-text">';
       if ( $show_label ) echo '<span class="sitsel-pnav-label">' . esc_html( $label ) . '</span>';
       if ( $show_title ) echo '<span class="sitsel-pnav-title">' . esc_html( $title ) . '</span>';
        echo '</div>';

    }

    /**
     * Style 2 – Minimal underline.
     * No box on the item; relies on CSS border-top / border-bottom from wrapper.
     */
    private function render_style_minimal(
        array $settings, bool $is_prev,
        string $label, string $title,
        bool $show_arrow, bool $show_label, bool $show_title
    ): void {
        echo '<div class="sitsel-pnav-inner">';
        if ( $is_prev && $show_arrow ) $this->render_arrow_box( $settings, 'prev' );
        echo '<div class="sitsel-pnav-text">';
        if ( $show_label ) echo '<span class="sitsel-pnav-label">' . esc_html( $label ) . '</span>';
        if ( $show_title ) echo '<span class="sitsel-pnav-title">' . esc_html( $title ) . '</span>';
        echo '</div>';
        if ( ! $is_prev && $show_arrow ) $this->render_arrow_box( $settings, 'next' );
        echo '</div>';
    }

    

    // ── Arrow helpers ─────────────────────────────────────────────────────

    /**
     * Renders the arrow wrapper box + icon inside it.
     * Same pattern as render_dot() in the timeline widget.
     */
    private function render_arrow_box( array $settings, string $direction ): void {
        echo '<div class="sitsel-pnav-arrow" aria-hidden="true">';
        $this->render_arrow_icon( $settings, $direction );
        echo '</div>';
    }

    /**
     * Renders only the icon (used both in arrow-box and badge).
     * Falls back to a unicode arrow if no icon is set.
     */
    private function render_arrow_icon( array $settings, string $direction ): void {
        $icon_key = ( 'prev' === $direction ) ? 'prev_icon' : 'next_icon';
        $icon     = $settings[ $icon_key ] ?? [];

        if ( ! empty( $icon['value'] ) ) {
            \Elementor\Icons_Manager::render_icon( $icon, [ 'aria-hidden' => 'true' ] );
        } else {
            echo ( 'prev' === $direction ) ? '&#8592;' : '&#8594;';
        }
    }

    // ── Adjacent post helper ──────────────────────────────────────────────

    /**
     * Returns the adjacent WP_Post object or false.
     * Mirrors the get_adjacent() helper from the previous standalone version.
     *
     * @param string $direction 'prev' | 'next'
     * @param bool   $same_cat
     * @return \WP_Post|false
     */
    private function get_adjacent_post( string $direction = 'prev', bool $same_cat = false ) {
        return get_adjacent_post(
            $same_cat,
            '',
            ( 'prev' === $direction ) // true = previous
        );
    }
}