<?php
/**
 * Service : Gestion de l'état utilisateur en BDD
 */

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

if ( ! class_exists( 'Avision_User_State' ) ) {

    class Avision_User_State {

        public static function save_level_status( $user_id, $level, $status ) {
            if ( ! $user_id ) return false;
            
            $current_data = self::get_all_progress( $user_id );
            $current_data[$level] = $status;
            
            return update_user_meta( $user_id, '_avision_progress', $current_data );
        }

        public static function get_all_progress( $user_id ) {
            if ( ! $user_id ) return array( 1 => 'start', 2 => 'locked', 3 => 'locked' );
            
            $data = get_user_meta( $user_id, '_avision_progress', true );
            return is_array( $data ) ? $data : array( 1 => 'start', 2 => 'locked', 3 => 'locked' );
        }

        public static function save_audit_email( $user_id, $email ) {
            return update_user_meta( $user_id, '_avision_audit_email', sanitize_email( $email ) );
        }
    }
}
