<?php

namespace Elementor;

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

class feminine_mentor_elementor_cta extends Widget_Base {
    
    public function get_name() {
        return 'feminine-mentor-cta';
    }
    
    public function get_title() {
        return __( 'Call to Action', 'feminine-mentor' );
    }
    
    public function get_icon() {
        return 'eicon-call-to-action';
    }
    
    public function get_categories() {
        return [ 'feminine-mentor-elementor-widget' ];
    }
    
    protected function register_controls() {
        // Content Section
        $this->start_controls_section(
            'feminine_mentor_cta_section',
            [
                'label' => esc_html__('CTA Content', 'feminine-mentor'),
            ]
        );

        $this->add_control(
            'feminine_mentor_cta_title',
            [
                'label' => __('Title', 'feminine-mentor'),
                'type' => Controls_Manager::TEXT,
                'default' => __( 'Get Started Today', 'feminine-mentor' ),
                'placeholder' => __( 'Enter your title', 'feminine-mentor' ),
                'label_block' => true,
            ]
        );

        $this->add_control(
            'feminine_mentor_cta_desc',
            [
                'label' => __('Description', 'feminine-mentor'),
                'type' => Controls_Manager::TEXTAREA,
                'default' => __( 'Join thousands of satisfied customers and experience the difference.', 'feminine-mentor' ),
                'placeholder' => __( 'Enter your description', 'feminine-mentor' ),
                'label_block' => true,
            ]
        );

        $this->add_control(
            'feminine_mentor_cta_button_text',
            [
                'label' => __('Button Text', 'feminine-mentor'),
                'type' => Controls_Manager::TEXT,
                'default' => __( 'Get Started', 'feminine-mentor' ),
                'placeholder' => __( 'Enter button text', 'feminine-mentor' ),
            ]
        );

        $this->add_control(
            'feminine_mentor_cta_button_url',
            [
                'label' => __( 'Button Link', 'feminine-mentor' ),
                'type' => Controls_Manager::URL,
                'placeholder' => __( 'https://your-link.com', 'feminine-mentor' ),
                'show_external' => true,
                'default' => [
                    'url' => '#',
                    'is_external' => false,
                    'nofollow' => false,
                ],
            ]
        );

        $this->add_control(
            'feminine_mentor_cta_img',
            [
                'label' => __( 'Background Image', 'feminine-mentor' ),
                'type' => Controls_Manager::MEDIA,
                'default' => [
                    'url' => Utils::get_placeholder_image_src(),
                ],
            ]
        );

        $this->add_control(
            'content_alignment',
            [
                'label' => __('Content Alignment', 'feminine-mentor'),
                'type' => Controls_Manager::CHOOSE,
                'options' => [
                    'left' => [
                        'title' => __('Left', 'feminine-mentor'),
                        'icon' => 'eicon-text-align-left',
                    ],
                    'center' => [
                        'title' => __('Center', 'feminine-mentor'),
                        'icon' => 'eicon-text-align-center',
                    ],
                    'right' => [
                        'title' => __('Right', 'feminine-mentor'),
                        'icon' => 'eicon-text-align-right',
                    ],
                ],
                'default' => 'center',
                'toggle' => true,
            ]
        );

        $this->end_controls_section();

        // Style Section - Container
        $this->start_controls_section(
            'container_style_section',
            [
                'label' => __('Container Style', 'feminine-mentor'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'container_min_height',
            [
                'label' => __('Min Height', 'feminine-mentor'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'vh'],
                'range' => [
                    'px' => [
                        'min' => 100,
                        'max' => 1000,
                    ],
                    'vh' => [
                        'min' => 10,
                        'max' => 100,
                    ],
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => 400,
                ],
                'selectors' => [
                    '{{WRAPPER}} .cta-section' => 'min-height: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'container_padding',
            [
                'label' => __('Padding', 'feminine-mentor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors' => [
                    '{{WRAPPER}} .cta-section' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'background_overlay',
            [
                'label' => __('Background Overlay', 'feminine-mentor'),
                'type' => Controls_Manager::COLOR,
                'default' => 'rgba(0,0,0,0.3)',
                'selectors' => [
                    '{{WRAPPER}} .cta-section::before' => 'content: ""; position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: {{VALUE}}; z-index: 1;',
                ],
            ]
        );

        $this->add_control(
            'background_overlay_opacity',
            [
                'label' => __('Overlay Opacity', 'feminine-mentor'),
                'type' => Controls_Manager::SLIDER,
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 1,
                        'step' => 0.1,
                    ],
                ],
                'default' => [
                    'size' => 0.3,
                ],
                'selectors' => [
                    '{{WRAPPER}} .cta-section::before' => 'opacity: {{SIZE}};',
                ],
            ]
        );

        $this->end_controls_section();

        // Style Section - Title
        $this->start_controls_section(
            'title_style_section',
            [
                'label' => __('Title Style', 'feminine-mentor'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'title_color',
            [
                'label' => __('Color', 'feminine-mentor'),
                'type' => Controls_Manager::COLOR,
                'default' => '#ffffff',
                'selectors' => [
                    '{{WRAPPER}} .cta-section h2' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'title_typography',
                'selector' => '{{WRAPPER}} .cta-section h2',
            ]
        );

        $this->add_control(
            'title_margin',
            [
                'label' => __('Margin', 'feminine-mentor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors' => [
                    '{{WRAPPER}} .cta-section h2' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Text_Shadow::get_type(),
            [
                'name' => 'title_text_shadow',
                'selector' => '{{WRAPPER}} .cta-section h2',
            ]
        );

        $this->end_controls_section();

        // Style Section - Description
        $this->start_controls_section(
            'description_style_section',
            [
                'label' => __('Description Style', 'feminine-mentor'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'description_color',
            [
                'label' => __('Color', 'feminine-mentor'),
                'type' => Controls_Manager::COLOR,
                'default' => '#ffffff',
                'selectors' => [
                    '{{WRAPPER}} .cta-section span' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'description_typography',
                'selector' => '{{WRAPPER}} .cta-section span',
            ]
        );

        $this->add_control(
            'description_margin',
            [
                'label' => __('Margin', 'feminine-mentor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors' => [
                    '{{WRAPPER}} .cta-section span' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Text_Shadow::get_type(),
            [
                'name' => 'description_text_shadow',
                'selector' => '{{WRAPPER}} .cta-section span',
            ]
        );

        $this->end_controls_section();

        // Style Section - Button
        $this->start_controls_section(
            'button_style_section',
            [
                'label' => __('Button Style', 'feminine-mentor'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->start_controls_tabs('button_style_tabs');

        // Normal State
        $this->start_controls_tab(
            'button_normal_tab',
            [
                'label' => __('Normal', 'feminine-mentor'),
            ]
        );

        $this->add_control(
            'button_text_color',
            [
                'label' => __('Text Color', 'feminine-mentor'),
                'type' => Controls_Manager::COLOR,
                'default' => '#ffffff',
                'selectors' => [
                    '{{WRAPPER}} .btn-default' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'button_background_color',
            [
                'label' => __('Background Color', 'feminine-mentor'),
                'type' => Controls_Manager::COLOR,
                'default' => '#E63462',
                'selectors' => [
                    '{{WRAPPER}} .btn-default' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'button_border_color',
            [
                'label' => __('Border Color', 'feminine-mentor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .btn-default' => 'border-color: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_tab();

        // Hover State
        $this->start_controls_tab(
            'button_hover_tab',
            [
                'label' => __('Hover', 'feminine-mentor'),
            ]
        );

        $this->add_control(
            'button_hover_text_color',
            [
                'label' => __('Text Color', 'feminine-mentor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .btn-default:hover' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'button_hover_background_color',
            [
                'label' => __('Background Color', 'feminine-mentor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .btn-default:hover' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'button_hover_border_color',
            [
                'label' => __('Border Color', 'feminine-mentor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .btn-default:hover' => 'border-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'button_hover_animation',
            [
                'label' => __('Hover Animation', 'feminine-mentor'),
                'type' => Controls_Manager::HOVER_ANIMATION,
            ]
        );

        $this->end_controls_tab();

        $this->end_controls_tabs();

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'button_typography',
                'selector' => '{{WRAPPER}} .btn-default',
            ]
        );

        $this->add_control(
            'button_border_width',
            [
                'label' => __('Border Width', 'feminine-mentor'),
                'type' => Controls_Manager::SLIDER,
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 10,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .btn-default' => 'border-width: {{SIZE}}{{UNIT}}; border-style: solid;',
                ],
            ]
        );

        $this->add_control(
            'button_border_radius',
            [
                'label' => __('Border Radius', 'feminine-mentor'),
                'type' => Controls_Manager::SLIDER,
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                ],
                'default' => [
                    'size' => 4,
                ],
                'selectors' => [
                    '{{WRAPPER}} .btn-default' => 'border-radius: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'button_padding',
            [
                'label' => __('Padding', 'feminine-mentor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'default' => [
                    'top' => '12',
                    'right' => '30',
                    'bottom' => '12',
                    'left' => '30',
                    'unit' => 'px',
                    'isLinked' => false,
                ],
                'selectors' => [
                    '{{WRAPPER}} .btn-default' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'button_margin',
            [
                'label' => __('Margin', 'feminine-mentor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors' => [
                    '{{WRAPPER}} .btn-default' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name' => 'button_box_shadow',
                'selector' => '{{WRAPPER}} .btn-default',
            ]
        );

        $this->end_controls_section();

        // Style Section - Content Container
        $this->start_controls_section(
            'content_container_style_section',
            [
                'label' => __('Content Container Style', 'feminine-mentor'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'content_max_width',
            [
                'label' => __('Max Width', 'feminine-mentor'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', '%'],
                'range' => [
                    'px' => [
                        'min' => 300,
                        'max' => 1200,
                    ],
                    '%' => [
                        'min' => 10,
                        'max' => 100,
                    ],
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => 800,
                ],
                'selectors' => [
                    '{{WRAPPER}} .cta-section .container' => 'max-width: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'content_background',
            [
                'label' => __('Content Background', 'feminine-mentor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .cta-section .container' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'content_background_opacity',
            [
                'label' => __('Background Opacity', 'feminine-mentor'),
                'type' => Controls_Manager::SLIDER,
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 1,
                        'step' => 0.1,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .cta-section .container' => 'opacity: {{SIZE}};',
                ],
            ]
        );

        $this->add_control(
            'content_padding',
            [
                'label' => __('Content Padding', 'feminine-mentor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors' => [
                    '{{WRAPPER}} .cta-section .container' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'content_border_radius',
            [
                'label' => __('Border Radius', 'feminine-mentor'),
                'type' => Controls_Manager::SLIDER,
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .cta-section .container' => 'border-radius: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        
        $feminine_mentor_cta_title = $settings['feminine_mentor_cta_title'];
        $feminine_mentor_cta_desc = $settings['feminine_mentor_cta_desc'];
        $feminine_mentor_cta_button_text = $settings['feminine_mentor_cta_button_text'];
        $feminine_mentor_cta_img = $settings['feminine_mentor_cta_img']['url'];
        $feminine_mentor_cta_button_url = $settings['feminine_mentor_cta_button_url']['url'];
        $content_alignment = $settings['content_alignment'];
        
        $button_target = $settings['feminine_mentor_cta_button_url']['is_external'] ? ' target="_blank"' : '';
        $button_nofollow = $settings['feminine_mentor_cta_button_url']['nofollow'] ? ' rel="nofollow"' : '';
        
        $image_style = $feminine_mentor_cta_img ? "style='background-image:url(" . esc_url($feminine_mentor_cta_img) . ")'" : '';
        
        $button_class = 'btn-default';
        if (!empty($settings['button_hover_animation'])) {
            $button_class .= ' elementor-animation-' . $settings['button_hover_animation'];
        }
        ?>

        <div class="cta-section section-overlay" <?php echo wp_kses_post($image_style); ?>>
            <div class="container">
                <div class="row">
                    <div class="col-md-12 cta-content cta-align-<?php echo esc_attr($content_alignment); ?>">
                        <?php if ($feminine_mentor_cta_title): ?>
                            <h2><?php echo esc_html($feminine_mentor_cta_title); ?></h2>
                        <?php endif; ?>

                        <?php if ($feminine_mentor_cta_desc): ?>
                            <span class="cta-description"><?php echo esc_html($feminine_mentor_cta_desc); ?></span>
                        <?php endif; ?>

                        <?php if ($feminine_mentor_cta_button_text && $feminine_mentor_cta_button_url): ?>
                            <a href="<?php echo esc_url($feminine_mentor_cta_button_url); ?>" 
                               class="<?php echo esc_attr($button_class); ?>" 
                               <?php echo $button_target . $button_nofollow; ?>>
                                <?php echo esc_html($feminine_mentor_cta_button_text); ?>
                            </a>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>

        <?php
    }
}

Plugin::instance()->widgets_manager->register_widget_type(new feminine_mentor_elementor_cta());