<?php
namespace AITheme\Elementor\Widgets;

use Elementor\Controls_Manager;
use Elementor\Repeater;

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

class Portfolio_Hover extends AI_Widget_Base {
    public function get_name() { return 'ai_portfolio_hover'; }
    public function get_title() { return __('Portfolio Hover', 'ai-theme'); }
    public function get_icon() { return 'eicon-image-rollover'; }
    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]);

        $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('description', ['label' => __('Description', 'ai-theme'), 'type' => Controls_Manager::TEXTAREA]);
        $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-hover bg-gradient-to-br from-gray-900 to-gray-800 py-16">
            <div class="ai-container">
                <?php if (!empty($s['title'])) : ?>
                <div class="text-center mb-12">
                    <h2 class="ai-portfolio-title text-4xl font-bold text-white mb-4"><?php echo esc_html($s['title']); ?></h2>
                </div>
                <?php endif; ?>

                <div class="ai-portfolio-items grid md:grid-cols-2 lg:grid-cols-4 gap-6">
                    <?php foreach ($s['items'] as $item) : ?>
                    <div class="ai-portfolio-item group relative overflow-hidden rounded-2xl aspect-square 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-full object-cover"
                            >
                            <div class="ai-portfolio-hover-content absolute inset-0 bg-gradient-to-t from-blue-600/90 via-purple-600/70 to-transparent translate-y-full group-hover:translate-y-0 transition-transform duration-300 flex flex-col justify-end p-6">
                                <h3 class="text-xl font-bold text-white mb-2"><?php echo esc_html($item['title']); ?></h3>
                                <?php if (!empty($item['description'])) : ?>
                                <p class="text-white/90 text-sm mb-3 line-clamp-2"><?php echo esc_html($item['description']); ?></p>
                                <?php endif; ?>
                            </div>
                        </a>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </section>
        <?php
    }
}
