"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserService = void 0;
const database_js_1 = require("../config/database.js");
const bcryptjs_1 = __importDefault(require("bcryptjs"));
class UserService {
    static async updateProfile(userId, data) {
        try {
            if (data.username) {
                const existingUser = await database_js_1.prisma.user.findFirst({
                    where: {
                        username: data.username,
                        id: { not: userId },
                    },
                });
                if (existingUser) {
                    return {
                        success: false,
                        error: "Username already taken",
                    };
                }
            }
            if (data.email) {
                const existingUser = await database_js_1.prisma.user.findFirst({
                    where: {
                        email: data.email,
                        id: { not: userId },
                    },
                });
                if (existingUser) {
                    return {
                        success: false,
                        error: "Email already taken",
                    };
                }
            }
            const updateData = {};
            if (data.username)
                updateData.username = data.username;
            if (data.email)
                updateData.email = data.email;
            if (data.avatar !== undefined)
                updateData.avatar = data.avatar;
            if (data.password) {
                updateData.password = await bcryptjs_1.default.hash(data.password, 12);
            }
            const updatedUser = await database_js_1.prisma.user.update({
                where: { id: userId },
                data: updateData,
                select: {
                    id: true,
                    username: true,
                    email: true,
                    avatar: true,
                },
            });
            return {
                success: true,
                data: updatedUser,
            };
        }
        catch (error) {
            console.error("Update profile error:", error);
            return {
                success: false,
                error: "Failed to update profile",
            };
        }
    }
}
exports.UserService = UserService;
