<?php
/**
 * Name: Custom Dashboard Welcome Panel
 * Description: Create custom welcome panel with modern design to greet users with personalized content, instructions, quick links, or important announcements on dashboard homepage
 * Category: Admin
 * Icon: -
 * UseCases: [
 *   {
 *     "title": "Client Onboarding",
 *     "description": "Create custom welcome panel with video tutorials, quick links to documentation, and step-by-step guide for new website owners."
 *   },
 *   {
 *     "title": "Team Guidelines",
 *     "description": "Display editorial guidelines, brand voice reminders, and content approval workflow for multi-author publishing teams."
 *   },
 *   {
 *     "title": "Important Announcements",
 *     "description": "Show scheduled maintenance notices, new feature announcements, or critical site updates that all admin users need to see."
 *   }
 * ]
 * WhereToFind: {
 *   "location": "Dashboard (Top of Page)",
 *   "url": "",
 *   "instructions": "Configure welcome panel in module settings. Custom panel appears at the top of the dashboard with your content, colors, and styling.",
 *   "newTab": false
 * }
 * Added: 2025-07-22
 * Tags: dashboard, welcome, branding, customization, admin
 */

// Prevent direct access
if (!defined('ABSPATH')) exit;

class SwitchboardCustomDashboardWelcome {

    public function __construct() {
        // Hide WordPress default welcome panel
        remove_action('welcome_panel', 'wp_welcome_panel');
        add_filter('get_user_metadata', [$this, 'force_show_welcome_panel'], 10, 4);

        // Add our custom panel
        add_action('admin_notices', [$this, 'render_custom_welcome_panel']);
        add_action('admin_head', [$this, 'add_welcome_panel_styles']);
        
        // Register with data registry
        $this->register_with_data_registry();
    }
    
    private function register_with_data_registry() {
        if (class_exists('Switchboard_Data_Registry')) {
            $registry = Switchboard_Data_Registry::get_instance();
            $registry->auto_register($this, __FILE__);
        }
    }
    
    public function register_data_locations() {
        return [
            'options' => [
                'switchboard_welcome_panel_settings'
            ],
            'option_patterns' => [],
            'tables' => [],
            'post_meta' => [],
            'user_meta' => [],
            'term_meta' => [],
            'transients' => []
        ];
    }
    
