const OnboardingProcess = require('../models/OnboardingProcess');

// ── Departments ──────────────────────────────────────────────────────────────

exports.getDepartments = (req, res) => {
    const data = OnboardingProcess.getAllDepartments();
    res.json({ status: 'success', data });
};

exports.createDepartment = (req, res) => {
    const { name, icon, sort_order } = req.body;
    if (!name?.trim()) {
        return res.status(400).json({ status: 'error', message: 'Name ist erforderlich' });
    }
    try {
        const dept = OnboardingProcess.createDepartment({ name: name.trim(), icon, sort_order });
        res.status(201).json({ status: 'success', data: dept });
    } catch (err) {
        if (err.message?.includes('UNIQUE')) {
            return res.status(409).json({ status: 'error', message: 'Abteilung existiert bereits' });
        }
        throw err;
    }
};

exports.updateDepartment = (req, res) => {
    const { id } = req.params;
    const { name, icon, sort_order } = req.body;
    const dept = OnboardingProcess.updateDepartment(id, { name, icon, sort_order });
    if (!dept) return res.status(404).json({ status: 'error', message: 'Abteilung nicht gefunden' });
    res.json({ status: 'success', data: dept });
};

exports.deleteDepartment = (req, res) => {
    const { id } = req.params;
    const ok = OnboardingProcess.deleteDepartment(id);
    if (!ok) return res.status(404).json({ status: 'error', message: 'Abteilung nicht gefunden' });
    res.json({ status: 'success', message: 'Abteilung gelöscht' });
};

exports.reorderDepartments = (req, res) => {
    const { ids } = req.body;
    if (!Array.isArray(ids)) {
        return res.status(400).json({ status: 'error', message: 'ids muss ein Array sein' });
    }
    OnboardingProcess.reorderDepartments(ids);
    res.json({ status: 'success', message: 'Reihenfolge gespeichert' });
};

// ── Processes ────────────────────────────────────────────────────────────────

exports.getProcesses = (req, res) => {
    const { department_id, applies_to, responsible_team } = req.query;
    const data = OnboardingProcess.getAllProcesses({
        departmentId:    department_id ? Number(department_id) : undefined,
        appliesTo:       applies_to || undefined,
        responsibleTeam: responsible_team || undefined,
    });
    res.json({ status: 'success', data });
};

exports.createProcess = (req, res) => {
    const { department_id, responsible_team, title, description, applies_to, tag, sort_order } = req.body;
    if (!department_id || !title?.trim()) {
        return res.status(400).json({ status: 'error', message: 'department_id und title sind erforderlich' });
    }
    const proc = OnboardingProcess.createProcess({
        department_id, responsible_team, title: title.trim(), description, applies_to, tag, sort_order,
    });
    res.status(201).json({ status: 'success', data: proc });
};

exports.updateProcess = (req, res) => {
    const { id } = req.params;
    const { title, description, applies_to, tag, sort_order, department_id, responsible_team } = req.body;
    const proc = OnboardingProcess.updateProcess(id, { title, description, applies_to, tag, sort_order, department_id, responsible_team });
    if (!proc) return res.status(404).json({ status: 'error', message: 'Prozess nicht gefunden' });
    res.json({ status: 'success', data: proc });
};

exports.deleteProcess = (req, res) => {
    const { id } = req.params;
    const ok = OnboardingProcess.deleteProcess(id);
    if (!ok) return res.status(404).json({ status: 'error', message: 'Prozess nicht gefunden' });
    res.json({ status: 'success', message: 'Prozess gelöscht' });
};

exports.reorderProcesses = (req, res) => {
    const { department_id, ids } = req.body;
    if (!department_id || !Array.isArray(ids)) {
        return res.status(400).json({ status: 'error', message: 'department_id und ids erforderlich' });
    }
    OnboardingProcess.reorderProcesses(department_id, ids);
    res.json({ status: 'success', message: 'Reihenfolge gespeichert' });
};
