"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ResetPasswordCommandHandler = void 0;
const TokenService_1 = require("../../Services/TokenService");
const PasswordService_1 = require("../../Services/PasswordService");
const Logger_1 = require("../../Services/Logger");
class ResetPasswordCommandHandler {
    constructor(userRepo) {
        this.userRepo = userRepo;
    }
    async execute(cmd) {
        try {
            if (!cmd.token) {
                throw new Error('Reset token is required');
            }
            if (!cmd.newPassword) {
                throw new Error('New password is required');
            }
            // Validate password strength
            const validation = PasswordService_1.PasswordService.validatePasswordStrength(cmd.newPassword);
            if (!validation.isValid) {
                throw new Error(`Password validation failed: ${validation.errors.join(', ')}`);
            }
            // Hash the token to compare with stored value
            const hashedToken = await TokenService_1.TokenService.hashToken(cmd.token);
            // Find user with this password reset token
            const user = await this.userRepo.findByToken(hashedToken);
            if (!user) {
                throw new Error('Invalid or expired reset token');
            }
            // Check if token is expired
            if (user.TokenExpires && user.TokenExpires < new Date()) {
                throw new Error('Reset token has expired');
            }
            // Hash the new password
            const hashedPassword = await PasswordService_1.PasswordService.hashPassword(cmd.newPassword);
            // Update user password and clear reset token
            user.password = hashedPassword;
            user.token = null;
            user.TokenExpires = null;
            await this.userRepo.update(user.id, user);
            return true;
        }
        catch (error) {
            (0, Logger_1.logError)('Password reset error', error instanceof Error ? error : new Error(String(error)));
            throw error;
        }
    }
}
exports.ResetPasswordCommandHandler = ResetPasswordCommandHandler;
//# sourceMappingURL=ResetPasswordCommandHandler.js.map