<?php
/**
 * ==========================
 * 🔐 IGNY8 FILE RULE HEADER
 * ==========================
 * @file         : init.php
 * @location     : /core/admin/init.php
 * @type         : Function Library
 * @scope        : Global
 * @allowed      : Admin initialization, settings registration, asset enqueuing
 * @reusability  : Globally Reusable
 * @notes        : WordPress admin initialization and settings management
 */

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

/**
 * ------------------------------------------------------------------------
 * ADMIN INITIALIZATION BOOTSTRAP
 * ------------------------------------------------------------------------
 */
add_action('admin_init', 'igny8_register_settings');

/**
 * ------------------------------------------------------------------------
 * SETTINGS REGISTRATION
 * ------------------------------------------------------------------------
 */
function igny8_register_settings() {
    $groups = igny8_get_settings_config();

    foreach ($groups as $group => $settings) {
        foreach ($settings as $name => $config) {
            register_setting($group, $name, $config);
        }
    }
}

/**
 * Settings Configuration (grouped)
 */
function igny8_get_settings_config() {
    return [
        'igny8_table_settings' => [
            'igny8_records_per_page' => [
                'type' => 'integer',
                'default' => 20,
                'sanitize_callback' => 'absint'
            ]
        ],
        'igny8_ai_integration_settings' => [
            'igny8_ai_cluster_building' => ['type' => 'string', 'default' => 'enabled', 'sanitize_callback' => 'sanitize_text_field'],
            'igny8_ai_content_ideas' => ['type' => 'string', 'default' => 'enabled', 'sanitize_callback' => 'sanitize_text_field'],
            'igny8_ai_auto_mapping' => ['type' => 'string', 'default' => 'enabled', 'sanitize_callback' => 'sanitize_text_field']
        ],
        'igny8_api_settings' => [
            'igny8_api_key' => ['type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field'],
            'igny8_runware_api_key' => ['type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field'],
            'igny8_model' => ['type' => 'string', 'default' => 'gpt-4.1', 'sanitize_callback' => 'sanitize_text_field'],
            'igny8_image_service' => ['type' => 'string', 'default' => 'openai', 'sanitize_callback' => 'sanitize_text_field'],
            'igny8_image_model' => ['type' => 'string', 'default' => 'dall-e-3', 'sanitize_callback' => 'sanitize_text_field'],
            'igny8_runware_model' => ['type' => 'string', 'default' => 'runware:97@1', 'sanitize_callback' => 'sanitize_text_field']
        ],
        'igny8_personalize_settings_group' => [
            'igny8_content_engine_global_status' => ['sanitize_callback' => 'igny8_sanitize_checkbox_setting'],
            'igny8_content_engine_enabled_post_types' => ['sanitize_callback' => 'igny8_sanitize_array_setting'],
            'igny8_content_engine_insertion_position' => ['sanitize_callback' => 'sanitize_text_field'],
            'igny8_content_engine_display_mode' => ['sanitize_callback' => 'sanitize_text_field'],
            'igny8_content_engine_teaser_text' => ['sanitize_callback' => 'sanitize_textarea_field'],
            'igny8_content_engine_save_variations' => ['sanitize_callback' => 'intval'],
            'igny8_content_engine_field_mode' => ['sanitize_callback' => 'sanitize_text_field'],
            'igny8_content_engine_detection_prompt' => ['sanitize_callback' => 'sanitize_textarea_field'],
            'igny8_content_engine_context_source' => ['sanitize_callback' => 'sanitize_textarea_field'],
            'igny8_content_engine_include_page_context' => ['sanitize_callback' => 'intval'],
            'igny8_content_engine_content_length' => ['sanitize_callback' => 'sanitize_text_field'],
            'igny8_content_engine_rewrite_prompt' => ['sanitize_callback' => 'sanitize_textarea_field'],
            'igny8_content_engine_fixed_fields_config' => ['sanitize_callback' => 'igny8_sanitize_fields_config']
        ]
    ];
}

/**
 * ------------------------------------------------------------------------
 * SANITIZATION HELPERS
 * ------------------------------------------------------------------------
 */
function igny8_sanitize_checkbox_setting($raw) {
    return isset($_POST['igny8_content_engine_global_status']) ? 'enabled' : 'disabled';
}

function igny8_sanitize_array_setting($raw) {
    return is_array($raw) ? array_map('sanitize_text_field', $raw) : [];
}

function igny8_sanitize_fields_config($raw) {
    if (!is_array($raw)) return [];
    $sanitized = [];
    foreach ($raw as $index => $field) {
        $sanitized[$index] = [
            'label' => sanitize_text_field($field['label'] ?? ''),
            'type' => sanitize_text_field($field['type'] ?? 'text'),
            'options' => sanitize_text_field($field['options'] ?? '')
        ];
    }
    return $sanitized;
}


// MOVED TO: igny8.php - Admin assets enqueuing moved to main plugin file

// ---------------------------------------------------------------------
// WORDPRESS FEATURE REGISTRATION
// ---------------------------------------------------------------------

function igny8_init_wordpress_features() {
    // Initialize module manager
    add_action('init', 'igny8_module_manager');

    // Register taxonomies
    add_action('init', 'igny8_register_taxonomies');

    // Register post meta once
    add_action('init', function() {
        if (!get_option('igny8_post_meta_registered')) {
            igny8_register_post_meta();
            update_option('igny8_post_meta_registered', true);
        }
    });
}

//Initialize WordPress features
igny8_init_wordpress_features();