    /**
     * Render settings panel
     * 
     * @param array $settings Current module settings
     * @return void Outputs HTML directly
     */
    public function render_settings($settings = []) {
        $defaults = [
            'title' => esc_html__('Welcome to Your Dashboard', 'wpswitchboard'),
            'content' => '',
            'title_alignment' => 'left',
            'text_alignment' => 'left',
            'title_color' => '#09090b',
            'text_color' => '#71717a',
            'title_bg_color' => '#fafafa',
            'text_bg_color' => '#ffffff',
            'width' => 100
        ];
        $settings = wp_parse_args($settings, $defaults);
        ?>
        <input type="hidden" name="module" value="custom-dashboard-welcome">

        <div class="sb-sp-group">
        <h4 class="sb-sp-heading-sm"><?php echo esc_html__('Content Settings', 'wpswitchboard'); ?></h4>
        </div>

        <div class="sb-sp-group">
        <label class="sb-sp-label" for="setting_title"><?php echo esc_html__('Welcome Title', 'wpswitchboard'); ?></label>
            <input type="text" name="setting_title" id="setting_title" value="<?php echo esc_attr($settings['title']); ?>">
            <p class="sb-sp-description"><?php echo esc_html__('Enter the welcome title that appears at the top of the panel.', 'wpswitchboard'); ?></p>
        </div>

        <div class="sb-sp-group">
        <label class="sb-sp-label" for="setting_content"><?php echo esc_html__('Welcome Content', 'wpswitchboard'); ?></label>
            <textarea name="setting_content" id="setting_content" rows="8"><?php echo esc_textarea($settings['content']); ?></textarea>
        <p class="sb-sp-description"><?php echo esc_html__('HTML is allowed. The panel uses a shadcn-style card design with a clean, modern look.', 'wpswitchboard'); ?></p>
        </div>

        <div class="sb-sp-group">
        <h4 class="sb-sp-heading-sm"><?php echo esc_html__('Layout Settings', 'wpswitchboard'); ?></h4>
        </div>

        <div class="sb-sp-group sb-sp-field-horizontal">
            <div class="sb-sp-field-horizontal-left">
                <label class="sb-sp-label" for="setting_width"><?php echo esc_html__('Panel Width', 'wpswitchboard'); ?></label>
                <p class="sb-sp-description"><?php echo esc_html__('Drag to adjust panel width (50-100%).', 'wpswitchboard'); ?></p>
            </div>
            <div class="sb-sp-field-horizontal-right">
                <input type="range" name="setting_width" id="setting_width" value="<?php echo esc_attr($settings['width']); ?>" min="50" max="100" step="5" class="sb-sp-range">
                <div class="sb-sp-range-value">
                    <span class="sb-sp-text-semibold"><span id="width_value"><?php echo esc_attr($settings['width']); ?></span>%</span>
                </div>
            </div>
        </div>

        <div class="sb-sp-group">
            <div class="sb-sp-grid-2">
                <div>
                <label class="sb-sp-label" for="setting_title_alignment"><?php echo esc_html__('Title Alignment', 'wpswitchboard'); ?></label>
                    <select name="setting_title_alignment" id="setting_title_alignment">
                    <option value="left" <?php selected($settings['title_alignment'], 'left'); ?>><?php echo esc_html__('Left', 'wpswitchboard'); ?></option>
                    <option value="center" <?php selected($settings['title_alignment'], 'center'); ?>><?php echo esc_html__('Center', 'wpswitchboard'); ?></option>
                    <option value="right" <?php selected($settings['title_alignment'], 'right'); ?>><?php echo esc_html__('Right', 'wpswitchboard'); ?></option>
                </select>
            </div>
                <div>
                <label class="sb-sp-label" for="setting_text_alignment"><?php echo esc_html__('Text Alignment', 'wpswitchboard'); ?></label>
                    <select name="setting_text_alignment" id="setting_text_alignment">
                    <option value="left" <?php selected($settings['text_alignment'], 'left'); ?>><?php echo esc_html__('Left', 'wpswitchboard'); ?></option>
                    <option value="center" <?php selected($settings['text_alignment'], 'center'); ?>><?php echo esc_html__('Center', 'wpswitchboard'); ?></option>
                    <option value="right" <?php selected($settings['text_alignment'], 'right'); ?>><?php echo esc_html__('Right', 'wpswitchboard'); ?></option>
                </select>
                </div>
            </div>
        </div>

        <div class="sb-sp-group">
        <h4 class="sb-sp-heading-sm"><?php echo esc_html__('Color Settings', 'wpswitchboard'); ?></h4>
        </div>

        <!-- Title Text Color -->
        <div class="sb-sp-group sb-sp-field-horizontal" style="align-items: center;">
            <div class="sb-sp-field-horizontal-left" style="flex: 1;">
                <label class="sb-sp-label" style="margin-bottom: 0;"><?php echo esc_html__('Title Text Color', 'wpswitchboard'); ?></label>
                <p class="sb-sp-description" style="margin: 4px 0 0 0;"><?php echo esc_html__('Default: #09090b (Zinc 950)', 'wpswitchboard'); ?></p>
            </div>
            <div class="sb-sp-field-horizontal-right" style="display: flex; align-items: center; gap: 12px;">
                <input type="color" name="setting_title_color" id="setting_title_color" value="<?php echo esc_attr($settings['title_color']); ?>" data-default="#09090b" style="width: 60px; height: 44px; padding: 4px; border: 1px solid var(--gray-200); border-radius: 8px; cursor: pointer;">
                <button type="button" class="sb-color-reset-btn" data-target="setting_title_color" data-default="#09090b" style="padding: 8px 14px; border: 1px solid var(--gray-200); border-radius: 6px; background: white; font-size: 13px; cursor: pointer; display: flex; align-items: center; gap: 6px;">↺ <?php echo esc_html__('Reset', 'wpswitchboard'); ?></button>
                <input type="text" id="setting_title_color_hex" value="<?php echo esc_attr($settings['title_color']); ?>" style="width: 90px; padding: 10px 12px; border: 1px solid var(--gray-200); border-radius: 6px; font-family: monospace; font-size: 14px;" pattern="^#[0-9A-Fa-f]{6}$">
            </div>
        </div>

        <!-- Title Background Color -->
        <div class="sb-sp-group sb-sp-field-horizontal" style="align-items: center;">
            <div class="sb-sp-field-horizontal-left" style="flex: 1;">
                <label class="sb-sp-label" style="margin-bottom: 0;"><?php echo esc_html__('Title Background Color', 'wpswitchboard'); ?></label>
                <p class="sb-sp-description" style="margin: 4px 0 0 0;"><?php echo esc_html__('Default: #fafafa (Light gray)', 'wpswitchboard'); ?></p>
            </div>
            <div class="sb-sp-field-horizontal-right" style="display: flex; align-items: center; gap: 12px;">
                <input type="color" name="setting_title_bg_color" id="setting_title_bg_color" value="<?php echo esc_attr($settings['title_bg_color']); ?>" data-default="#fafafa" style="width: 60px; height: 44px; padding: 4px; border: 1px solid var(--gray-200); border-radius: 8px; cursor: pointer;">
                <button type="button" class="sb-color-reset-btn" data-target="setting_title_bg_color" data-default="#fafafa" style="padding: 8px 14px; border: 1px solid var(--gray-200); border-radius: 6px; background: white; font-size: 13px; cursor: pointer; display: flex; align-items: center; gap: 6px;">↺ <?php echo esc_html__('Reset', 'wpswitchboard'); ?></button>
                <input type="text" id="setting_title_bg_color_hex" value="<?php echo esc_attr($settings['title_bg_color']); ?>" style="width: 90px; padding: 10px 12px; border: 1px solid var(--gray-200); border-radius: 6px; font-family: monospace; font-size: 14px;" pattern="^#[0-9A-Fa-f]{6}$">
            </div>
        </div>

        <!-- Content Text Color -->
        <div class="sb-sp-group sb-sp-field-horizontal" style="align-items: center;">
            <div class="sb-sp-field-horizontal-left" style="flex: 1;">
                <label class="sb-sp-label" style="margin-bottom: 0;"><?php echo esc_html__('Content Text Color', 'wpswitchboard'); ?></label>
                <p class="sb-sp-description" style="margin: 4px 0 0 0;"><?php echo esc_html__('Default: #71717a (Zinc 500)', 'wpswitchboard'); ?></p>
            </div>
            <div class="sb-sp-field-horizontal-right" style="display: flex; align-items: center; gap: 12px;">
                <input type="color" name="setting_text_color" id="setting_text_color" value="<?php echo esc_attr($settings['text_color']); ?>" data-default="#71717a" style="width: 60px; height: 44px; padding: 4px; border: 1px solid var(--gray-200); border-radius: 8px; cursor: pointer;">
                <button type="button" class="sb-color-reset-btn" data-target="setting_text_color" data-default="#71717a" style="padding: 8px 14px; border: 1px solid var(--gray-200); border-radius: 6px; background: white; font-size: 13px; cursor: pointer; display: flex; align-items: center; gap: 6px;">↺ <?php echo esc_html__('Reset', 'wpswitchboard'); ?></button>
                <input type="text" id="setting_text_color_hex" value="<?php echo esc_attr($settings['text_color']); ?>" style="width: 90px; padding: 10px 12px; border: 1px solid var(--gray-200); border-radius: 6px; font-family: monospace; font-size: 14px;" pattern="^#[0-9A-Fa-f]{6}$">
            </div>
        </div>

        <!-- Content Background Color -->
        <div class="sb-sp-group sb-sp-field-horizontal" style="align-items: center;">
            <div class="sb-sp-field-horizontal-left" style="flex: 1;">
                <label class="sb-sp-label" style="margin-bottom: 0;"><?php echo esc_html__('Content Background Color', 'wpswitchboard'); ?></label>
                <p class="sb-sp-description" style="margin: 4px 0 0 0;"><?php echo esc_html__('Default: #ffffff (White)', 'wpswitchboard'); ?></p>
            </div>
            <div class="sb-sp-field-horizontal-right" style="display: flex; align-items: center; gap: 12px;">
                <input type="color" name="setting_text_bg_color" id="setting_text_bg_color" value="<?php echo esc_attr($settings['text_bg_color']); ?>" data-default="#ffffff" style="width: 60px; height: 44px; padding: 4px; border: 1px solid var(--gray-200); border-radius: 8px; cursor: pointer;">
                <button type="button" class="sb-color-reset-btn" data-target="setting_text_bg_color" data-default="#ffffff" style="padding: 8px 14px; border: 1px solid var(--gray-200); border-radius: 6px; background: white; font-size: 13px; cursor: pointer; display: flex; align-items: center; gap: 6px;">↺ <?php echo esc_html__('Reset', 'wpswitchboard'); ?></button>
                <input type="text" id="setting_text_bg_color_hex" value="<?php echo esc_attr($settings['text_bg_color']); ?>" style="width: 90px; padding: 10px 12px; border: 1px solid var(--gray-200); border-radius: 6px; font-family: monospace; font-size: 14px;" pattern="^#[0-9A-Fa-f]{6}$">
            </div>
        </div>

        <div class="sb-sp-group">
            <button type="button" id="reset-welcome-panel" class="sb-module-button sb-module-button-secondary">
                <?php echo esc_html__('Reset to Defaults', 'wpswitchboard'); ?>
            </button>
            <p class="sb-sp-description" style="margin-top: 8px;"><?php echo esc_html__('Reset all settings to their default values.', 'wpswitchboard'); ?></p>
        </div>

        <script>
        jQuery(document).ready(function($) {
            // Update width value display
            $("#setting_width").on("input", function() {
                $("#width_value").text($(this).val());
            });

            // Sync color picker with hex input
            var colorFields = ['title_color', 'title_bg_color', 'text_color', 'text_bg_color'];
            colorFields.forEach(function(field) {
                var colorInput = $('#setting_' + field);
                var hexInput = $('#setting_' + field + '_hex');

                // Color picker changes hex input
                colorInput.on('input', function() {
                    hexInput.val($(this).val());
                });

                // Hex input changes color picker
                hexInput.on('input', function() {
                    var val = $(this).val();
                    if (/^#[0-9A-Fa-f]{6}$/.test(val)) {
                        colorInput.val(val);
                    }
                });
            });

            // Individual reset buttons
            $(".sb-color-reset-btn").on("click", function() {
                var target = $(this).data('target');
                var defaultVal = $(this).data('default');
                $('#' + target).val(defaultVal);
                $('#' + target + '_hex').val(defaultVal);
            });

            // Reset to defaults
            $("#reset-welcome-panel").on("click", function() {
                if (!confirm("<?php echo esc_js(__('Are you sure you want to reset all settings to defaults?', 'wpswitchboard')); ?>")) {
                    return;
                }

                // Default values
                var defaults = {
                    title: "<?php echo esc_js(__('Welcome to Your Dashboard', 'wpswitchboard')); ?>",
                    content: "",
                    title_alignment: "left",
                    text_alignment: "left",
                    title_color: "#09090b",
                    text_color: "#71717a",
                    title_bg_color: "#fafafa",
                    text_bg_color: "#ffffff",
                    width: 100
                };

                // Apply defaults to form fields
                $("#setting_title").val(defaults.title);
                $("#setting_content").val(defaults.content);
                $("#setting_title_alignment").val(defaults.title_alignment);
                $("#setting_text_alignment").val(defaults.text_alignment);
                $("#setting_title_color").val(defaults.title_color);
                $("#setting_title_color_hex").val(defaults.title_color);
                $("#setting_text_color").val(defaults.text_color);
                $("#setting_text_color_hex").val(defaults.text_color);
                $("#setting_title_bg_color").val(defaults.title_bg_color);
                $("#setting_title_bg_color_hex").val(defaults.title_bg_color);
                $("#setting_text_bg_color").val(defaults.text_bg_color);
                $("#setting_text_bg_color_hex").val(defaults.text_bg_color);
                $("#setting_width").val(defaults.width);
                $("#width_value").text(defaults.width);
            });
        });
        </script>
        <?php
    }

