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

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

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

class Biscotti_Beaver_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('init', [$this, 'register_modules']);
    }

    public function register_modules()
    {
        if (!class_exists('FLBuilder')) {
            return;
        }

        $this->register_cookie_table();
        $this->register_consent_link();
        $this->register_settings_link();
        $this->register_data_export();
        $this->register_do_not_sell();
        $this->register_impressum();
        $this->register_privacy_policy();
        $this->register_cookie_policy();
    }

    private function get_lang_field()
    {
        return [
            'type' => 'select',
            'label' => esc_html__('Language', 'biscotti-cmp'),
            'default' => 'auto',
            'options' => Biscotti_Languages::select_options( true, esc_html__( 'Auto (from WordPress)', 'biscotti-cmp' ) ),
        ];
    }

    private function register_cookie_table()
    {
        FLBuilder::register_module('Biscotti_BB_Cookie_Table', [
            'general' => [
                'title' => esc_html__('Settings', 'biscotti-cmp'),
                'sections' => [
                    'content' => [
                        'title' => '',
                        'fields' => [
                            'category' => [
                                'type' => 'select',
                                'label' => esc_html__('Category', 'biscotti-cmp'),
                                'default' => 'all',
                                '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'),
                                ],
                            ],
                            'show_duration' => [
                                'type' => 'select',
                                'label' => esc_html__('Show Duration', 'biscotti-cmp'),
                                'default' => 'true',
                                'options' => ['true' => esc_html__('Yes', 'biscotti-cmp'), 'false' => esc_html__('No', 'biscotti-cmp')],
                            ],
                            'show_provider' => [
                                'type' => 'select',
                                'label' => esc_html__('Show Provider', 'biscotti-cmp'),
                                'default' => 'true',
                                'options' => ['true' => esc_html__('Yes', 'biscotti-cmp'), 'false' => esc_html__('No', 'biscotti-cmp')],
                            ],
                        ],
                    ],
                ],
            ],
        ]);
    }

    private function register_consent_link()
    {
        FLBuilder::register_module('Biscotti_BB_Consent_Link', [
            'general' => [
                'title' => esc_html__('Settings', 'biscotti-cmp'),
                'sections' => [
                    'content' => [
                        'title' => '',
                        'fields' => [
                            'link_text' => [
                                'type' => 'text',
                                'label' => esc_html__('Link Text', 'biscotti-cmp'),
                                'default' => esc_html__('Change cookie settings', 'biscotti-cmp'),
                            ],
                        ],
                    ],
                ],
            ],
        ]);
    }

    private function register_settings_link()
    {
        FLBuilder::register_module('Biscotti_BB_Settings_Link', [
            'general' => [
                'title' => esc_html__('Settings', 'biscotti-cmp'),
                'sections' => [
                    'content' => [
                        'title' => '',
                        'fields' => [
                            'link_text' => [
                                'type' => 'text',
                                'label' => esc_html__('Link Text', 'biscotti-cmp'),
                                'default' => esc_html__('Cookie Settings', 'biscotti-cmp'),
                            ],
                        ],
                    ],
                ],
            ],
        ]);
    }

    private function register_data_export()
    {
        FLBuilder::register_module('Biscotti_BB_Data_Export', [
            'general' => [
                'title' => esc_html__('Info', 'biscotti-cmp'),
                'sections' => [
                    'content' => [
                        'title' => '',
                        'fields' => [],
                    ],
                ],
            ],
        ]);
    }

    private function register_do_not_sell()
    {
        FLBuilder::register_module('Biscotti_BB_Do_Not_Sell', [
            'general' => [
                'title' => esc_html__('Settings', 'biscotti-cmp'),
                'sections' => [
                    'content' => [
                        'title' => '',
                        'fields' => [
                            'link_text' => [
                                'type' => 'text',
                                'label' => esc_html__('Link Text', 'biscotti-cmp'),
                                'default' => esc_html__('Do Not Sell My Data', 'biscotti-cmp'),
                            ],
                        ],
                    ],
                ],
            ],
        ]);
    }

    private function register_legal_module($class_name, $fields_extra = [])
    {
        $fields = [
            'enable_cache' => [
                'type' => 'select',
                'label' => esc_html__('Enable Cache', 'biscotti-cmp'),
                'default' => 'true',
                'options' => ['true' => esc_html__('Yes', 'biscotti-cmp'), 'false' => esc_html__('No', 'biscotti-cmp')],
            ],
            'doc_lang' => $this->get_lang_field(),
        ];

        FLBuilder::register_module($class_name, [
            'general' => [
                'title' => esc_html__('Settings', 'biscotti-cmp'),
                'sections' => [
                    'content' => [
                        'title' => '',
                        'fields' => array_merge($fields, $fields_extra),
                    ],
                ],
            ],
        ]);
    }

    private function register_impressum()
    {
        $this->register_legal_module('Biscotti_BB_Impressum');
    }

    private function register_privacy_policy()
    {
        $this->register_legal_module('Biscotti_BB_Privacy_Policy');
    }

    private function register_cookie_policy()
    {
        $this->register_legal_module('Biscotti_BB_Cookie_Policy');
    }
}

// =============================================================================
// MODULE CLASSES
// =============================================================================

class Biscotti_BB_Cookie_Table extends FLBuilderModule
{
    public function __construct()
    {
        parent::__construct([
            'name' => esc_html__('Biscotti Cookie Table', 'biscotti-cmp'),
            'description' => esc_html__('Cookie table for privacy policy', 'biscotti-cmp'),
            'group' => 'Biscotti CMP',
            'category' => 'Biscotti CMP',
            'icon' => 'editor-table.svg',
        ]);
    }

