const MailTemplateService = require('../../services/template/MailTemplateService');

class MailTemplatesController {
  static _currentUserId(req) {
    return req.user?.userId ?? req.user?.id ?? null;
  }

  static async list(req, res) {
    try {
      const data = await MailTemplateService.list(req.query || {});
      return res.json({ success: true, data });
    } catch (error) {
      return res.status(error?.status || 500).json({ success: false, message: error.message || 'Failed to list mail templates' });
    }
  }

  static async getById(req, res) {
    try {
      const data = await MailTemplateService.getById(req.params.id);
      if (!data) {
        return res.status(404).json({ success: false, message: 'Mail template not found' });
      }
      return res.json({ success: true, data });
    } catch (error) {
      return res.status(error?.status || 500).json({ success: false, message: error.message || 'Failed to load mail template' });
    }
  }

  static async create(req, res) {
    try {
      const data = await MailTemplateService.create(req.body || {}, MailTemplatesController._currentUserId(req));
      return res.status(201).json({ success: true, data });
    } catch (error) {
      return res.status(error?.status || 500).json({ success: false, message: error.message || 'Failed to create mail template' });
    }
  }

  static async update(req, res) {
    try {
      const data = await MailTemplateService.update(req.params.id, req.body || {}, MailTemplatesController._currentUserId(req));
      if (!data) {
        return res.status(404).json({ success: false, message: 'Mail template not found' });
      }
      return res.json({ success: true, data });
    } catch (error) {
      return res.status(error?.status || 500).json({ success: false, message: error.message || 'Failed to update mail template' });
    }
  }

  static async activate(req, res) {
    try {
      const data = await MailTemplateService.activate(req.params.id, MailTemplatesController._currentUserId(req));
      if (!data) {
        return res.status(404).json({ success: false, message: 'Mail template not found' });
      }
      return res.json({ success: true, data });
    } catch (error) {
      return res.status(error?.status || 500).json({ success: false, message: error.message || 'Failed to activate mail template' });
    }
  }

  static async archive(req, res) {
    try {
      const data = await MailTemplateService.archive(req.params.id, MailTemplatesController._currentUserId(req));
      if (!data) {
        return res.status(404).json({ success: false, message: 'Mail template not found' });
      }
      return res.json({ success: true, data });
    } catch (error) {
      return res.status(error?.status || 500).json({ success: false, message: error.message || 'Failed to archive mail template' });
    }
  }

  static async unarchive(req, res) {
    try {
      const data = await MailTemplateService.unarchive(req.params.id, MailTemplatesController._currentUserId(req));
      if (!data) {
        return res.status(404).json({ success: false, message: 'Mail template not found' });
      }
      return res.json({ success: true, data });
    } catch (error) {
      return res.status(error?.status || 500).json({ success: false, message: error.message || 'Failed to unarchive mail template' });
    }
  }

  static async remove(req, res) {
    try {
      const deleted = await MailTemplateService.remove(req.params.id);
      if (!deleted) {
        return res.status(404).json({ success: false, message: 'Mail template not found' });
      }
      return res.json({ success: true });
    } catch (error) {
      return res.status(error?.status || 500).json({ success: false, message: error.message || 'Failed to delete mail template' });
    }
  }
}

module.exports = MailTemplatesController;
