import { ResetPasswordService } from '../../services/resetpasswordservice.js';
import { sendError } from '../../utils/response.js';
import { GeneralService } from '../../share/generalservice.js';

export class ResetPasswordController {
  constructor() {
    this.resetPasswordService = new ResetPasswordService();
    this.generalService = new GeneralService();
  }

  async onNavigate(req, res) {
    let idx = -1, result = [];
    try {
      const { email, token, newPassword } = req.body.request;
      result = await this.resetPasswordService.resetPassword(email, token, newPassword);
    } catch (error) {
      idx = 1;
      this.generalService.devhint(1, 'resetpasswordcontroller.js', 'Jumpout', error.message);
    } finally {
      if (idx === 1) return sendError('ไม่สามารถรีเซ็ตรหัสผ่านได้', 'Password reset error');
      return result;
    }
  }
}
