<?php
namespace AITheme\Elementor\Widgets;

use Elementor\Controls_Manager;
use Elementor\Repeater;

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

class Portfolio_Grid extends AI_Widget_Base {
    public function get_name() { return 'ai_portfolio_grid'; }
    public function get_title() { return __('Portfolio Grid', 'ai-theme'); }
    public function get_icon() { return 'eicon-gallery-grid'; }
    public function get_categories() { return array('ai-theme-portfolio'); }

    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]);
        $this->add_control('subtitle', ['label' => __('Subtitle', 'ai-theme'), 'type' => Controls_Manager::TEXT]);

        $repeater = new Repeater();
        $repeater->add_control('image', ['label' => __('Image', 'ai-theme'), 'type' => Controls_Manager::MEDIA]);
        $repeater->add_control('title', ['label' => __('Title', 'ai-theme'), 'type' => Controls_Manager::TEXT]);
        $repeater->add_control('category', ['label' => __('Category', 'ai-theme'), 'type' => Controls_Manager::TEXT]);
        $repeater->add_control('link', ['label' => __('Link', 'ai-theme'), 'type' => Controls_Manager::URL]);
        $this->add_control('items', ['label' => __('Portfolio Items', 'ai-theme'), 'type' => Controls_Manager::REPEATER, 'fields' => $repeater->get_controls()]);
        $this->end_controls_section();
    }

    protected function render() {
        $s = $this->get_settings_for_display();
        ?>
        <section class="ai-portfolio ai-portfolio-grid bg-white py-16">
            <div class="ai-container">
                <div class="ai-portfolio-header text-center mb-12">
                    <?php if (!empty($s['title'])) : ?>
                    <h2 class="ai-portfolio-title text-4xl font-bold text-gray-900 mb-4"><?php echo esc_html($s['title']); ?></h2>
                    <?php endif; ?>
                    <?php if (!empty($s['subtitle'])) : ?>
                    <p class="ai-portfolio-subtitle text-lg text-gray-600"><?php echo esc_html($s['subtitle']); ?></p>
                    <?php endif; ?>
                </div>

                <div class="ai-portfolio-items grid md:grid-cols-2 gap-8">
                    <?php foreach ($s['items'] as $item) : ?>
                    <div class="ai-portfolio-item group relative overflow-hidden rounded-2xl bg-gray-900 cursor-pointer">
                        <a href="<?php echo esc_url($item['link']['url'] ?? '#'); ?>">
                            <img
                                src="<?php echo esc_url($item['image']['url']); ?>"
                                alt="<?php echo esc_attr($item['title']); ?>"
                                class="w-full h-80 object-cover opacity-80 group-hover:opacity-50 group-hover:scale-110 transition-all duration-500"
                            >
                            <div class="ai-portfolio-overlay absolute inset-0 bg-gradient-to-t from-black via-black/50 to-transparent flex flex-col justify-end p-8">
                                <?php if (!empty($item['category'])) : ?>
                                <span class="inline-block px-3 py-1 bg-blue-600 text-white text-sm font-semibold rounded-full mb-3 self-start">
                                    <?php echo esc_html($item['category']); ?>
                                </span>
                                <?php endif; ?>
                                <h3 class="text-3xl font-bold text-white mb-2"><?php echo esc_html($item['title']); ?></h3>
                            </div>
                        </a>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </section>
        <?php
    }
}