    public function force_show_welcome_panel($value, $object_id, $meta_key, $single) {
        if ($meta_key === 'show_welcome_panel') {
            return 0; // Hide WP default
        }
        return $value;
    }

    private function get_settings() {
        $defaults = [
            'title' => esc_html__('Welcome to Your Dashboard', 'wpswitchboard'),
            'content' => '',
            'title_alignment' => 'left',
            'text_alignment' => 'left',
            'title_color' => '#09090b',
            'text_color' => '#71717a',
            'title_bg_color' => '#fafafa',
            'text_bg_color' => '#ffffff',
            'width' => 100
        ];

        $settings = get_option('switchboard_welcome_panel_settings', []);
        return wp_parse_args($settings, $defaults);
    }

    public function add_welcome_panel_styles() {
        // Only show on dashboard
        $screen = get_current_screen();
        if (!$screen || $screen->id !== 'dashboard') {
            return;
        }

        $settings = $this->get_settings();
        ?>
        <style>
            /* Hide WordPress default welcome panel completely */
            #welcome-panel {
                display: none !important;
            }

            /* Custom Welcome Panel Container - Always Left Aligned */
            .switchboard-welcome-wrapper {
                max-width: <?php echo esc_attr($settings['width']); ?>%;
                margin: 20px 0 30px 0;
            }

