<?php
defined('ABSPATH') || exit;

class Termin_Kalender_Magic_Pages
{
    public function __construct()
    {
        add_filter('default_content', [$this, 'magic_page_content'], 10, 2);
        add_action('admin_enqueue_scripts', [$this, 'enqueue_onboarding_scripts'], 20);
    }

    public function enqueue_onboarding_scripts($hook)
    {
        if ($hook === 'toplevel_page_termin-kalender') {
            $options = get_option('termin_kalender_options', []);
            if (empty($options['tk_welcome_screen_seen'])) {
                wp_localize_script('termin_kalender_common_admin', 'tkAdminSettings', ['show_welcome_screen' => true]);
                // Set the flag to true so it doesn't show again
                $options['tk_welcome_screen_seen'] = true;
                update_option('termin_kalender_options', $options);
            }
        }
    }

    public function magic_page_content($content, $post)
    {
        // Verify nonce for magic page content generation to prevent unauthorized pre-filling
        if (!isset($_GET['tk_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['tk_nonce'])), 'tk_magic_page')) {
            return $content;
        }

        if (isset($_GET['tk_magic_page'])) {
            $page_type = sanitize_text_field(wp_unslash($_GET['tk_magic_page']));
            
            $calendar_block_content = '<!-- wp:termin-kalender/kalender-block /-->';
            if ($page_type === 'calendar') {
                $attrs = [];
                
                // ter_kal_CHANGE: Retrieve hidden categories from GET param array
                // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only reading parameter to prefill new post content, no state change
                if (isset($_GET['tk_hide_cats']) && is_array($_GET['tk_hide_cats'])) {
                    $hidden_cats = array_map('sanitize_text_field', wp_unslash($_GET['tk_hide_cats']));
                    if (!empty($hidden_cats)) {
                        $attrs['hidden_categories'] = $hidden_cats;
                        $attrs['hide_category_buttons'] = true; // Automatically hide buttons when explicitly filtering via magic page
                    }
                }
                
                if (!empty($attrs)) {
                    // WICHTIG: Native json_encode as per instructions, avoiding escaped unicode
                    $calendar_block_content = '<!-- wp:termin-kalender/kalender-block ' . json_encode($attrs, JSON_UNESCAPED_UNICODE) . ' /-->';
                }
            }
            
            $list_block_content = '<!-- wp:termin-kalender/simplelist /-->';
            if ($page_type === 'list') {
                $attrs = [];
                // ter_kal_CHANGE: Retrieve time display setting for simple list
                // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only reading parameter to prefill new post content, no state change
                if (isset($_GET['tk_show_time'])) {
                    $show_time = sanitize_text_field(wp_unslash($_GET['tk_show_time']));
                    if (in_array($show_time, ['start_end', 'start', 'hide'], true)) {
                        $attrs['show_time'] = $show_time;
                    }
                }
                
                if (!empty($attrs)) {
                    $list_block_content = '<!-- wp:termin-kalender/simplelist ' . json_encode($attrs, JSON_UNESCAPED_UNICODE) . ' /-->';
                }
            }

            $mappings = apply_filters('termin_kalender_magic_page_mapping', [
                'calendar' => $calendar_block_content,
                'list'     => $list_block_content
            ]);

            if (isset($mappings[$page_type])) {
                return $mappings[$page_type];
            }
        }
        return $content;
    }
}
