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

const getMemberOrders = async (req, res) => {
  try {
    const { clubId, memberId } = req.params;
    const { authcode: userToken } = req.headers;
    const result = await memberOrderService.getMemberOrders(userToken, clubId, memberId);
    res.status(result.status).json(result.response);
  } catch (error) {
    console.error('[getMemberOrders] - Error:', error);
    res.status(500).json({ success: false, error: 'Bestellungen konnten nicht geladen werden.' });
  }
};

const createMemberOrder = async (req, res) => {
  try {
    const { clubId, memberId } = req.params;
    const { authcode: userToken } = req.headers;
    const result = await memberOrderService.createMemberOrder(userToken, clubId, memberId, req.body || {});
    res.status(result.status).json(result.response);
  } catch (error) {
    console.error('[createMemberOrder] - Error:', error);
    res.status(500).json({ success: false, error: 'Bestellung konnte nicht gespeichert werden.' });
  }
};

const updateMemberOrder = async (req, res) => {
  try {
    const { clubId, memberId, orderId } = req.params;
    const { authcode: userToken } = req.headers;
    const result = await memberOrderService.updateMemberOrder(userToken, clubId, memberId, orderId, req.body || {});
    res.status(result.status).json(result.response);
  } catch (error) {
    console.error('[updateMemberOrder] - Error:', error);
    res.status(500).json({ success: false, error: 'Bestellung konnte nicht aktualisiert werden.' });
  }
};

const getGlobalOrders = async (req, res) => {
  try {
    const { authcode: userToken } = req.headers;
    const result = await memberOrderService.getGlobalOrders(userToken);
    res.status(result.status).json(result.response);
  } catch (error) {
    console.error('[getGlobalOrders] - Error:', error);
    res.status(500).json({ success: false, error: 'Bestellübersicht konnte nicht geladen werden.' });
  }
};

export {
  getMemberOrders,
  createMemberOrder,
  updateMemberOrder,
  getGlobalOrders
};
