"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getProfile = exports.updatePassword = exports.updateProfile = void 0;
const logger_1 = __importDefault(require("../../../logger"));
const auth0Api_1 = require("../../../util/auth0/auth0Api");
const updateProfile = async (req, res) => {
    try {
        const { email, firstName, lastName } = req.body;
        const userId = req.auth?.payload?.sub;
        if (!userId) {
            res.status(401).json({
                success: false,
                message: 'User not authenticated',
            });
            return;
        }
        const updatedUser = await (0, auth0Api_1.callAuth0Api)({
            method: 'PATCH',
            endpoint: `/users/${userId}`,
            body: {
                email,
                given_name: firstName,
                family_name: lastName,
            },
        });
        res.json({
            success: true,
            data: updatedUser,
        });
    }
    catch (error) {
        logger_1.default.error(`Profile update failed: ${error.message}`);
        res.status(500).json({
            success: false,
            message: error.message || 'Failed to update user profile',
        });
    }
};
exports.updateProfile = updateProfile;
const updatePassword = async (req, res) => {
    try {
        const { newPassword, currentPassword } = req.body;
        const userId = req.auth?.payload?.sub;
        if (!userId) {
            res.status(401).json({
                success: false,
                message: 'User not authenticated',
            });
            return;
        }
        // First verify current password
        await (0, auth0Api_1.callAuth0Api)({
            method: 'POST',
            endpoint: '/oauth/token',
            body: {
                grant_type: 'password',
                username: req.auth?.payload?.email,
                password: currentPassword,
                client_id: process.env.AUTH0_M2M_CLIENT_ID,
                client_secret: process.env.AUTH0_M2M_CLIENT_SECRET,
                audience: process.env.AUTH0_AUDIENCE,
                scope: 'openid profile email',
            },
            useManagementApi: false,
        });
        // Update password if current password is valid
        await (0, auth0Api_1.callAuth0Api)({
            method: 'PATCH',
            endpoint: `/users/${userId}`,
            body: {
                password: newPassword,
                connection: 'Username-Password-Authentication',
            },
        });
        res.json({
            success: true,
        });
    }
    catch (error) {
        logger_1.default.error(`Password update failed: ${error.message}`);
        res.status(500).json({
            success: false,
            message: error.message || 'Failed to update password',
        });
    }
};
exports.updatePassword = updatePassword;
const getProfile = async (req, res) => {
    try {
        const userId = req.auth?.payload?.sub;
        if (!userId) {
            res.status(401).json({
                success: false,
                message: 'User not authenticated',
            });
            return;
        }
        const userData = await (0, auth0Api_1.callAuth0Api)({
            method: 'GET',
            endpoint: `/users/${userId}`,
        });
        res.json({
            success: true,
            data: userData,
        });
    }
    catch (error) {
        logger_1.default.error(`Failed to fetch user profile: ${error.message}`);
        res.status(500).json({
            success: false,
            message: error.message || 'Failed to fetch user profile',
        });
    }
};
exports.getProfile = getProfile;
//# sourceMappingURL=index.js.map