<?php
namespace AITheme\Elementor\Widgets;

use Elementor\Controls_Manager;

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

class Blog_Sidebar extends AI_Widget_Base {
    public function get_name() { return 'ai_blog_sidebar'; }
    public function get_title() { return __('Blog Sidebar (WordPress Posts)', 'ai-theme'); }
    public function get_icon() { return 'eicon-posts-justified'; }
    public function get_categories() { return array('ai-theme-blog'); }

    protected function register_controls() {
        $this->start_controls_section('content_section', ['label' => __('Content', 'ai-theme'), 'tab' => Controls_Manager::TAB_CONTENT]);

        $this->add_control('title', [
            'label' => __('Title', 'ai-theme'),
            'type' => Controls_Manager::TEXT,
            'default' => __('Latest Articles', 'ai-theme'),
        ]);

        $this->add_control('posts_per_page', [
            'label' => __('Number of Posts', 'ai-theme'),
            'type' => Controls_Manager::NUMBER,
            'default' => 5,
            'min' => 1,
            'max' => 20,
        ]);

        $this->add_control('orderby', [
            'label' => __('Order By', 'ai-theme'),
            'type' => Controls_Manager::SELECT,
            'default' => 'date',
            'options' => [
                'date' => __('Date', 'ai-theme'),
                'title' => __('Title', 'ai-theme'),
                'modified' => __('Last Modified', 'ai-theme'),
                'rand' => __('Random', 'ai-theme'),
            ],
        ]);

        $this->add_control('show_excerpt', [
            'label' => __('Show Excerpt', 'ai-theme'),
            'type' => Controls_Manager::SWITCHER,
            'default' => 'yes',
        ]);

        $this->add_control('show_date', [
            'label' => __('Show Date', 'ai-theme'),
            'type' => Controls_Manager::SWITCHER,
            'default' => 'yes',
        ]);

        $this->add_control('sidebar_title', [
            'label' => __('Sidebar Title', 'ai-theme'),
            'type' => Controls_Manager::TEXT,
            'default' => __('Categories', 'ai-theme'),
        ]);

        $this->end_controls_section();
    }

    protected function render() {
        $s = $this->get_settings_for_display();
        $posts = $this->get_posts($s);
        $categories = get_categories(['hide_empty' => true]);
        ?>
        <section class="ai-blog ai-blog-sidebar bg-white py-16">
            <div class="ai-container">
                <div class="grid md:grid-cols-3 gap-12">
                    <div class="ai-blog-main md:col-span-2 space-y-8">
                        <?php if (!empty($s['title'])) : ?><h2 class="text-4xl font-bold text-gray-900 mb-8"><?php echo esc_html($s['title']); ?></h2><?php endif; ?>

                        <?php if (empty($posts)) : ?>
                        <div class="text-center py-12">
                            <p class="text-gray-500"><?php _e('No posts found.', 'ai-theme'); ?></p>
                        </div>
                        <?php else : ?>
                        <div class="ai-blog-posts">
                            <?php foreach ($posts as $post) : $this->render_post($post, $s); endforeach; ?>
                        </div>
                        <?php endif; ?>
                    </div>
                    <aside class="ai-blog-aside space-y-8">
                        <div class="bg-gray-50 p-6 rounded-xl">
                            <?php if (!empty($s['sidebar_title'])) : ?><h3 class="font-bold text-gray-900 mb-4"><?php echo esc_html($s['sidebar_title']); ?></h3><?php endif; ?>
                            <?php if (!empty($categories)) : ?>
                            <ul class="ai-blog-categories space-y-2">
                                <?php foreach ($categories as $cat) : ?>
                                <li>
                                    <a href="<?php echo esc_url(get_category_link($cat->term_id)); ?>" class="flex items-center justify-between text-gray-600 hover:text-blue-600 transition-colors">
                                        <span><?php echo esc_html($cat->name); ?></span>
                                        <span class="text-sm text-gray-400">(<?php echo $cat->count; ?>)</span>
                                    </a>
                                </li>
                                <?php endforeach; ?>
                            </ul>
                            <?php endif; ?>
                        </div>
                    </aside>
                </div>
            </div>
        </section>
        <?php
    }

    private function get_posts($settings) {
        $args = [
            'post_type' => 'post',
            'post_status' => 'publish',
            'posts_per_page' => $settings['posts_per_page'] ?? 5,
            'orderby' => $settings['orderby'] ?? 'date',
            'order' => 'DESC',
        ];

        return (new \WP_Query($args))->posts;
    }

    private function render_post($post, $settings) {
        $thumbnail_url = get_the_post_thumbnail_url($post->ID, 'medium_large');
        ?>
        <article class="ai-blog-post border-b border-gray-200 pb-8 last:border-b-0 mb-8">
            <?php if ($thumbnail_url) : ?>
            <a href="<?php echo esc_url(get_permalink($post)); ?>">
                <img src="<?php echo esc_url($thumbnail_url); ?>" alt="<?php echo esc_attr($post->post_title); ?>" class="w-full h-64 object-cover rounded-xl mb-6">
            </a>
            <?php endif; ?>
            <h3 class="text-2xl font-bold text-gray-900 mb-3">
                <a href="<?php echo esc_url(get_permalink($post)); ?>" class="hover:text-blue-600 transition-colors"><?php echo esc_html($post->post_title); ?></a>
            </h3>
            <?php if ($settings['show_excerpt'] === 'yes') : ?>
            <p class="text-gray-600 mb-4"><?php echo esc_html(wp_trim_words(get_the_excerpt($post), 30)); ?></p>
            <?php endif; ?>
            <?php if ($settings['show_date'] === 'yes') : ?>
            <div class="flex items-center gap-4 text-sm text-gray-500">
                <span class="ai-blog-date"><?php echo esc_html(get_the_date('', $post)); ?></span>
            </div>
            <?php endif; ?>
        </article>
        <?php
    }
}
