import * as groupService from '../services/group.service.js';
import { logger } from '../utils/logger.js';

export const createGroup = async (req, res, next) => {
  try {
    const { name, memberIds } = req.body;
    const userId = req.user.id;

    const group = await groupService.createGroup(name, userId, memberIds);

    res.status(201).json({
      success: true,
      data: group,
      message: 'Skupina bola vytvorená',
    });
  } catch (error) {
    logger.error('Create group error', error);
    next(error);
  }
};

export const getUserGroups = async (req, res, next) => {
  try {
    const groups = await groupService.getUserGroups(req.user.id);

    res.json({
      success: true,
      data: groups,
    });
  } catch (error) {
    logger.error('Get groups error', error);
    next(error);
  }
};

export const getGroupDetails = async (req, res, next) => {
  try {
    const { groupId } = req.params;
    const group = await groupService.getGroupDetails(groupId, req.user.id);

    res.json({
      success: true,
      data: group,
    });
  } catch (error) {
    logger.error('Get group details error', error);
    next(error);
  }
};

export const getGroupMessages = async (req, res, next) => {
  try {
    const { groupId } = req.params;
    const messages = await groupService.getGroupMessages(groupId, req.user.id);

    res.json({
      success: true,
      data: messages,
    });
  } catch (error) {
    logger.error('Get group messages error', error);
    next(error);
  }
};

export const sendGroupMessage = async (req, res, next) => {
  try {
    const { groupId } = req.params;
    const { content } = req.body;

    const message = await groupService.sendGroupMessage(groupId, req.user.id, content);

    res.status(201).json({
      success: true,
      data: message,
      message: 'Správa odoslaná',
    });
  } catch (error) {
    logger.error('Send group message error', error);
    next(error);
  }
};

export const addGroupMember = async (req, res, next) => {
  try {
    const { groupId } = req.params;
    const { userId } = req.body;

    await groupService.addGroupMember(groupId, userId, req.user.id);

    res.json({
      success: true,
      message: 'Člen bol pridaný',
    });
  } catch (error) {
    logger.error('Add member error', error);
    next(error);
  }
};

export const removeGroupMember = async (req, res, next) => {
  try {
    const { groupId, userId } = req.params;

    await groupService.removeGroupMember(groupId, userId, req.user.id);

    res.json({
      success: true,
      message: 'Člen bol odstránený',
    });
  } catch (error) {
    logger.error('Remove member error', error);
    next(error);
  }
};

export const updateGroupName = async (req, res, next) => {
  try {
    const { groupId } = req.params;
    const { name } = req.body;

    const group = await groupService.updateGroupName(groupId, name, req.user.id);

    res.json({
      success: true,
      data: group,
      message: 'Názov skupiny bol aktualizovaný',
    });
  } catch (error) {
    logger.error('Update group name error', error);
    next(error);
  }
};

export const deleteGroup = async (req, res, next) => {
  try {
    const { groupId } = req.params;

    await groupService.deleteGroup(groupId, req.user.id);

    res.json({
      success: true,
      message: 'Skupina bola odstránená',
    });
  } catch (error) {
    logger.error('Delete group error', error);
    next(error);
  }
};
