<?php
/**
 * Biscotti Divi Builder Modules
 *
 * Registers visual Divi Builder modules for all Biscotti shortcodes.
 * Each module extends ET_Builder_Module and renders via do_shortcode().
 *
 * @package Biscotti_CMP
 * @since 3.1.0
 */

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

// Guard: only load if Divi Builder is active
if (!class_exists('ET_Builder_Module')) {
    return;
}

class Biscotti_Divi_Modules
{
    private static $instance = null;

    public static function get_instance()
    {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function __construct()
    {
        add_action('et_builder_ready', [$this, 'register_modules']);
    }

    public function register_modules()
    {
        // Each module is a self-contained class below
        new Biscotti_Divi_Cookie_Table();
        new Biscotti_Divi_Consent_Link();
        new Biscotti_Divi_Settings_Link();
        new Biscotti_Divi_Data_Export();
        new Biscotti_Divi_Do_Not_Sell();
        new Biscotti_Divi_Impressum();
        new Biscotti_Divi_Privacy_Policy();
        new Biscotti_Divi_Cookie_Policy();
    }
}

// =============================================================================
// BASE MODULE
// =============================================================================
class Biscotti_Divi_Base extends ET_Builder_Module
{
    public $vb_support = 'on';
    protected $shortcode_tag = '';

    public function init()
    {
        $this->main_css_element = '%%order_class%%';
    }

    public function get_fields()
    {
        return [];
    }

    public function render($attrs, $content, $render_slug)
    {
        return do_shortcode('[' . esc_attr($this->shortcode_tag) . $this->build_atts($attrs) . ']');
    }

    protected function build_atts($attrs)
    {
        return '';
    }

    protected function get_lang_options()
    {
        return Biscotti_Languages::select_options( true, esc_html__( 'Auto (from WordPress)', 'biscotti-cmp' ) );
    }
}

// =============================================================================
// COOKIE TABLE
// =============================================================================
class Biscotti_Divi_Cookie_Table extends Biscotti_Divi_Base
{
    public $slug = 'biscotti_divi_cookie_table';
    public $name = 'Biscotti Cookie Table';
    protected $shortcode_tag = 'biscotti_cookies';

    public function init()
    {
        parent::init();
        $this->name = esc_html__('Biscotti Cookie Table', 'biscotti-cmp');
        $this->icon = 'A'; // Divi uses letters as fallback icons
    }

    public function get_fields()
    {
        return [
            'cookie_category' => [
                'label' => esc_html__('Category', 'biscotti-cmp'),
                'type' => 'select',
                'options' => [
                    'all' => esc_html__('All', 'biscotti-cmp'),
                    'essential' => esc_html__('Essential', 'biscotti-cmp'),
                    'functional' => esc_html__('Functional', 'biscotti-cmp'),
                    'analytics' => esc_html__('Analytics', 'biscotti-cmp'),
                    'marketing' => esc_html__('Marketing', 'biscotti-cmp'),
                ],
                'default' => 'all',
                'toggle_slug' => 'main_content',
            ],
            'show_duration' => [
                'label' => esc_html__('Show Duration', 'biscotti-cmp'),
                'type' => 'yes_no_button',
                'options' => ['on' => esc_html__('Yes', 'biscotti-cmp'), 'off' => esc_html__('No', 'biscotti-cmp')],
                'default' => 'on',
                'toggle_slug' => 'main_content',
            ],
            'show_provider' => [
                'label' => esc_html__('Show Provider', 'biscotti-cmp'),
                'type' => 'yes_no_button',
                'options' => ['on' => esc_html__('Yes', 'biscotti-cmp'), 'off' => esc_html__('No', 'biscotti-cmp')],
                'default' => 'on',
                'toggle_slug' => 'main_content',
            ],
        ];
    }

    protected function build_atts($attrs)
    {
        $atts = ' category="' . esc_attr($attrs['cookie_category'] ?? 'all') . '"';
        $atts .= ' show_duration="' . (($attrs['show_duration'] ?? 'on') === 'on' ? 'true' : 'false') . '"';
        $atts .= ' show_provider="' . (($attrs['show_provider'] ?? 'on') === 'on' ? 'true' : 'false') . '"';
        return $atts;
    }
}

// =============================================================================
// CONSENT LINK
// =============================================================================
class Biscotti_Divi_Consent_Link extends Biscotti_Divi_Base
{
    public $slug = 'biscotti_divi_consent_link';
    public $name = 'Biscotti Consent Link';
    protected $shortcode_tag = 'biscotti_consent_link';

    public function init()
    {
        parent::init();
        $this->name = esc_html__('Biscotti Consent Link', 'biscotti-cmp');
    }

    public function get_fields()
    {
        return [
            'link_text' => [
                'label' => esc_html__('Link Text', 'biscotti-cmp'),
                'type' => 'text',
                'default' => esc_html__('Change cookie settings', 'biscotti-cmp'),
                'toggle_slug' => 'main_content',
            ],
        ];
    }

