<?php
namespace AITheme\Elementor\Widgets;

use Elementor\Controls_Manager;
use Elementor\Repeater;

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

class Pricing_V1 extends AI_Widget_Base {
    public function get_name() { return 'ai_pricing_v1'; }
    public function get_title() { return __('Pricing V1', 'ai-theme'); }
    public function get_icon() { return 'eicon-price-table'; }
    public function get_categories() { return array('ai-theme-pricing'); }

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

        $repeater = new Repeater();
        $repeater->add_control('name', ['label' => __('Plan Name', 'ai-theme'), 'type' => Controls_Manager::TEXT]);
        $repeater->add_control('price', ['label' => __('Price', 'ai-theme'), 'type' => Controls_Manager::TEXT]);
        $repeater->add_control('period', ['label' => __('Period', 'ai-theme'), 'type' => Controls_Manager::TEXT, 'default' => '/month']);
        $repeater->add_control('description', ['label' => __('Description', 'ai-theme'), 'type' => Controls_Manager::TEXTAREA]);
        $repeater->add_control('features', ['label' => __('Features (one per line)', 'ai-theme'), 'type' => Controls_Manager::TEXTAREA]);
        $repeater->add_control('button_text', ['label' => __('Button Text', 'ai-theme'), 'type' => Controls_Manager::TEXT, 'default' => __('Choose Plan', 'ai-theme')]);
        $repeater->add_control('highlighted', ['label' => __('Highlighted', 'ai-theme'), 'type' => Controls_Manager::SWITCHER]);
        $this->add_control('plans', ['label' => __('Plans', '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-pricing ai-pricing-v1 bg-white py-16">
            <div class="ai-container">
                <div class="ai-pricing-header text-center mb-12">
                    <?php if (!empty($s['subtitle'])) : ?><span class="ai-pricing-subtitle text-lg text-gray-600 block mb-2"><?php echo esc_html($s['subtitle']); ?></span><?php endif; ?>
                    <h2 class="ai-pricing-title text-4xl font-bold text-gray-900"><?php echo esc_html($s['title']); ?></h2>
                </div>
                <div class="ai-pricing-plans grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
                    <?php foreach ($s['plans'] as $p) :
                        $is_highlighted = $p['highlighted'] === 'yes';
                        $card_classes = $is_highlighted
                            ? 'border-primary shadow-xl scale-105'
                            : 'border-gray-200 shadow-sm';
                    ?>
                    <div class="ai-pricing-plan<?php echo $is_highlighted ? ' ai-pricing-highlighted' : ''; ?> relative bg-white rounded-2xl p-8 border-2 <?php echo $card_classes; ?> transition-all hover:shadow-lg">
                        <?php if ($is_highlighted) : ?>
                            <div class="absolute -top-4 left-1/2 -translate-x-1/2 bg-gradient-to-r from-primary to-secondary text-white px-4 py-1 rounded-full text-sm font-semibold">Popular</div>
                        <?php endif; ?>
                        <h3 class="text-2xl font-bold text-gray-900 mb-2"><?php echo esc_html($p['name']); ?></h3>
                        <div class="ai-pricing-price mb-6">
                            <span class="text-5xl font-bold text-gray-900"><?php echo esc_html($p['price']); ?></span>
                            <span class="text-gray-600"><?php echo esc_html($p['period']); ?></span>
                        </div>
                        <?php if (!empty($p['description'])) : ?><p class="text-gray-600 mb-4"><?php echo esc_html($p['description']); ?></p><?php endif; ?>
                        <?php if (!empty($p['features'])) : ?>
                        <ul class="ai-pricing-features space-y-3 mb-8">
                            <?php foreach (explode("\n", $p['features']) as $f) : if (trim($f)) : ?>
                                <li class="flex items-center gap-2">
                                    <span class="text-primary">✓</span>
                                    <span class="text-gray-700"><?php echo esc_html(trim($f)); ?></span>
                                </li>
                            <?php endif; endforeach; ?>
                        </ul>
                        <?php endif; ?>
                        <a href="#" class="ai-pricing-button block w-full py-3 text-center rounded-xl font-semibold transition-all <?php echo $is_highlighted ? 'bg-gradient-to-r from-primary to-secondary text-white hover:shadow-lg' : 'bg-gray-100 text-gray-900 hover:bg-gray-200'; ?>"><?php echo esc_html($p['button_text']); ?></a>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </section>
        <?php
    }
}
