<?php

namespace MODELORA\Core;

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

class Admin
{
    public function register(): void
    {
        add_action('admin_menu', [$this, 'menu']);
        add_action('admin_init', [$this, 'settings']);
    }

    public function menu(): void
    {
        add_menu_page(
            __('Modelora', 'modelora-3d-product-viewer'),
            __('Modelora', 'modelora-3d-product-viewer'),
            'manage_options',
            'modelora-add-models',
            [$this, 'render_dashboard'],
            'dashicons-format-gallery',
            56
        );

        add_submenu_page(
            'modelora-add-models',
            __('All Models', 'modelora-3d-product-viewer'),
            __('All Models', 'modelora-3d-product-viewer'),
            'manage_options',
            'edit.php?post_type=modelora_model'
        );

        add_submenu_page(
            'modelora-add-models',
            __('Add Models', 'modelora-3d-product-viewer'),
            __('Add Models', 'modelora-3d-product-viewer'),
            'manage_options',
            'modelora-add-models',
            [$this, 'render_dashboard']
        );

        add_submenu_page(
            'modelora-add-models',
            __('Settings', 'modelora-3d-product-viewer'),
            __('Settings', 'modelora-3d-product-viewer'),
            'manage_options',
            'modelora-settings',
            [$this, 'render_settings']
        );
    }

    public function settings(): void
    {
        register_setting('modelora_settings_group', 'modelora_settings', [
            'sanitize_callback' => [$this, 'sanitize_settings'],
            'default' => [],
        ]);
    }

    public function sanitize_settings(array $settings): array
    {
        return [
            'lazy_load' => ! empty($settings['lazy_load']) ? 1 : 0,
            'progressive_loading' => ! empty($settings['progressive_loading']) ? 1 : 0,
            'auto_lod' => ! empty($settings['auto_lod']) ? 1 : 0,
            'cdn_enabled' => ! empty($settings['cdn_enabled']) ? 1 : 0,
            'dark_mode_ui' => ! empty($settings['dark_mode_ui']) ? 1 : 0,
            'max_upload_mb' => max(5, min(500, (int) ($settings['max_upload_mb'] ?? 50))),
            'default_preset' => sanitize_key($settings['default_preset'] ?? 'studio'),
        ];
    }

    public function render_dashboard(): void
    {
        echo '<div class="wrap modelora-admin-wrap"><div id="modelora-admin-root"></div></div>';
    }

    public function render_settings(): void
    {
        $settings = get_option('modelora_settings', []);
        ?>
        <div class="wrap modelora-settings-wrap">
            <h1><?php esc_html_e('Modelora Settings', 'modelora-3d-product-viewer'); ?></h1>
            <form method="post" action="options.php" class="modelora-settings-form">
                <?php settings_fields('modelora_settings_group'); ?>
                <div class="modelora-card-grid">
                    <label class="modelora-switch"><input type="checkbox" name="modelora_settings[lazy_load]" value="1" <?php checked($settings['lazy_load'] ?? 1, 1); ?>> <?php esc_html_e('Enable lazy loading', 'modelora-3d-product-viewer'); ?></label>
                    <label class="modelora-switch"><input type="checkbox" name="modelora_settings[progressive_loading]" value="1" <?php checked($settings['progressive_loading'] ?? 1, 1); ?>> <?php esc_html_e('Enable progressive loading', 'modelora-3d-product-viewer'); ?></label>
                    <label class="modelora-switch"><input type="checkbox" name="modelora_settings[auto_lod]" value="1" <?php checked($settings['auto_lod'] ?? 1, 1); ?>> <?php esc_html_e('Enable Auto LOD', 'modelora-3d-product-viewer'); ?></label>
                    <label class="modelora-switch"><input type="checkbox" name="modelora_settings[cdn_enabled]" value="1" <?php checked($settings['cdn_enabled'] ?? 0, 1); ?>> <?php esc_html_e('CDN-ready URLs', 'modelora-3d-product-viewer'); ?></label>
                    <label class="modelora-switch"><input type="checkbox" name="modelora_settings[dark_mode_ui]" value="1" <?php checked($settings['dark_mode_ui'] ?? 0, 1); ?>> <?php esc_html_e('Dark mode dashboard', 'modelora-3d-product-viewer'); ?></label>
                    <label>
                        <?php esc_html_e('Max upload size (MB)', 'modelora-3d-product-viewer'); ?>
                        <input type="number" min="5" max="500" name="modelora_settings[max_upload_mb]" value="<?php echo esc_attr((int) ($settings['max_upload_mb'] ?? 50)); ?>" />
                    </label>
                    <label>
                        <?php esc_html_e('Default preset', 'modelora-3d-product-viewer'); ?>
                        <select name="modelora_settings[default_preset]">
                            <option value="studio" <?php selected($settings['default_preset'] ?? 'studio', 'studio'); ?>>Studio</option>
                            <option value="outdoor" <?php selected($settings['default_preset'] ?? '', 'outdoor'); ?>>Outdoor</option>
                            <option value="dark" <?php selected($settings['default_preset'] ?? '', 'dark'); ?>>Dark</option>
                        </select>
                    </label>
                </div>
                <?php submit_button(__('Save Settings', 'modelora-3d-product-viewer')); ?>
            </form>
        </div>
        <?php
    }
}

