const { WorshipLeader } = require('../models');
const { Op } = require('sequelize');

function normalizeLeaderPayload(body) {
  const code = String(body.code || '').trim();
  const name = String(body.name || '').trim();
  const aliases = String(body.aliases || '').trim();
  const active = body.active === undefined ? true : !!body.active;
  return { code, name, aliases, active };
}

exports.getAllWorshipLeaders = async (req, res) => {
  try {
    const includeInactive = String(req.query?.includeInactive || '').toLowerCase();
    const wantsInactive = includeInactive === '1' || includeInactive === 'true' || includeInactive === 'yes';

    const where = wantsInactive ? undefined : { active: true };
    const leaders = await WorshipLeader.findAll({
      where,
      order: [['code', 'ASC']],
    });
    res.json(leaders);
  } catch (error) {
    console.error('getAllWorshipLeaders:', error);
    res.status(500).json({ error: 'Failed to fetch worship leaders' });
  }
};

exports.createWorshipLeader = async (req, res) => {
  try {
    const payload = normalizeLeaderPayload(req.body || {});
    if (!payload.code || !payload.name) {
      return res.status(400).json({ message: 'code und name sind Pflichtfelder.' });
    }
    const existing = await WorshipLeader.findOne({ where: { code: payload.code } });
    if (existing) {
      return res.status(409).json({ message: `Kürzel "${payload.code}" existiert bereits.` });
    }
    const created = await WorshipLeader.create(payload);
    res.status(201).json(created);
  } catch (error) {
    console.error('createWorshipLeader:', error);
    res.status(500).json({ error: 'Failed to create worship leader' });
  }
};

exports.updateWorshipLeader = async (req, res) => {
  try {
    const { id } = req.params;
    const leader = await WorshipLeader.findByPk(id);
    if (!leader) {
      return res.status(404).json({ message: 'Worship leader not found' });
    }

    const payload = normalizeLeaderPayload(req.body || {});
    if (!payload.code || !payload.name) {
      return res.status(400).json({ message: 'code und name sind Pflichtfelder.' });
    }

    const codeClash = await WorshipLeader.findOne({
      where: { code: payload.code, id: { [Op.ne]: id } },
    });
    if (codeClash) {
      return res.status(409).json({ message: `Kürzel "${payload.code}" existiert bereits.` });
    }

    await leader.update(payload);
    res.json(leader);
  } catch (error) {
    console.error('updateWorshipLeader:', error);
    res.status(500).json({ error: 'Failed to update worship leader' });
  }
};

exports.deleteWorshipLeader = async (req, res) => {
  try {
    const { id } = req.params;
    const deleted = await WorshipLeader.destroy({ where: { id } });
    if (!deleted) {
      return res.status(404).json({ message: 'Worship leader not found' });
    }
    res.status(204).json();
  } catch (error) {
    console.error('deleteWorshipLeader:', error);
    res.status(500).json({ error: 'Failed to delete worship leader' });
  }
};

