<?php

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

abstract class AIEO_DMM_Module_Base {

    
    abstract public function get_id();

    
    abstract public function get_label();

    
    abstract public function render_subtab();

    
    public function register_hooks() {
        
    }

    
    public function enqueue_admin_assets() {
        
    }

    
    public function get_priority() {
        return 100;
    }

    
    public function get_group() {
        return null;
    }

    
    public function get_section() {
        return null;
    }

    
    public function is_available() {
        return true;
    }

    
    public function render_upsell_stub() {
        echo '<div class="notice notice-info"><p>';
        echo esc_html(sprintf(
            
            __('“%s” is available in the Pro version of AI eShop Optimizer.', 'ai-eshop-optimizer'),
            $this->get_label()
        ));
        echo '</p></div>';
    }

    
    public function is_pro_locked() {
        return false;
    }

    
    public function get_pro_promo() {
        return array();
    }

    
    public static function is_aieo_pro_user() {
        $eshop_optimizer_pro = (
            get_option('aieo_activate_account') == '1'
            && get_option('aieo_user_membership') === 'Pro'
        );
        $rooster_connected = (
            get_option('aieo_rooster_enabled')
            && get_option('aieo_rooster_status') === 'connected'
        );
        return $eshop_optimizer_pro || $rooster_connected;
    }

    
    public function render() {
        if (!$this->is_available()) {
            $this->render_upsell_stub();
            return;
        }
        if ($this->is_pro_locked() && !self::is_aieo_pro_user()) {
            $this->render_pro_locked_screen();
            return;
        }
        $this->render_subtab();
    }

    
    protected function render_pro_locked_screen() {
        $promo       = (array) $this->get_pro_promo();
        $title       = !empty($promo['title']) ? (string) $promo['title'] : $this->get_label();
        $description = !empty($promo['description']) ? (string) $promo['description'] : '';
        $bullets     = !empty($promo['bullets']) && is_array($promo['bullets']) ? $promo['bullets'] : array();
        $cta_url     = 'https://rooster.systems/get-aieo/';

        self::print_pro_overlay_styles();

        echo '<div class="aieo-dmm-pro-locked-wrapper aieo-pro-locked">';

        
        
        echo '<div class="aieo-dmm-pro-placeholder" aria-hidden="true">';
        echo '<h2>' . esc_html($this->get_label()) . '</h2>';
        for ($i = 0; $i < 4; $i++) {
            echo '<div class="aieo-dmm-pro-placeholder-card"><div class="aieo-dmm-pro-placeholder-line"></div><div class="aieo-dmm-pro-placeholder-line short"></div><div class="aieo-dmm-pro-placeholder-line"></div></div>';
        }
        echo '</div>';

        echo '<div class="aieo-pro-overlay">';
        echo   '<div class="aieo-pro-overlay-content">';
        echo     '<div class="aieo-pro-badge">';
        echo       '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">';
        echo         '<path d="M5 16L3 5L8.5 10L12 4L15.5 10L21 5L19 16H5Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>';
        echo       '</svg>';
        echo       '<span>' . esc_html__('PRO', 'ai-eshop-optimizer') . '</span>';
        echo     '</div>';
        echo     '<h3>' . esc_html($title) . '</h3>';
        if ($description !== '') {
            echo '<p>' . esc_html($description) . '</p>';
        }
        if (!empty($bullets)) {
            echo '<ul class="aieo-pro-overlay-bullets">';
            foreach ($bullets as $b) {
                echo '<li>' . esc_html((string) $b) . '</li>';
            }
            echo '</ul>';
        }
        echo     '<a href="' . esc_url($cta_url) . '" target="_blank" rel="noopener noreferrer" class="aieo-pro-overlay-button">';
        echo       esc_html__('Upgrade to Pro', 'ai-eshop-optimizer') . ' →';
        echo     '</a>';
        echo   '</div>';
        echo '</div>';

        echo '</div>';
    }

    
    protected static function print_pro_overlay_styles() {
        static $printed = false;
        if ($printed) { return; }
        $printed = true;
        ?>
        <style id="aieo-dmm-pro-overlay-css">
            .aieo-dmm-pro-locked-wrapper { position: relative; min-height: 480px; border-radius: 12px; overflow: hidden; }
            .aieo-dmm-pro-placeholder { padding: 24px 28px; }
            .aieo-dmm-pro-placeholder h2 { margin: 0 0 18px; font-size: 22px; font-weight: 600; color: #1d1d1f; }
            .aieo-dmm-pro-placeholder-card { background: #fff; border: 1px solid #e6e6e8; border-radius: 10px; padding: 18px 20px; margin-bottom: 14px; }
            .aieo-dmm-pro-placeholder-line { height: 12px; background: linear-gradient(90deg, #ececef 0%, #f4f4f6 50%, #ececef 100%); border-radius: 6px; margin-bottom: 10px; }
            .aieo-dmm-pro-placeholder-line.short { width: 55%; }
            .aieo-dmm-pro-placeholder-line:last-child { margin-bottom: 0; }
            .aieo-pro-locked .aieo-dmm-pro-placeholder { filter: blur(2px); opacity: 0.7; pointer-events: none; user-select: none; }
            .aieo-pro-overlay { position: absolute; inset: 0; background: linear-gradient(135deg, rgba(102, 126, 234, 0.05) 0%, rgba(118, 75, 162, 0.75) 100%); -webkit-backdrop-filter: blur(1px); backdrop-filter: blur(1px); border-radius: 12px; z-index: 10; display: flex; align-items: center; justify-content: center; padding: 40px; animation: aieoProFade 0.3s ease; }
            @keyframes aieoProFade { from { opacity: 0; } to { opacity: 1; } }
            .aieo-pro-overlay-content { text-align: center; color: #fff; max-width: 520px; }
            .aieo-pro-badge { display: inline-flex; align-items: center; gap: 6px; background: rgba(255, 255, 255, 0.2); padding: 8px 16px; border-radius: 20px; font-weight: 700; font-size: 13px; margin-bottom: 16px; border: 2px solid rgba(255, 255, 255, 0.3); letter-spacing: 0.04em; }
            .aieo-pro-badge svg { width: 16px; height: 16px; color: #ffd97d; }
            .aieo-pro-overlay-content h3 { margin: 0 0 14px; font-size: 26px; font-weight: 700; color: #fff; text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); }
            .aieo-pro-overlay-content p { margin: 0 0 18px; font-size: 15px; line-height: 1.6; opacity: 0.95; }
            .aieo-pro-overlay-bullets { text-align: left; margin: 0 auto 22px; padding: 0; list-style: none; display: inline-block; }
            .aieo-pro-overlay-bullets li { padding: 4px 0 4px 22px; position: relative; font-size: 14px; line-height: 1.5; opacity: 0.95; }
            .aieo-pro-overlay-bullets li::before { content: "✓"; position: absolute; left: 0; top: 4px; color: #ffd97d; font-weight: 700; }
            .aieo-pro-overlay-button { display: inline-block; background: #fff; color: #667eea; padding: 14px 32px; border-radius: 8px; text-decoration: none; font-weight: 700; font-size: 16px; box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2); transition: transform 0.2s ease, box-shadow 0.2s ease, color 0.2s ease; }
            .aieo-pro-overlay-button:hover, .aieo-pro-overlay-button:focus { transform: translateY(-2px); box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3); color: #5568d3; text-decoration: none; }
            @media (max-width: 768px) {
                .aieo-pro-overlay { padding: 24px; }
                .aieo-pro-overlay-content h3 { font-size: 20px; }
                .aieo-pro-overlay-content p { font-size: 14px; }
                .aieo-pro-overlay-button { padding: 12px 24px; font-size: 14px; }
            }
        </style>
        <?php
    }

    
    public function get_url() {
        return add_query_arg(
            array('page' => AIEO_DMM::MENU_SLUG, 'dmm_tab' => $this->get_id()),
            admin_url('admin.php')
        );
    }

    
    public function get_form_action_url($action) {
        return add_query_arg(
            array('action' => $action),
            admin_url('admin-post.php')
        );
    }

    
    protected function render_template($file_basename, array $vars = array()) {
        $path = AIEO_DIR . 'assets/pages/dmm/' . $file_basename;
        if (!file_exists($path)) {
            printf(
                '<div class="notice notice-error"><p>%s</p></div>',
                esc_html(sprintf(
                    
                    __('Missing template: %s', 'ai-eshop-optimizer'),
                    $file_basename
                ))
            );
            return;
        }
        
        $module = $this;
        if (!empty($vars)) {
            extract($vars, EXTR_SKIP); 
        }
        include $path;
    }
}
