import clubCommunicationService from '../services/clubCommunicationService.js';

function handleError(res, scope, error) {
  console.error(`[${scope}] - Error:`, error);
  res.status(error?.status || 500).json({
    error: error?.message || 'internalerror',
  });
}

const clubCommunicationController = {
  async list(req, res) {
    try {
      const { clubId } = req.params;
      const payload = await clubCommunicationService.listClubCommunication(Number(clubId));
      res.json(payload);
    } catch (error) {
      handleError(res, 'listClubCommunication', error);
    }
  },

  async createThread(req, res) {
    try {
      const { clubId } = req.params;
      const thread = await clubCommunicationService.createThread(Number(clubId), req.user?.id || null, req.body || {});
      res.status(201).json({ thread });
    } catch (error) {
      handleError(res, 'createClubCommunicationThread', error);
    }
  },

  async updateThread(req, res) {
    try {
      const { clubId, threadId } = req.params;
      const thread = await clubCommunicationService.updateThread(Number(clubId), Number(threadId), req.body || {});
      res.json({ thread });
    } catch (error) {
      handleError(res, 'updateClubCommunicationThread', error);
    }
  },

  async deleteThread(req, res) {
    try {
      const { clubId, threadId } = req.params;
      await clubCommunicationService.deleteThread(Number(clubId), Number(threadId));
      res.status(204).send();
    } catch (error) {
      handleError(res, 'deleteClubCommunicationThread', error);
    }
  },

  async addMessage(req, res) {
    try {
      const { clubId, threadId } = req.params;
      const message = await clubCommunicationService.addMessage(Number(clubId), Number(threadId), req.user?.id || null, req.body || {});
      res.status(201).json({ message });
    } catch (error) {
      handleError(res, 'addClubCommunicationMessage', error);
    }
  },

  async sendThread(req, res) {
    try {
      const { clubId, threadId } = req.params;
      const thread = await clubCommunicationService.sendThread(Number(clubId), Number(threadId), req.user?.id || null);
      res.json({ thread });
    } catch (error) {
      handleError(res, 'sendClubCommunicationThread', error);
    }
  },

  async createGroup(req, res) {
    try {
      const { clubId } = req.params;
      const group = await clubCommunicationService.createGroup(Number(clubId), req.body || {});
      res.status(201).json({ group });
    } catch (error) {
      handleError(res, 'createClubDistributionGroup', error);
    }
  },

  async updateGroup(req, res) {
    try {
      const { clubId, groupId } = req.params;
      const group = await clubCommunicationService.updateGroup(Number(clubId), Number(groupId), req.body || {});
      res.json({ group });
    } catch (error) {
      handleError(res, 'updateClubDistributionGroup', error);
    }
  },

  async deleteGroup(req, res) {
    try {
      const { clubId, groupId } = req.params;
      await clubCommunicationService.deleteGroup(Number(clubId), Number(groupId));
      res.status(204).send();
    } catch (error) {
      handleError(res, 'deleteClubDistributionGroup', error);
    }
  },

  async createTemplate(req, res) {
    try {
      const { clubId } = req.params;
      const template = await clubCommunicationService.createTemplate(Number(clubId), req.body || {});
      res.status(201).json({ template });
    } catch (error) {
      handleError(res, 'createClubCommunicationTemplate', error);
    }
  },

  async updateTemplate(req, res) {
    try {
      const { clubId, templateId } = req.params;
      const template = await clubCommunicationService.updateTemplate(Number(clubId), Number(templateId), req.body || {});
      res.json({ template });
    } catch (error) {
      handleError(res, 'updateClubCommunicationTemplate', error);
    }
  },

  async deleteTemplate(req, res) {
    try {
      const { clubId, templateId } = req.params;
      await clubCommunicationService.deleteTemplate(Number(clubId), Number(templateId));
      res.status(204).send();
    } catch (error) {
      handleError(res, 'deleteClubCommunicationTemplate', error);
    }
  },
};

export default clubCommunicationController;
