import * as emailAccountService from '../services/email-account.service.js';
import {
  logEmailLink,
} from '../services/audit.service.js';

/**
 * Get all email accounts for logged-in user
 * GET /api/email-accounts
 */
export const getEmailAccounts = async (req, res, next) => {
  try {
    const userId = req.userId;
    const accounts = await emailAccountService.getUserEmailAccounts(userId);

    res.status(200).json({
      success: true,
      data: accounts,
    });
  } catch (error) {
    next(error);
  }
};

/**
 * Get a specific email account
 * GET /api/email-accounts/:id
 */
export const getEmailAccount = async (req, res, next) => {
  try {
    const userId = req.userId;
    const { id } = req.params;

    const account = await emailAccountService.getEmailAccountById(id, userId);

    res.status(200).json({
      success: true,
      data: account,
    });
  } catch (error) {
    next(error);
  }
};

/**
 * Create a new email account
 * POST /api/email-accounts
 */
export const createEmailAccount = async (req, res, next) => {
  const { email, emailPassword } = req.body;
  const userId = req.userId;
  const ipAddress = req.ip || req.connection.remoteAddress;
  const userAgent = req.headers['user-agent'];

  try {
    const account = await emailAccountService.createEmailAccount(
      userId,
      email,
      emailPassword
    );

    // Log email account creation
    await logEmailLink(userId, email, ipAddress, userAgent);

    res.status(201).json({
      success: true,
      data: account,
      message: 'Email účet úspešne pripojený',
    });
  } catch (error) {
    next(error);
  }
};

/**
 * Update email account password
 * PATCH /api/email-accounts/:id/password
 */
export const updateEmailAccountPassword = async (req, res, next) => {
  try {
    const userId = req.userId;
    const { id } = req.params;
    const { emailPassword } = req.body;

    const result = await emailAccountService.updateEmailAccountPassword(
      id,
      userId,
      emailPassword
    );

    res.status(200).json({
      success: true,
      data: result,
      message: 'Heslo k emailovému účtu bolo aktualizované',
    });
  } catch (error) {
    next(error);
  }
};

/**
 * Toggle email account active status
 * PATCH /api/email-accounts/:id/status
 */
export const toggleEmailAccountStatus = async (req, res, next) => {
  try {
    const userId = req.userId;
    const { id } = req.params;
    const { isActive } = req.body;

    const result = await emailAccountService.toggleEmailAccountStatus(
      id,
      userId,
      isActive
    );

    res.status(200).json({
      success: true,
      data: result,
      message: `Email účet ${isActive ? 'aktivovaný' : 'deaktivovaný'}`,
    });
  } catch (error) {
    next(error);
  }
};

/**
 * Set email account as primary
 * POST /api/email-accounts/:id/set-primary
 */
export const setPrimaryEmailAccount = async (req, res, next) => {
  try {
    const userId = req.userId;
    const { id } = req.params;

    const result = await emailAccountService.setPrimaryEmailAccount(id, userId);

    res.status(200).json({
      success: true,
      data: result,
      message: 'Primárny email účet bol nastavený',
    });
  } catch (error) {
    next(error);
  }
};

/**
 * Delete email account
 * DELETE /api/email-accounts/:id
 */
export const deleteEmailAccount = async (req, res, next) => {
  try {
    const userId = req.userId;
    const { id } = req.params;

    const result = await emailAccountService.removeUserFromEmailAccount(id, userId);

    res.status(200).json({
      success: true,
      data: result,
      message: result.message,
    });
  } catch (error) {
    next(error);
  }
};
