/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2026 metamuffin <metamuffin.org>
*/

use crate::{DATABASE, login::hash_password, session::Session};
use anyhow::Result;
use jellycommon::user::{PlayerKind, Theme, User};

pub fn update_user_password(session: &Session, password: &str) -> Result<()> {
    DATABASE.update_user(&session.user.name, |user| {
        user.password = hash_password(&session.user.name, password);
        Ok(())
    })?;
    Ok(())
}
pub fn update_user_display_name(session: &Session, display_name: &str) -> Result<()> {
    DATABASE.update_user(&session.user.name, |user| {
        user.display_name = display_name.to_owned();
        Ok(())
    })?;
    Ok(())
}
pub fn update_user_native_secret(session: &Session, native_secret: &str) -> Result<()> {
    DATABASE.update_user(&session.user.name, |user| {
        user.native_secret = native_secret.to_owned();
        Ok(())
    })?;
    Ok(())
}
pub fn update_user_theme(session: &Session, theme: Theme) -> Result<()> {
    DATABASE.update_user(&session.user.name, |user| {
        user.theme = theme;
        Ok(())
    })?;
    Ok(())
}
pub fn update_user_player_preference(
    session: &Session,
    player_preference: PlayerKind,
) -> Result<()> {
    DATABASE.update_user(&session.user.name, |user| {
        user.player_preference = player_preference;
        Ok(())
    })?;
    Ok(())
}
pub fn register_user(invitation: &str, username: &str, password: &str) -> Result<()> {
    DATABASE.register_user(
        invitation,
        username,
        User {
            display_name: username.to_owned(),
            name: username.to_owned(),
            password: hash_password(username, password),
            ..Default::default()
        },
    )
}
