<?php
/**
 * Class LSC_Admin_Users
 * Handles the chatter user management admin page.
 */
class LSC_Admin_Users {

    public function __construct() {
        add_action( 'admin_post_lsc_create_user',  [ $this, 'handle_create_user' ] );
        add_action( 'admin_post_lsc_delete_user',  [ $this, 'handle_delete_user' ] );
        add_action( 'admin_post_lsc_toggle_user',  [ $this, 'handle_toggle_user' ] );
    }

    /**
     * Render the chatter users page.
     */
    public static function page_users() {
        $chatters = get_users( [ 'role' => 'chatter' ] );
        require_once LSC_PLUGIN_DIR . 'admin/views/view-users.php';
    }

    /**
     * Handle create user form submission.
     */
    public function handle_create_user() {
        check_admin_referer( 'lsc_create_user' );

        if ( ! current_user_can( 'lsc_manage_users' ) ) {
            wp_die( esc_html__( 'Permission denied.', 'live-support-chat' ) );
        }

        $username  = isset( $_POST['username'] )  ? sanitize_user( wp_unslash( $_POST['username'] ) )      : '';
        $email     = isset( $_POST['email'] )     ? sanitize_email( wp_unslash( $_POST['email'] ) )        : '';
        $password  = isset( $_POST['password'] )  ? wp_unslash( $_POST['password'] )                       : '';
        $can_chat  = isset( $_POST['can_chat'] )  ? 1 : 0;

        if ( empty( $username ) || empty( $email ) || empty( $password ) ) {
            wp_safe_redirect( add_query_arg( [ 'page' => 'lsc-users', 'error' => 'missing_fields' ], admin_url( 'admin.php' ) ) );
            exit;
        }

        if ( username_exists( $username ) || email_exists( $email ) ) {
            wp_safe_redirect( add_query_arg( [ 'page' => 'lsc-users', 'error' => 'user_exists' ], admin_url( 'admin.php' ) ) );
            exit;
        }

        $user_id = wp_create_user( $username, $password, $email );

        if ( is_wp_error( $user_id ) ) {
            wp_safe_redirect( add_query_arg( [ 'page' => 'lsc-users', 'error' => 'create_failed' ], admin_url( 'admin.php' ) ) );
            exit;
        }

        $user = new WP_User( $user_id );
        $user->set_role( 'chatter' );
        update_user_meta( $user_id, 'lsc_can_chat', $can_chat );

        wp_safe_redirect( add_query_arg( [ 'page' => 'lsc-users', 'created' => 1 ], admin_url( 'admin.php' ) ) );
        exit;
    }

    /**
     * Handle delete user form submission.
     */
    public function handle_delete_user() {
        check_admin_referer( 'lsc_delete_user' );

        if ( ! current_user_can( 'lsc_manage_users' ) ) {
            wp_die( esc_html__( 'Permission denied.', 'live-support-chat' ) );
        }

        $user_id = isset( $_POST['user_id'] ) ? (int) $_POST['user_id'] : 0;
        if ( $user_id ) {
            require_once ABSPATH . 'wp-admin/includes/user.php';
            wp_delete_user( $user_id );
        }

        wp_safe_redirect( add_query_arg( [ 'page' => 'lsc-users', 'deleted' => 1 ], admin_url( 'admin.php' ) ) );
        exit;
    }

    /**
     * Handle toggle can_chat meta.
     */
    public function handle_toggle_user() {
        check_admin_referer( 'lsc_toggle_user' );

        if ( ! current_user_can( 'lsc_manage_users' ) ) {
            wp_die( esc_html__( 'Permission denied.', 'live-support-chat' ) );
        }

        $user_id  = isset( $_POST['user_id'] )  ? (int) $_POST['user_id']     : 0;
        $can_chat = isset( $_POST['can_chat'] ) ? (int) $_POST['can_chat']    : 0;

        if ( $user_id ) {
            update_user_meta( $user_id, 'lsc_can_chat', $can_chat ? 0 : 1 );
        }

        wp_safe_redirect( add_query_arg( [ 'page' => 'lsc-users' ], admin_url( 'admin.php' ) ) );
        exit;
    }
}
