import { ipcMain } from 'electron';
import { createHandler } from '../database/LocalSystem.js';
import User, {UserInfoResponse} from '../database/models/User.js';

interface UpdateUserData {
    firstName: string;
    lastName: string;
    username: string;
    email: string;
    authorName?: string;
}

// GET /user/info - Get user info from local DB
ipcMain.handle('db:user:info', createHandler<void, UserInfoResponse>(
    async function(userId: string, _body: void, _lang: 'fr' | 'en'):Promise<UserInfoResponse> {
        return await User.returnUserInfos(userId);
    }
));

// PUT /user/update - Update user info in local DB
ipcMain.handle('db:user:update', createHandler<UpdateUserData, boolean>(
    async function(userId: string, data: UpdateUserData, lang: 'fr' | 'en'):Promise<boolean> {
        const userKey = '';
        return await User.updateUserInfos(userKey, userId, data.firstName, data.lastName, data.username, data.email, data.authorName, lang);
    }
));