    protected function build_atts($attrs)
    {
        return !empty($attrs['link_text']) ? ' text="' . esc_attr($attrs['link_text']) . '"' : '';
    }
}

// =============================================================================
// SETTINGS LINK
// =============================================================================
class Biscotti_Divi_Settings_Link extends Biscotti_Divi_Base
{
    public $slug = 'biscotti_divi_settings_link';
    public $name = 'Biscotti Settings Link';
    protected $shortcode_tag = 'biscotti_settings_link';

    public function init()
    {
        parent::init();
        $this->name = esc_html__('Biscotti Settings Link', 'biscotti-cmp');
    }

    public function get_fields()
    {
        return [
            'link_text' => [
                'label' => esc_html__('Link Text', 'biscotti-cmp'),
                'type' => 'text',
                'default' => esc_html__('Cookie Settings', 'biscotti-cmp'),
                'toggle_slug' => 'main_content',
            ],
        ];
    }

    protected function build_atts($attrs)
    {
        return !empty($attrs['link_text']) ? ' text="' . esc_attr($attrs['link_text']) . '"' : '';
    }
}

// =============================================================================
// DATA EXPORT
// =============================================================================
class Biscotti_Divi_Data_Export extends Biscotti_Divi_Base
{
    public $slug = 'biscotti_divi_data_export';
    public $name = 'Biscotti Data Export';
    protected $shortcode_tag = 'biscotti_data_export';

    public function init()
    {
        parent::init();
        $this->name = esc_html__('Biscotti Data Export', 'biscotti-cmp');
    }
}

// =============================================================================
// DO NOT SELL (CCPA)
// =============================================================================
class Biscotti_Divi_Do_Not_Sell extends Biscotti_Divi_Base
{
    public $slug = 'biscotti_divi_do_not_sell';
    public $name = 'Biscotti Do Not Sell';
    protected $shortcode_tag = 'biscotti_do_not_sell';

    public function init()
    {
        parent::init();
        $this->name = esc_html__('Biscotti Do Not Sell (CCPA)', 'biscotti-cmp');
    }

    public function get_fields()
    {
        return [
            'link_text' => [
                'label' => esc_html__('Link Text', 'biscotti-cmp'),
                'type' => 'text',
                'default' => esc_html__('Do Not Sell My Data', 'biscotti-cmp'),
                'toggle_slug' => 'main_content',
            ],
        ];
    }

    protected function build_atts($attrs)
    {
        return !empty($attrs['link_text']) ? ' text="' . esc_attr($attrs['link_text']) . '"' : '';
    }
}

// =============================================================================
// LEGAL DOCUMENT MODULES (Impressum, Privacy, Cookie Policy)
// =============================================================================
class Biscotti_Divi_Legal_Base extends Biscotti_Divi_Base
{
    public function get_fields()
    {
        return [
            'enable_cache' => [
                'label' => esc_html__('Enable Cache', 'biscotti-cmp'),
                'type' => 'yes_no_button',
                'options' => ['on' => esc_html__('Yes', 'biscotti-cmp'), 'off' => esc_html__('No', 'biscotti-cmp')],
                'default' => 'on',
                'toggle_slug' => 'main_content',
            ],
            'doc_lang' => [
                'label' => esc_html__('Language', 'biscotti-cmp'),
                'type' => 'select',
                'options' => $this->get_lang_options(),
                'default' => 'auto',
                'toggle_slug' => 'main_content',
            ],
        ];
    }

    protected function build_atts($attrs)
    {
        $atts = ' cache="' . (($attrs['enable_cache'] ?? 'on') === 'on' ? 'true' : 'false') . '"';
        $lang = $attrs['doc_lang'] ?? 'auto';
        if ($lang !== 'auto') {
            $atts .= ' lang="' . esc_attr($lang) . '"';
        }
        return $atts;
    }
}

class Biscotti_Divi_Impressum extends Biscotti_Divi_Legal_Base
{
    public $slug = 'biscotti_divi_impressum';
    public $name = 'Biscotti Impressum';
    protected $shortcode_tag = 'biscotti_impressum';

    public function init()
    {
        parent::init();
        $this->name = esc_html__('Biscotti Impressum', 'biscotti-cmp');
    }
}

class Biscotti_Divi_Privacy_Policy extends Biscotti_Divi_Legal_Base
{
    public $slug = 'biscotti_divi_privacy_policy';
    public $name = 'Biscotti Privacy Policy';
    protected $shortcode_tag = 'biscotti_privacy_policy';

    public function init()
    {
        parent::init();
        $this->name = esc_html__('Biscotti Privacy Policy', 'biscotti-cmp');
    }
}

class Biscotti_Divi_Cookie_Policy extends Biscotti_Divi_Legal_Base
{
    public $slug = 'biscotti_divi_cookie_policy';
    public $name = 'Biscotti Cookie Policy';
    protected $shortcode_tag = 'biscotti_cookie_policy';

    public function init()
    {
        parent::init();
        $this->name = esc_html__('Biscotti Cookie Policy', 'biscotti-cmp');
    }
}

// Initialize
Biscotti_Divi_Modules::get_instance();