    // Beaver Builder auto-calls this if exists
    public function render()
    {
        $s = $this->settings;
        echo do_shortcode('[biscotti_cookies category="' . esc_attr($s->category ?? 'all') . '" show_duration="' . esc_attr($s->show_duration ?? 'true') . '" show_provider="' . esc_attr($s->show_provider ?? 'true') . '"]');
    }
}

class Biscotti_BB_Consent_Link extends FLBuilderModule
{
    public function __construct()
    {
        parent::__construct([
            'name' => esc_html__('Biscotti Consent Link', 'biscotti-cmp'),
            'description' => esc_html__('Link to reopen consent banner', 'biscotti-cmp'),
            'group' => 'Biscotti CMP',
            'category' => 'Biscotti CMP',
            'icon' => 'admin-links.svg',
        ]);
    }

    public function render()
    {
        $text = $this->settings->link_text ?? '';
        $atts = !empty($text) ? ' text="' . esc_attr($text) . '"' : '';
        echo do_shortcode('[biscotti_consent_link' . $atts . ']');
    }
}

class Biscotti_BB_Settings_Link extends FLBuilderModule
{
    public function __construct()
    {
        parent::__construct([
            'name' => esc_html__('Biscotti Settings Link', 'biscotti-cmp'),
            'description' => esc_html__('Link to open cookie settings panel', 'biscotti-cmp'),
            'group' => 'Biscotti CMP',
            'category' => 'Biscotti CMP',
            'icon' => 'admin-generic.svg',
        ]);
    }

    public function render()
    {
        $text = $this->settings->link_text ?? '';
        $atts = !empty($text) ? ' text="' . esc_attr($text) . '"' : '';
        echo do_shortcode('[biscotti_settings_link' . $atts . ']');
    }
}

class Biscotti_BB_Data_Export extends FLBuilderModule
{
    public function __construct()
    {
        parent::__construct([
            'name' => esc_html__('Biscotti Data Export', 'biscotti-cmp'),
            'description' => esc_html__('GDPR Art. 15 data export button', 'biscotti-cmp'),
            'group' => 'Biscotti CMP',
            'category' => 'Biscotti CMP',
            'icon' => 'download.svg',
        ]);
    }

    public function render()
    {
        echo do_shortcode('[biscotti_data_export]');
    }
}

class Biscotti_BB_Do_Not_Sell extends FLBuilderModule
{
    public function __construct()
    {
        parent::__construct([
            'name' => esc_html__('Biscotti Do Not Sell (CCPA)', 'biscotti-cmp'),
            'description' => esc_html__('CCPA "Do Not Sell My Data" link', 'biscotti-cmp'),
            'group' => 'Biscotti CMP',
            'category' => 'Biscotti CMP',
            'icon' => 'shield.svg',
        ]);
    }

    public function render()
    {
        $text = $this->settings->link_text ?? '';
        $atts = !empty($text) ? ' text="' . esc_attr($text) . '"' : '';
        echo do_shortcode('[biscotti_do_not_sell' . $atts . ']');
    }
}

// Legal document modules
class Biscotti_BB_Legal_Base extends FLBuilderModule
{
    protected $shortcode_name = '';

    public function render()
    {
        $s = $this->settings;
        $atts = ' cache="' . esc_attr($s->enable_cache ?? 'true') . '"';
        $lang = $s->doc_lang ?? 'auto';
        if ($lang !== 'auto') {
            $atts .= ' lang="' . esc_attr($lang) . '"';
        }
        echo do_shortcode('[' . $this->shortcode_name . $atts . ']');
    }
}

class Biscotti_BB_Impressum extends Biscotti_BB_Legal_Base
{
    protected $shortcode_name = 'biscotti_impressum';

    public function __construct()
    {
        parent::__construct([
            'name' => esc_html__('Biscotti Impressum', 'biscotti-cmp'),
            'description' => esc_html__('Legal notice from Biscotti Legal Suite', 'biscotti-cmp'),
            'group' => 'Biscotti CMP',
            'category' => 'Biscotti CMP',
            'icon' => 'media-document.svg',
        ]);
    }
}

class Biscotti_BB_Privacy_Policy extends Biscotti_BB_Legal_Base
{
    protected $shortcode_name = 'biscotti_privacy_policy';

    public function __construct()
    {
        parent::__construct([
            'name' => esc_html__('Biscotti Privacy Policy', 'biscotti-cmp'),
            'description' => esc_html__('Privacy policy from Biscotti Legal Suite', 'biscotti-cmp'),
            'group' => 'Biscotti CMP',
            'category' => 'Biscotti CMP',
            'icon' => 'lock.svg',
        ]);
    }
}

class Biscotti_BB_Cookie_Policy extends Biscotti_BB_Legal_Base
{
    protected $shortcode_name = 'biscotti_cookie_policy';

    public function __construct()
    {
        parent::__construct([
            'name' => esc_html__('Biscotti Cookie Policy', 'biscotti-cmp'),
            'description' => esc_html__('Cookie policy from Biscotti Legal Suite', 'biscotti-cmp'),
            'group' => 'Biscotti CMP',
            'category' => 'Biscotti CMP',
            'icon' => 'info-circle.svg',
        ]);
    }
}

// Initialize
Biscotti_Beaver_Modules::get_instance();
