<?php
/**
 * Features Cards Widget
 *
 * @package AI_Theme
 */

namespace AITheme\Elementor\Widgets;

use Elementor\Controls_Manager;
use Elementor\Repeater;

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

/**
 * Features Cards Widget
 */
class Features_Cards extends AI_Widget_Base {

    public function get_name() {
        return 'ai_features_cards';
    }

    public function get_title() {
        return __('Features Cards', 'ai-theme');
    }

    public function get_icon() {
        return 'eicon-info-box';
    }

    public function get_categories() {
        return array('ai-theme-blocks');
    }

    public function get_keywords() {
        return array('features', 'cards', 'services', 'ai');
    }

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

        $this->add_control(
            'heading',
            array(
                'label'       => __('Heading', 'ai-theme'),
                'type'        => Controls_Manager::TEXT,
                'default'     => __('Our Features', 'ai-theme'),
                'label_block' => true,
            )
        );

        $this->add_control(
            'description',
            array(
                'label'   => __('Description', 'ai-theme'),
                'type'    => Controls_Manager::TEXTAREA,
                'default' => __('Discover what makes us special', 'ai-theme'),
            )
        );

        $repeater = new Repeater();

        $repeater->add_control(
            'title',
            array(
                'label'   => __('Title', 'ai-theme'),
                'type'    => Controls_Manager::TEXT,
                'default' => __('Feature Title', 'ai-theme'),
            )
        );

        $repeater->add_control(
            'description',
            array(
                'label'   => __('Description', 'ai-theme'),
                'type'    => Controls_Manager::TEXTAREA,
                'default' => __('Feature description text', 'ai-theme'),
            )
        );

        $repeater->add_control(
            'icon',
            array(
                'label'   => __('Icon', 'ai-theme'),
                'type'    => Controls_Manager::ICONS,
                'default' => array(
                    'value'   => 'fas fa-star',
                    'library' => 'fa-solid',
                ),
            )
        );

        $this->add_control(
            'features',
            array(
                'label'   => __('Features', 'ai-theme'),
                'type'    => Controls_Manager::REPEATER,
                'fields'  => $repeater->get_controls(),
                'default' => array(
                    array('title' => 'Feature 1', 'description' => 'Description 1'),
                    array('title' => 'Feature 2', 'description' => 'Description 2'),
                    array('title' => 'Feature 3', 'description' => 'Description 3'),
                ),
            )
        );

        $this->end_controls_section();
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        ?>
        <div class="ai-features-cards py-16 bg-gray-50">
            <div class="ai-container">
                <div class="ai-features-header text-center mb-12">
                    <h2 class="text-3xl md:text-4xl font-bold text-gray-900 mb-4"><?php echo esc_html($settings['heading']); ?></h2>
                    <p class="text-lg text-gray-600"><?php echo esc_html($settings['description']); ?></p>
                </div>
                <div class="ai-features-grid grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
                    <?php foreach ($settings['features'] as $feature) : ?>
                        <div class="ai-feature-card bg-white p-6 rounded-xl shadow-sm hover:shadow-md transition-shadow text-center">
                            <?php if (!empty($feature['icon']['value'])) : ?>
                                <div class="ai-feature-icon w-16 h-16 mx-auto mb-4 flex items-center justify-center rounded-full bg-primary/10">
                                    <i class="<?php echo esc_attr($feature['icon']['value']); ?> text-2xl text-primary"></i>
                                </div>
                            <?php endif; ?>
                            <h3 class="text-lg font-semibold text-gray-900 mb-2"><?php echo esc_html($feature['title']); ?></h3>
                            <div class="text-sm text-gray-600 prose prose-sm max-w-none"><?php echo wp_kses_post($feature['description']); ?></div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
        <?php
    }
}
