import friendlyMatchSharedService from '../services/friendlyMatchSharedService.js';
import {
  emitFriendlyInvitationAccepted,
  emitFriendlyInvitationCreated,
  emitFriendlyInvitationDeclined,
  emitFriendlySharedMatchUpdated,
} from '../services/socketService.js';

function userTokenFrom(req) {
  const authHeader = req.headers.authorization;
  if (authHeader && authHeader.startsWith('Bearer ')) {
    return authHeader.slice(7);
  }
  return req.headers.authcode || authHeader;
}

export const createFriendlyMatchInvitation = async (req, res) => {
  try {
    const invitation = await friendlyMatchSharedService.createInvitation(userTokenFrom(req), req.params.clubId, req.body);
    emitFriendlyInvitationCreated(invitation.fromClubId, invitation.toClubId, invitation);
    res.status(201).json(invitation);
  } catch (error) {
    console.error('[createFriendlyMatchInvitation] Error:', error);
    res.status(error.statusCode || 500).json({ error: error.message || 'Einladung konnte nicht erstellt werden.' });
  }
};

export const listIncomingFriendlyMatchInvitations = async (req, res) => {
  try {
    const items = await friendlyMatchSharedService.listIncomingInvitations(userTokenFrom(req), req.params.clubId);
    res.status(200).json(items);
  } catch (error) {
    console.error('[listIncomingFriendlyMatchInvitations] Error:', error);
    res.status(error.statusCode || 500).json({ error: error.message || 'Eingehende Einladungen konnten nicht geladen werden.' });
  }
};

export const listOutgoingFriendlyMatchInvitations = async (req, res) => {
  try {
    const items = await friendlyMatchSharedService.listOutgoingInvitations(userTokenFrom(req), req.params.clubId);
    res.status(200).json(items);
  } catch (error) {
    console.error('[listOutgoingFriendlyMatchInvitations] Error:', error);
    res.status(error.statusCode || 500).json({ error: error.message || 'Ausgehende Einladungen konnten nicht geladen werden.' });
  }
};

export const acceptFriendlyMatchInvitation = async (req, res) => {
  try {
    const result = await friendlyMatchSharedService.acceptInvitation(userTokenFrom(req), req.params.clubId, req.params.invitationId);
    emitFriendlyInvitationAccepted(result.invitation.fromClubId, result.invitation.toClubId, result.invitation);
    emitFriendlySharedMatchUpdated(result.sharedMatch.homeClubId, result.sharedMatch.guestClubId, result.sharedMatch);
    res.status(200).json(result);
  } catch (error) {
    console.error('[acceptFriendlyMatchInvitation] Error:', error);
    res.status(error.statusCode || 500).json({ error: error.message || 'Einladung konnte nicht angenommen werden.' });
  }
};

export const declineFriendlyMatchInvitation = async (req, res) => {
  try {
    const invitation = await friendlyMatchSharedService.declineInvitation(userTokenFrom(req), req.params.clubId, req.params.invitationId);
    emitFriendlyInvitationDeclined(invitation.fromClubId, invitation.toClubId, invitation.id);
    res.status(200).json({ success: true, id: invitation.id });
  } catch (error) {
    console.error('[declineFriendlyMatchInvitation] Error:', error);
    res.status(error.statusCode || 500).json({ error: error.message || 'Einladung konnte nicht abgelehnt werden.' });
  }
};
