import * as emailSignatureService from '../services/email-signature.service.js';

/**
 * Get current user's email signature
 * GET /api/email-signature
 */
export const getSignature = async (req, res, next) => {
  try {
    const signature = await emailSignatureService.getSignature(req.userId);

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

/**
 * Create or update email signature
 * POST /api/email-signature
 */
export const upsertSignature = async (req, res, next) => {
  try {
    const signature = await emailSignatureService.upsertSignature(req.userId, req.body);

    res.status(200).json({
      success: true,
      data: signature,
      message: 'Podpis bol uložený',
    });
  } catch (error) {
    next(error);
  }
};

/**
 * Toggle signature on/off
 * PATCH /api/email-signature/toggle
 */
export const toggleSignature = async (req, res, next) => {
  try {
    const { isEnabled } = req.body;
    const signature = await emailSignatureService.toggleSignature(req.userId, isEnabled);

    res.status(200).json({
      success: true,
      data: signature,
      message: isEnabled ? 'Podpis zapnutý' : 'Podpis vypnutý',
    });
  } catch (error) {
    next(error);
  }
};

/**
 * Get formatted signature text for email
 * GET /api/email-signature/formatted
 */
export const getFormattedSignature = async (req, res, next) => {
  try {
    const signature = await emailSignatureService.getSignature(req.userId);
    const formatted = emailSignatureService.formatSignatureText(signature);

    res.status(200).json({
      success: true,
      data: {
        text: formatted,
        isEnabled: signature?.isEnabled || false,
      },
    });
  } catch (error) {
    next(error);
  }
};

/**
 * Delete email signature
 * DELETE /api/email-signature
 */
export const deleteSignature = async (req, res, next) => {
  try {
    const result = await emailSignatureService.deleteSignature(req.userId);

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