const ApiResponse = require('../utils/apiResponse');
const { asyncHandler } = require('../middleware/errorHandler');
const settingsService = require('../services/profileService');

exports.getOverview = asyncHandler(async (req, res) => {
  const overview = await settingsService.getOverview(req.user.id);
  ApiResponse.success(res, {overview});
});

exports.getProfile = asyncHandler(async (req, res) => {
  const user = await settingsService.getProfile(req.user.id);
  ApiResponse.success(res, { user });
});

exports.updateProfile = asyncHandler(async (req, res) => {
  const user = await settingsService.updateProfile(req.user.id, req.body);
  ApiResponse.success(res, { user }, 'Profile updated successfully');
});

exports.uploadAvatar = asyncHandler(async (req, res) => {
  const result = await settingsService.uploadAvatar(req.user.id, req.file);

  ApiResponse.success(
    res,
    {
      avatar: result.avatar,
      profileImage: result.profileImage,
      user: result.user,
    },
    'Avatar uploaded successfully'
  );
});

exports.deleteAvatar = asyncHandler(async (req, res) => {
  const user = await settingsService.deleteAvatar(req.user.id);
  ApiResponse.success(res, { user }, 'Avatar deleted successfully');
});

exports.getSecurityOverview = asyncHandler(async (req, res) => {
  const data = await settingsService.getSecurityOverview(req.user.id, req.sessionId);
  ApiResponse.success(res, data);
});

exports.changePassword = asyncHandler(async (req, res) => {
  const { currentPassword, newPassword } = req.body;

  const result = await settingsService.changePassword(
    req.user.id,
    currentPassword,
    newPassword
  );

  ApiResponse.success(res, result, 'Password updated successfully');
});

exports.changePin = asyncHandler(async (req, res) => {
  const { currentPin, newPin } = req.body;

  const result = await settingsService.changePin(
    req.user.id,
    currentPin,
    newPin
  );

  ApiResponse.success(res, result, 'Pin updated successfully');
});

exports.verifyPassword2fa = asyncHandler(async (req, res) => {
  const ip = req.ip || req.connection.remoteAddress;
     const device = req.headers['user-agent'];
  const result = await settingsService.verifyPassword2fa(
    req.user.id,
    req.body,
    ip,
    device
  );

  ApiResponse.success(res, result, '2fa has been initiated');
});

exports.enable2FA = asyncHandler(async (req, res) => {
  const { code } = req.body;

  const result = await settingsService.enable2FA(req.user.id, req.body);

  ApiResponse.success(res, result, 'Two-factor authentication enabled');
});

exports.disable2FA = asyncHandler(async (req, res) => {
  const result = await settingsService.disable2FA(req.user.id , req.body);

  ApiResponse.success(res, result, 'Two-factor authentication disabled');
});

exports.resend2fa = asyncHandler(async (req, res) => {

  const ip = req.ip || req.connection.remoteAddress;
  const device = req.headers['user-agent'];
  await settingsService.resend2fa(req.user?.id, ip, device);
  ApiResponse.success(res, null, 'Two factor email sent');
});

exports.updateAntiPhishing = asyncHandler(async (req, res) => {
  const { antiPhishingEnabled, antiPhishingCode } = req.body;

  const result = await settingsService.updateAntiPhishing(
    req.user.id,
    antiPhishingEnabled,
    antiPhishingCode
  );

  ApiResponse.success(res, result, 'Anti-phishing code updated');
});

exports.updateWithdrawalWhitelist = asyncHandler(async (req, res) => {
  const { withdrawalWhitelist } = req.body;

  const result = await settingsService.updateWithdrawalWhitelist(
    req.user.id,
    withdrawalWhitelist
  );

  ApiResponse.success(
    res,
    result,
    `Withdrawal whitelist ${withdrawalWhitelist ? 'enabled' : 'disabled'}`
  );
});

exports.updateLoginType = asyncHandler(async (req, res) => {
  const { currentPassword,loginW } = req.body;

  const result = await settingsService.updateLoginType(
    req.user.id,
    currentPassword,
    loginW
  );

  ApiResponse.success(
    res,
    result,
    `Login type change to  ${loginW}`
  );
});

exports.updateLoginNotifications = asyncHandler(async (req, res) => {
  const { loginNotifications } = req.body;

  const result = await settingsService.updateLoginNotifications(
    req.user.id,
    loginNotifications
  );

  ApiResponse.success(
    res,
    result,
    `Login notifications ${loginNotifications ? 'enabled' : 'disabled'}`
  );
});

exports.revokeSessionByBody = asyncHandler(async (req, res) => {
  const { sessionId } = req.body;

  if (String(sessionId) === String(req.sessionId)) {
    return ApiResponse.error(res, 'Cannot revoke current session', 400);
  }

  await settingsService.revokeSession(req.user.id, sessionId);
  ApiResponse.success(res, null, 'Session revoked');
});

exports.revokeAllSessions = asyncHandler(async (req, res) => {
  await settingsService.revokeAllSessions(req.user.id, req.sessionId);
  ApiResponse.success(res, null, 'All other sessions revoked');
});

exports.regenerateBackupCodes = asyncHandler(async (req, res) => {
  const result = await settingsService.regenerateBackupCodes(req.user.id);
  ApiResponse.success(res, result, 'Backup codes refreshed');
});