<?php

namespace Elementor;

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

class feminine_mentor_pro_Elementor_Blog extends Widget_Base {
    
    public function get_name() {
        return 'feminine-mentor-blog';
    }
    
    public function get_title() {
        return __('Blog Widget', 'feminine-mentor');
    }
    
    public function get_icon() {
        return 'eicon-posts-grid';
    }
    
    public function get_categories() {
        return ['feminine-mentor-elementor-widget'];
    }
    
    protected function register_controls() {
        // Content Section
        $this->start_controls_section(
            'content_section',
            [
                'label' => __('Content Settings', 'feminine-mentor'),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'blog_category',
            [
                'label' => __('Select Category', 'feminine-mentor'),
                'type' => Controls_Manager::SELECT2,
                'options' => $this->get_blog_categories(),
                'multiple' => true,
                'label_block' => true,
            ]
        );

        $this->add_control(
            'posts_per_page',
            [
                'label' => __('Number of Posts', 'feminine-mentor'),
                'type' => Controls_Manager::NUMBER,
                'default' => 3,
                'min' => 1,
                'max' => 50,
            ]
        );

        $this->add_control(
            'orderby',
            [
                'label' => __('Order By', 'feminine-mentor'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'date' => __('Date', 'feminine-mentor'),
                    'title' => __('Title', 'feminine-mentor'),
                    'name' => __('Slug', 'feminine-mentor'),
                    'modified' => __('Last Modified', 'feminine-mentor'),
                    'comment_count' => __('Comment Count', 'feminine-mentor'),
                    'rand' => __('Random', 'feminine-mentor'),
                ],
                'default' => 'date',
            ]
        );

        $this->add_control(
            'order',
            [
                'label' => __('Order', 'feminine-mentor'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'desc' => __('Descending', 'feminine-mentor'),
                    'asc' => __('Ascending', 'feminine-mentor'),
                ],
                'default' => 'desc',
            ]
        );

        $this->add_control(
            'show_image',
            [
                'label' => __('Show Featured Image', 'feminine-mentor'),
                'type' => Controls_Manager::SWITCHER,
                'label_on' => __('Show', 'feminine-mentor'),
                'label_off' => __('Hide', 'feminine-mentor'),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'show_excerpt',
            [
                'label' => __('Show Excerpt', 'feminine-mentor'),
                'type' => Controls_Manager::SWITCHER,
                'label_on' => __('Show', 'feminine-mentor'),
                'label_off' => __('Hide', 'feminine-mentor'),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'excerpt_length',
            [
                'label' => __('Excerpt Length', 'feminine-mentor'),
                'type' => Controls_Manager::NUMBER,
                'default' => 20,
                'min' => 5,
                'max' => 100,
                'condition' => [
                    'show_excerpt' => 'yes',
                ],
            ]
        );

        $this->add_control(
            'show_meta',
            [
                'label' => __('Show Meta', 'feminine-mentor'),
                'type' => Controls_Manager::SWITCHER,
                'label_on' => __('Show', 'feminine-mentor'),
                'label_off' => __('Hide', 'feminine-mentor'),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'show_category',
            [
                'label' => __('Show Category', 'feminine-mentor'),
                'type' => Controls_Manager::SWITCHER,
                'label_on' => __('Show', 'feminine-mentor'),
                'label_off' => __('Hide', 'feminine-mentor'),
                'return_value' => 'yes',
                'default' => 'yes',
                'condition' => [
                    'show_meta' => 'yes',
                ],
            ]
        );

        $this->add_control(
            'show_date',
            [
                'label' => __('Show Date', 'feminine-mentor'),
                'type' => Controls_Manager::SWITCHER,
                'label_on' => __('Show', 'feminine-mentor'),
                'label_off' => __('Hide', 'feminine-mentor'),
                'return_value' => 'yes',
                'default' => 'yes',
                'condition' => [
                    'show_meta' => 'yes',
                ],
            ]
        );

        $this->end_controls_section();

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

        $this->add_control(
            'layout',
            [
                'label' => __('Layout', 'feminine-mentor'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'grid' => __('Grid', 'feminine-mentor'),
                    'list' => __('List', 'feminine-mentor'),
                ],
                'default' => 'grid',
            ]
        );

        $this->add_control(
            'columns',
            [
                'label' => __('Columns', 'feminine-mentor'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    '1' => __('1 Column', 'feminine-mentor'),
                    '2' => __('2 Columns', 'feminine-mentor'),
                    '3' => __('3 Columns', 'feminine-mentor'),
                    '4' => __('4 Columns', 'feminine-mentor'),
                ],
                'default' => '3',
                'condition' => [
                    'layout' => 'grid',
                ],
            ]
        );

        $this->add_control(
            'column_gap',
            [
                'label' => __('Column Gap', 'feminine-mentor'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px'],
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => 30,
                ],
                'selectors' => [
                    '{{WRAPPER}} .feminine-mentor-blog-grid' => 'gap: {{SIZE}}{{UNIT}};',
                ],
                'condition' => [
                    'layout' => 'grid',
                ],
            ]
        );

        $this->add_control(
            'row_gap',
            [
                'label' => __('Row Gap', 'feminine-mentor'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px'],
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => 30,
                ],
                'selectors' => [
                    '{{WRAPPER}} .feminine-mentor-blog-grid' => 'row-gap: {{SIZE}}{{UNIT}};',
                    '{{WRAPPER}} .feminine-mentor-blog-list .blog-post-item:not(:last-child)' => 'margin-bottom: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();

        // Style Section - Image
        $this->start_controls_section(
            'image_style_section',
            [
                'label' => __('Image Style', 'feminine-mentor'),
                'tab' => Controls_Manager::TAB_STYLE,
                'condition' => [
                    'show_image' => 'yes',
                ],
            ]
        );

        $this->add_control(
            'image_height',
            [
                'label' => __('Image Height', 'feminine-mentor'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', '%'],
                'range' => [
                    'px' => [
                        'min' => 100,
                        'max' => 500,
                    ],
                    '%' => [
                        'min' => 10,
                        'max' => 100,
                    ],
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => 200,
                ],
                'selectors' => [
                    '{{WRAPPER}} .blog-post-thumbnail img' => 'height: {{SIZE}}{{UNIT}}; object-fit: cover;',
                ],
            ]
        );

        $this->add_control(
            'image_border_radius',
            [
                'label' => __('Border Radius', 'feminine-mentor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors' => [
                    '{{WRAPPER}} .blog-post-thumbnail' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                    '{{WRAPPER}} .blog-post-thumbnail img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'image_spacing',
            [
                'label' => __('Spacing', 'feminine-mentor'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px'],
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 50,
                    ],
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => 15,
                ],
                'selectors' => [
                    '{{WRAPPER}} .blog-post-thumbnail' => 'margin-bottom: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $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,
                'selectors' => [
                    '{{WRAPPER}} .blog-post-title a' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'title_hover_color',
            [
                'label' => __('Hover Color', 'feminine-mentor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .blog-post-title a:hover' => 'color: {{VALUE}};',
                ],
            ]
        );

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

        $this->add_control(
            'title_spacing',
            [
                'label' => __('Spacing', 'feminine-mentor'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px'],
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 50,
                    ],
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => 10,
                ],
                'selectors' => [
                    '{{WRAPPER}} .blog-post-title' => 'margin-bottom: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();

        // Style Section - Excerpt
        $this->start_controls_section(
            'excerpt_style_section',
            [
                'label' => __('Excerpt Style', 'feminine-mentor'),
                'tab' => Controls_Manager::TAB_STYLE,
                'condition' => [
                    'show_excerpt' => 'yes',
                ],
            ]
        );

        $this->add_control(
            'excerpt_color',
            [
                'label' => __('Color', 'feminine-mentor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .blog-post-excerpt' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'excerpt_typography',
                'selector' => '{{WRAPPER}} .blog-post-excerpt',
            ]
        );

        $this->add_control(
            'excerpt_spacing',
            [
                'label' => __('Spacing', 'feminine-mentor'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px'],
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 50,
                    ],
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => 15,
                ],
                'selectors' => [
                    '{{WRAPPER}} .blog-post-excerpt' => 'margin-bottom: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();

        // Style Section - Meta
        $this->start_controls_section(
            'meta_style_section',
            [
                'label' => __('Meta Style', 'feminine-mentor'),
                'tab' => Controls_Manager::TAB_STYLE,
                'condition' => [
                    'show_meta' => 'yes',
                ],
            ]
        );

        $this->add_control(
            'meta_color',
            [
                'label' => __('Color', 'feminine-mentor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .blog-post-meta' => 'color: {{VALUE}};',
                    '{{WRAPPER}} .blog-post-meta a' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'meta_hover_color',
            [
                'label' => __('Hover Color', 'feminine-mentor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .blog-post-meta a:hover' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'meta_typography',
                'selector' => '{{WRAPPER}} .blog-post-meta',
            ]
        );

        $this->add_control(
            'meta_spacing',
            [
                'label' => __('Spacing', 'feminine-mentor'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px'],
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 50,
                    ],
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => 10,
                ],
                'selectors' => [
                    '{{WRAPPER}} .blog-post-meta' => 'margin-bottom: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();
    }

    // Helper function to get blog categories
    private function get_blog_categories() {
        $categories = get_categories([
            'hide_empty' => true,
        ]);
        
        $options = [];
        if ($categories) {
            foreach ($categories as $category) {
                $options[$category->term_id] = $category->name;
            }
        }
        return $options;
    }

    // Helper function to get post excerpt
    private function get_post_excerpt($excerpt_length = 20) {
        $excerpt = get_the_excerpt();
        if (empty($excerpt)) {
            $excerpt = get_the_content();
        }
        
        $excerpt = wp_trim_words($excerpt, $excerpt_length, '...');
        return $excerpt;
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        
        // Query arguments
        $args = [
            'post_type' => 'post',
            'posts_per_page' => $settings['posts_per_page'],
            'post_status' => 'publish',
            'orderby' => $settings['orderby'],
            'order' => $settings['order'],
        ];

        // Add category filter if selected
        if (!empty($settings['blog_category'])) {
            $args['tax_query'] = [
                [
                    'taxonomy' => 'category',
                    'field' => 'term_id',
                    'terms' => $settings['blog_category'],
                ],
            ];
        }

        $blog_query = new \WP_Query($args);

        // Layout classes
        $layout_class = 'feminine-mentor-blog-' . $settings['layout'];
        $grid_class = ($settings['layout'] === 'grid') ? 'feminine-mentor-blog-grid feminine-mentor-blog-columns-' . $settings['columns'] : 'feminine-mentor-blog-list';

        ?>
        <div class="feminine-mentor-blog-widget <?php echo esc_attr($layout_class); ?>">
            <div class="<?php echo esc_attr($grid_class); ?>">
                <?php
                if ($blog_query->have_posts()) :
                    while ($blog_query->have_posts()) : $blog_query->the_post();
                        ?>
                        <article class="blog-post-item">
                            <div class="blog-post-inner">
                                <?php if ($settings['show_image'] === 'yes' && has_post_thumbnail()) : ?>
                                    <div class="blog-post-thumbnail">
                                        <a href="<?php the_permalink(); ?>">
                                            <?php the_post_thumbnail('medium_large'); ?>
                                        </a>
                                    </div>
                                <?php endif; ?>

                                <div class="blog-post-content">
                                    <h3 class="blog-post-title">
                                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                    </h3>

                                    <?php if ($settings['show_meta'] === 'yes') : ?>
                                        <div class="blog-post-meta">
                                            <?php if ($settings['show_date'] === 'yes') : ?>
                                                <span class="post-date">
                                                    <?php echo get_the_date(); ?>
                                                </span>
                                            <?php endif; ?>

                                            <?php if ($settings['show_category'] === 'yes') : ?>
                                                <?php if ($settings['show_date'] === 'yes') : ?>
                                                    <span class="meta-separator">|</span>
                                                <?php endif; ?>
                                                <span class="post-categories">
                                                    <?php
                                                    $categories = get_the_category();
                                                    if (!empty($categories)) {
                                                        $category_links = [];
                                                        foreach ($categories as $category) {
                                                            $category_links[] = '<a href="' . esc_url(get_category_link($category->term_id)) . '">' . esc_html($category->name) . '</a>';
                                                        }
                                                        echo implode(', ', $category_links);
                                                    }
                                                    ?>
                                                </span>
                                            <?php endif; ?>
                                        </div>
                                    <?php endif; ?>

                                    <?php if ($settings['show_excerpt'] === 'yes') : ?>
                                        <div class="blog-post-excerpt">
                                            <?php echo wp_kses_post($this->get_post_excerpt($settings['excerpt_length'])); ?>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </article>
                        <?php
                    endwhile;
                    wp_reset_postdata();
                else :
                    echo '<p>' . __('No posts found.', 'feminine-mentor') . '</p>';
                endif;
                ?>
            </div>
        </div>
        <?php
    }
}

// Register the widget
Plugin::instance()->widgets_manager->register_widget_type(new feminine_mentor_pro_Elementor_Blog());