<?php

namespace MovieEngine\Admin\General;

if (! defined("ABSPATH")) {
    exit; // Exit if accessed directly
}

/**
 * User Settings Tab
 */
class UserSettings {

    public static function render() {
        settings_fields('movie_engine_general');
        $general_settings = get_option('movie_engine_general_settings', []);

        $block_wp_admin = $general_settings['movie_engine_block_wp_admin'] ?? ($general_settings['block_wp_admin'] ?? 1);
        $enable_custom_login = $general_settings['movie_engine_enable_custom_login'] ?? ($general_settings['enable_custom_login'] ?? 1);
        $enable_device_management = $general_settings['movie_engine_enable_device_management'] ?? ($general_settings['enable_device_management'] ?? 0);
        $device_limit_count = $general_settings['movie_engine_device_limit_count'] ?? ($general_settings['device_limit_count'] ?? 2);
        $login_redirect = $general_settings['movie_engine_login_redirect'] ?? ($general_settings['login_redirect'] ?? 'dashboard');
?>
        <input type="hidden" name="movie_engine_general_settings[active_sub_tab]" id="movie-engine-active-sub-tab" value="user_settings">
        <div class="movie-engine-settings-section">
            <div class="movie-engine-section-header">
                <h2><?php esc_html_e('User Settings', 'cine-movie-engine'); ?></h2>
                <p class="description"><?php esc_html_e('Manage user access, security, and device limits.', 'cine-movie-engine'); ?></p>
            </div>

            <div class="movie-engine-card-grid">
                <div class="movie-engine-card">
                    <div class="movie-engine-card-header">
                        <h3><span class="dashicons dashicons-admin-users"></span> <?php esc_html_e('Access Control', 'cine-movie-engine'); ?></h3>
                    </div>
                    <div class="movie-engine-card-body">
                        <div class="movie-engine-form-group">
                            <label class="movie-engine-checkbox-label">
                                <input type="hidden" name="movie_engine_general_settings[movie_engine_block_wp_admin]" value="0">
                                <input type="checkbox" name="movie_engine_general_settings[movie_engine_block_wp_admin]" value="1" <?php checked(1, $block_wp_admin); ?>>
                                <span><?php esc_html_e('Block WP-Admin Access', 'cine-movie-engine'); ?></span>
                            </label>
                            <p class="movie-engine-help-text"><?php esc_html_e('Prevent non-admin users from accessing the WordPress dashboard. They will be redirected to the frontend dashboard.', 'cine-movie-engine'); ?></p>
                        </div>

                        <div class="movie-engine-form-group">
                            <label class="movie-engine-checkbox-label">
                                <input type="hidden" name="movie_engine_general_settings[movie_engine_enable_custom_login]" value="0">
                                <input type="checkbox" name="movie_engine_general_settings[movie_engine_enable_custom_login]" value="1" <?php checked(1, $enable_custom_login); ?>>
                                <span><?php esc_html_e('Enable Custom Login', 'cine-movie-engine'); ?></span>
                            </label>
                            <p class="movie-engine-help-text"><?php esc_html_e('Redirect standard WordPress login page to the custom login page.', 'cine-movie-engine'); ?></p>
                        </div>

                        <div class="movie-engine-form-group">
                            <label for="login_redirect"><?php esc_html_e('Login Redirect', 'cine-movie-engine'); ?></label>
                            <select name="movie_engine_general_settings[movie_engine_login_redirect]" id="login_redirect" class="movie-engine-select">
                                <option value="dashboard" <?php selected('dashboard', $login_redirect); ?>><?php esc_html_e('User Dashboard', 'cine-movie-engine'); ?></option>
                                <option value="home" <?php selected('home', $login_redirect); ?>><?php esc_html_e('Home Page', 'cine-movie-engine'); ?></option>
                                <option value="previous" <?php selected('previous', $login_redirect); ?>><?php esc_html_e('Previous Page', 'cine-movie-engine'); ?></option>
                            </select>
                            <p class="movie-engine-help-text"><?php esc_html_e('Where to send users after they log in.', 'cine-movie-engine'); ?></p>
                        </div>
                    </div>
                </div>

                <?php
                if (has_action('movie_engine_render_device_settings')) {
                    do_action('movie_engine_render_device_settings', $general_settings);
                } else {
                ?>
                    <div class="movie-engine-card">
                        <div class="movie-engine-card-header">
                            <h3>
                                <span class="dashicons dashicons-smartphone"></span>
                                <?php esc_html_e('Device Management', 'cine-movie-engine'); ?>
                                <span class="me-pro-badge"><?php esc_html_e('Pro', 'cine-movie-engine'); ?></span>
                            </h3>
                        </div>
                        <div class="movie-engine-card-body">
                            <div class="me-pro-notice-box">
                                <span class="dashicons dashicons-lock"></span>
                                <div class="me-pro-notice-message">
                                    <strong><?php esc_html_e('Device Management', 'cine-movie-engine'); ?></strong>
                                    <p><?php esc_html_e('Multi-device management and session limits are PRO features. By default, 1 device is allowed per user.', 'cine-movie-engine'); ?></p>
                                </div>
                                <a href="<?php echo esc_url(\MovieEngine\Admin\Upgrade::get_pro_url('pricing')); ?>" target="_blank" class="button button-primary me-pro-notice-btn">
                                    <?php esc_html_e('Unlock Unlimited Devices', 'cine-movie-engine'); ?>
                                </a>
                            </div>

                            <div class="me-locked-feature">
                                <div class="movie-engine-form-group">
                                    <label class="movie-engine-checkbox-label">
                                        <input type="checkbox" checked disabled>
                                        <span><?php esc_html_e('Enable Device Limits', 'cine-movie-engine'); ?></span>
                                    </label>
                                    <p class="movie-engine-help-text"><?php esc_html_e('Limit the number of active devices a user can have at once.', 'cine-movie-engine'); ?></p>
                                </div>

                                <div class="movie-engine-form-group">
                                    <label><?php esc_html_e('Maximum Devices', 'cine-movie-engine'); ?></label>
                                    <input type="number" value="1" class="movie-engine-input" disabled>
                                    <p class="movie-engine-help-text"><?php esc_html_e('Number of simultaneous active sessions allowed per user.', 'cine-movie-engine'); ?></p>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php } ?>
            </div>

            <div class="movie-engine-form-actions">
                <?php submit_button(__('Save User Settings', 'cine-movie-engine'), 'primary me-btn-primary', 'submit', false); ?>
            </div>
        </div>

<?php
    }
}
