<?php
namespace AITheme\Elementor\Widgets;

use Elementor\Controls_Manager;

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

class Newsletter_Split extends AI_Widget_Base {
    public function get_name() { return 'ai_newsletter_split'; }
    public function get_title() { return __('Newsletter Split', 'ai-theme'); }
    public function get_icon() { return 'eicon-call-to-action'; }
    public function get_categories() { return array('ai-theme-newsletter'); }

    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' => '']);
        $this->add_control('description', ['label' => __('Description', 'ai-theme'), 'type' => Controls_Manager::TEXTAREA]);
        $this->add_control('image', ['label' => __('Image', 'ai-theme'), 'type' => Controls_Manager::MEDIA]);
        $this->add_control('placeholder', ['label' => __('Placeholder', 'ai-theme'), 'type' => Controls_Manager::TEXT, 'default' => '']);
        $this->add_control('button_text', ['label' => __('Button Text', 'ai-theme'), 'type' => Controls_Manager::TEXT, 'default' => '']);
        $this->add_control('privacy_text', ['label' => __('Privacy Text', 'ai-theme'), 'type' => Controls_Manager::TEXT, 'default' => '']);
        $this->end_controls_section();
    }

    /**
     * Detect language from content (checks for Cyrillic characters)
     */
    private function detectLanguage($settings) {
        $textToCheck = ($settings['title'] ?? '') . ($settings['description'] ?? '') . ($settings['button_text'] ?? '');
        return preg_match('/[\p{Cyrillic}]/u', $textToCheck) ? 'ru' : 'en';
    }

    protected function render() {
        $s = $this->get_settings_for_display();
        $lang = $this->detectLanguage($s);
        $isRu = $lang === 'ru';

        $defaults = $isRu ? [
            'placeholder' => 'Введите ваш email',
            'button_text' => 'Подписаться',
            'email_label' => 'Email адрес',
            'privacy_text' => 'Я согласен с политикой конфиденциальности',
            'benefits' => [
                'Эксклюзивные советы и идеи',
                'Ранний доступ к новым функциям',
                'Специальные предложения и скидки',
            ],
        ] : [
            'placeholder' => 'Enter your email',
            'button_text' => 'Subscribe',
            'email_label' => 'Email address',
            'privacy_text' => 'I agree to the privacy policy',
            'benefits' => [
                'Exclusive tips and insights',
                'Early access to new features',
                'Special offers and discounts',
            ],
        ];

        $placeholder = !empty($s['placeholder']) ? $s['placeholder'] : $defaults['placeholder'];
        $buttonText = !empty($s['button_text']) ? $s['button_text'] : $defaults['button_text'];
        $privacyText = !empty($s['privacy_text']) ? $s['privacy_text'] : $defaults['privacy_text'];
        $consentId = 'consent-' . $this->get_id();
        ?>
        <section class="ai-newsletter ai-newsletter-split w-full bg-gray-50 py-16">
            <div class="ai-container">
                <div class="bg-white rounded-2xl shadow-xl overflow-hidden">
                    <div class="grid grid-cols-1 lg:grid-cols-2">
                        <div class="p-8 md:p-12" style="background: linear-gradient(to bottom right, var(--color-primary), var(--color-secondary));">
                            <div class="h-full flex flex-col justify-center">
                                <?php if (!empty($s['title'])) : ?>
                                    <h2 class="text-3xl font-bold text-white mb-4"><?php echo esc_html($s['title']); ?></h2>
                                <?php endif; ?>
                                <?php if (!empty($s['description'])) : ?>
                                    <p class="text-lg text-white/90 mb-8"><?php echo esc_html($s['description']); ?></p>
                                <?php endif; ?>
                                <ul class="space-y-4">
                                    <?php foreach ($defaults['benefits'] as $benefit) : ?>
                                    <li class="flex items-start gap-3">
                                        <div class="flex-shrink-0 w-6 h-6 bg-white rounded-full flex items-center justify-center">
                                            <span style="color: var(--color-primary);">✓</span>
                                        </div>
                                        <span class="text-white"><?php echo esc_html($benefit); ?></span>
                                    </li>
                                    <?php endforeach; ?>
                                </ul>
                            </div>
                        </div>
                        <div class="p-8 md:p-12 flex items-center">
                            <div class="w-full">
                                <form class="ai-newsletter-form space-y-6">
                                    <div>
                                        <label class="block text-sm font-medium text-gray-700 mb-2"><?php echo esc_html($defaults['email_label']); ?></label>
                                        <input type="email" placeholder="<?php echo esc_attr($placeholder); ?>" required class="w-full pl-4 pr-4 py-3 rounded-lg border border-gray-300 focus:outline-none" style="border-color: var(--color-primary);" />
                                    </div>
                                    <div class="flex items-start gap-3">
                                        <input type="checkbox" id="<?php echo esc_attr($consentId); ?>" class="mt-1 w-4 h-4 border-gray-300 rounded" style="accent-color: var(--color-primary);" />
                                        <label for="<?php echo esc_attr($consentId); ?>" class="text-sm text-gray-700">
                                            <?php echo esc_html($privacyText); ?>
                                        </label>
                                    </div>
                                    <button type="submit" class="w-full text-white px-6 py-4 rounded-lg font-semibold transition-colors flex items-center justify-center gap-2 group" style="background-color: var(--color-primary);">
                                        <?php echo esc_html($buttonText); ?>
                                        <span class="text-xl group-hover:translate-x-1 transition-transform inline-block">→</span>
                                    </button>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
        <?php
    }
}
