<?php
namespace AITheme\Elementor\Widgets;

use Elementor\Controls_Manager;
use Elementor\Repeater;

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

class Integrations_Grid extends AI_Widget_Base {
    public function get_name() { return 'ai_integrations_grid'; }
    public function get_title() { return __('Integrations Grid', 'ai-theme'); }
    public function get_icon() { return 'eicon-apps'; }
    public function get_categories() { return array('ai-theme-integrations'); }

    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('logo', ['label' => __('Logo', 'ai-theme'), 'type' => Controls_Manager::MEDIA]);
        $repeater->add_control('name', ['label' => __('Name', '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('integrations', ['label' => __('Integrations', '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-integrations ai-integrations-grid w-full bg-gray-50 py-16">
            <div class="ai-container">
                <div class="ai-integrations-header text-center mb-12">
                    <?php if (!empty($s['title'])) : ?>
                    <h2 class="text-3xl md: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="text-lg text-gray-600 max-w-2xl mx-auto"><?php echo esc_html($s['subtitle']); ?></p>
                    <?php endif; ?>
                </div>
                <div class="ai-integrations-items grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-6">
                    <?php foreach ($s['integrations'] as $int) : ?>
                    <div class="ai-integration-item bg-white rounded-xl p-6 hover:shadow-xl transition-all group relative">
                        <?php if (!empty($int['logo']['url'])) : ?>
                        <div class="aspect-square bg-gradient-to-br from-gray-100 to-gray-200 rounded-lg flex items-center justify-center mb-4 group-hover:scale-105 transition-transform">
                            <img src="<?php echo esc_url($int['logo']['url']); ?>" alt="<?php echo esc_attr($int['name']); ?>" class="w-full h-full object-contain p-4">
                        </div>
                        <?php endif; ?>
                        <h3 class="text-lg font-bold text-gray-900 mb-2 text-center"><?php echo esc_html($int['name']); ?></h3>
                        <?php if (!empty($int['description'])) : ?>
                        <div class="opacity-0 group-hover:opacity-100 transition-opacity">
                            <p class="text-sm text-gray-600 text-center line-clamp-2"><?php echo esc_html($int['description']); ?></p>
                        </div>
                        <?php endif; ?>
                        <?php if (!empty($int['link']['url'])) : ?>
                        <a href="<?php echo esc_url($int['link']['url']); ?>" class="absolute bottom-3 right-3 opacity-0 group-hover:opacity-100 transition-opacity text-blue-600 text-sm font-medium"><?php _e('Learn more', 'ai-theme'); ?></a>
                        <?php endif; ?>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </section>
        <?php
    }
}