            /* Shadcn-style Modern Welcome Panel */
            .switchboard-custom-welcome {
                background: #ffffff;
                border: 1px solid #e4e4e7;
                border-radius: 12px;
                padding: 0;
                margin: 0;
                box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.03), 0 1px 2px -1px rgba(0, 0, 0, 0.03);
                overflow: hidden;
            }

            /* Header Section */
            .switchboard-welcome-header {
                background: <?php echo esc_attr($settings['title_bg_color']); ?>;
                border-bottom: 1px solid #e4e4e7;
                padding: 32px 40px;
            }

            .switchboard-custom-welcome h2 {
                color: <?php echo esc_attr($settings['title_color']); ?>;
                text-align: <?php echo esc_attr($settings['title_alignment']); ?>;
                font-size: 28px;
                font-weight: 600;
                margin: 0;
                line-height: 1.3;
                letter-spacing: -0.025em;
            }

            /* Content Section */
            .switchboard-welcome-content {
                padding: 32px 40px;
                background: <?php echo esc_attr($settings['text_bg_color']); ?>;
                color: <?php echo esc_attr($settings['text_color']); ?>;
                font-size: 15px;
                line-height: 1.7;
                text-align: <?php echo esc_attr($settings['text_alignment']); ?>;
            }

            .switchboard-welcome-content p {
                color: <?php echo esc_attr($settings['text_color']); ?>;
                margin: 0 0 16px 0;
                font-size: 15px;
                line-height: 1.7;
                text-align: <?php echo esc_attr($settings['text_alignment']); ?>;
            }

            .switchboard-welcome-content p:last-child {
                margin-bottom: 0;
            }

            /* Shadcn-style links */
            .switchboard-welcome-content a {
                color: var(--primary-500);
                text-decoration: none;
                font-weight: 500;
                transition: color 0.2s ease;
                border-bottom: 1px solid transparent;
            }

            .switchboard-welcome-content a:hover {
                color: var(--primary-600);
                border-bottom-color: var(--primary-600);
            }

            /* Shadcn-style lists */
            .switchboard-welcome-content ul,
            .switchboard-welcome-content ol {
                margin: 16px 0;
                padding-left: 24px;
            }

            .switchboard-welcome-content li {
                margin-bottom: 8px;
                color: <?php echo esc_attr($settings['text_color']); ?>;
            }

            /* Shadcn-style code blocks */
            .switchboard-welcome-content code {
                background: #f4f4f5;
                border: 1px solid #e4e4e7;
                border-radius: 4px;
                padding: 2px 6px;
                font-size: 14px;
                font-family: 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', Consolas, 'Courier New', monospace;
                color: #18181b;
            }

            /* Shadcn-style buttons (if any in content) */
            .switchboard-welcome-content .button,
            .switchboard-welcome-content button {
                background: #18181b;
                color: #ffffff;
                border: 1px solid #18181b;
                border-radius: 6px;
                padding: 8px 16px;
                font-size: 14px;
                font-weight: 500;
                cursor: pointer;
                transition: all 0.2s ease;
                display: inline-flex;
                align-items: center;
                gap: 8px;
            }

            .switchboard-welcome-content .button:hover,
            .switchboard-welcome-content button:hover {
                background: #27272a;
                border-color: #27272a;
            }

            /* Responsive design */
            @media (max-width: 768px) {
                .switchboard-welcome-wrapper {
                    max-width: 100%;
                    margin: 15px;
                }

                .switchboard-welcome-header,
                .switchboard-welcome-content {
                    padding: 24px;
                }

                .switchboard-custom-welcome h2 {
                    font-size: 24px;
                }

                .switchboard-welcome-content {
                    font-size: 14px;
                }
            }
        </style>
        <?php
    }

    public function render_custom_welcome_panel() {
        // Only show on dashboard
        $screen = get_current_screen();
        if (!$screen || $screen->id !== 'dashboard') {
            return;
        }

        $settings = $this->get_settings();

        // Show default message if no content
        if (empty($settings['content'])) {
            $settings['content'] = '<p>' . esc_html__('Welcome! Configure this panel in Switchboard → Custom Dashboard Welcome Panel settings.', 'wpswitchboard') . '</p>';
        }
        ?>
        <div class="switchboard-welcome-wrapper">
            <div class="switchboard-custom-welcome">
                <div class="switchboard-welcome-header">
                    <h2><?php echo esc_html($settings['title']); ?></h2>
                </div>
                <div class="switchboard-welcome-content">
                    <?php echo wp_kses_post($settings['content']); ?>
                </div>
            </div>
        </div>
        <?php
    }
}

// Initialize and register with data system
$switchboard_instance = new SwitchboardCustomDashboardWelcome();
Switchboard_Data_Registry::get_instance()->auto_register($switchboard_instance, __FILE__);
