const TicketSettings = require('../models/TicketSettings');
const { EmailTemplate } = require('../models/EmailTemplate');
const { EmailDesign } = require('../models/EmailDesign');
const { buildPreviewHtml } = require('../services/email.service');

// ── Categories ────────────────────────────────────────────────────────────────

exports.getCategories = (req, res) => {
    try {
        res.json(TicketSettings.getAllCategories());
    } catch (e) {
        res.status(500).json({ message: e.message });
    }
};

exports.createCategory = (req, res) => {
    const { name, icon } = req.body;
    if (!name?.trim()) return res.status(400).json({ message: 'Name erforderlich' });
    try {
        const cat = TicketSettings.createCategory({ name, icon });
        res.status(201).json(cat);
    } catch (e) {
        if (e.message?.includes('UNIQUE')) return res.status(409).json({ message: 'Kategorie existiert bereits' });
        res.status(500).json({ message: e.message });
    }
};

exports.updateCategory = (req, res) => {
    try {
        const cat = TicketSettings.updateCategory(req.params.id, req.body);
        if (!cat) return res.status(404).json({ message: 'Nicht gefunden' });
        res.json(cat);
    } catch (e) {
        if (e.message?.includes('UNIQUE')) return res.status(409).json({ message: 'Name bereits vergeben' });
        res.status(500).json({ message: e.message });
    }
};

exports.deleteCategory = (req, res) => {
    try {
        TicketSettings.deleteCategory(req.params.id);
        res.json({ success: true });
    } catch (e) {
        res.status(500).json({ message: e.message });
    }
};

exports.reorderCategories = (req, res) => {
    const { ids } = req.body;
    if (!Array.isArray(ids)) return res.status(400).json({ message: 'ids array erforderlich' });
    try {
        TicketSettings.reorderCategories(ids);
        res.json({ success: true });
    } catch (e) {
        res.status(500).json({ message: e.message });
    }
};

// ── Templates ─────────────────────────────────────────────────────────────────

exports.getTemplates = (req, res) => {
    try {
        res.json(TicketSettings.getAllTemplates());
    } catch (e) {
        res.status(500).json({ message: e.message });
    }
};

exports.createTemplate = (req, res) => {
    const { label, title, description, category, priority } = req.body;
    if (!label?.trim()) return res.status(400).json({ message: 'Label erforderlich' });
    try {
        const tpl = TicketSettings.createTemplate({ label, title, description, category, priority });
        res.status(201).json(tpl);
    } catch (e) {
        res.status(500).json({ message: e.message });
    }
};

exports.updateTemplate = (req, res) => {
    try {
        const tpl = TicketSettings.updateTemplate(req.params.id, req.body);
        if (!tpl) return res.status(404).json({ message: 'Nicht gefunden' });
        res.json(tpl);
    } catch (e) {
        res.status(500).json({ message: e.message });
    }
};

exports.deleteTemplate = (req, res) => {
    try {
        TicketSettings.deleteTemplate(req.params.id);
        res.json({ success: true });
    } catch (e) {
        res.status(500).json({ message: e.message });
    }
};

// ── Email Templates ────────────────────────────────────────────────────────────

exports.getEmailTemplates = (req, res) => {
    try {
        res.json(EmailTemplate.getAll());
    } catch (e) {
        res.status(500).json({ message: e.message });
    }
};

exports.updateEmailTemplate = (req, res) => {
    const { type } = req.params;
    const { subject, intro } = req.body;
    if (!subject?.trim() || !intro?.trim()) {
        return res.status(400).json({ message: 'Betreff und Intro-Text sind erforderlich' });
    }
    try {
        const tpl = EmailTemplate.update(type, { subject, intro });
        if (!tpl) return res.status(404).json({ message: 'Vorlage nicht gefunden' });
        res.json(tpl);
    } catch (e) {
        res.status(500).json({ message: e.message });
    }
};

exports.resetEmailTemplate = (req, res) => {
    try {
        const tpl = EmailTemplate.reset(req.params.type);
        if (!tpl) return res.status(404).json({ message: 'Vorlage nicht gefunden' });
        res.json(tpl);
    } catch (e) {
        res.status(500).json({ message: e.message });
    }
};

// ── Email Design ───────────────────────────────────────────────────────────────

exports.getEmailDesign = (req, res) => {
    try { res.json(EmailDesign.getDesign()); }
    catch (e) { res.status(500).json({ message: e.message }); }
};

exports.updateEmailDesign = (req, res) => {
    try { res.json(EmailDesign.updateDesign(req.body)); }
    catch (e) { res.status(500).json({ message: e.message }); }
};

exports.resetEmailDesign = (req, res) => {
    try { res.json(EmailDesign.reset()); }
    catch (e) { res.status(500).json({ message: e.message }); }
};

exports.previewEmail = (req, res) => {
    try {
        const { type } = req.params;
        const design = req.body?.design || {};
        const darkMode = !!req.body?.darkMode;
        const html = buildPreviewHtml(type, design, darkMode);
        res.setHeader('Content-Type', 'text/html; charset=utf-8');
        res.send(html);
    } catch (e) {
        res.status(500).json({ message: e.message });
    }
};
