const { getDatabase } = require('../config/database');

const DEFAULTS = [
    {
        type: 'ticket_created',
        label: 'Ticket-Bestätigung',
        subject: '[{{ticket_number}}] Ticket erstellt: {{ticket_title}}',
        intro: 'Danke, {{requester_name}}! Wir haben Ihre Anfrage erhalten und melden uns so schnell wie möglich.',
    },
    {
        type: 'ticket_assigned',
        label: 'Ticket-Zuweisung',
        subject: '[{{ticket_number}}] Ticket zugewiesen: {{ticket_title}}',
        intro: 'Hallo {{assignee_name}}, dir wurde ein neues Ticket zugewiesen. Bitte bearbeite es zeitnah.',
    },
    {
        type: 'comment_added',
        label: 'Neuer Kommentar',
        subject: '[{{ticket_number}}] Neue Antwort: {{ticket_title}}',
        intro: 'Das Support-Team hat auf Ihr Ticket {{ticket_number}} geantwortet.',
    },
    {
        type: 'status_changed',
        label: 'Statusänderung',
        subject: '[{{ticket_number}}] Status: {{new_status}} – {{ticket_title}}',
        intro: 'Der Status Ihres Tickets {{ticket_number}} hat sich geändert.',
    },
    {
        type: 'escalation',
        label: 'Eskalation',
        subject: '⚠️ Eskalation: {{ticket_number}} – {{ticket_title}}',
        intro: 'Ein Ticket wartet seit {{age_hours}} Stunden auf Bearbeitung und wurde noch nicht bearbeitet.',
    },
    {
        type: 'satisfaction',
        label: 'Zufriedenheits-Feedback',
        subject: 'War Ihr Problem gelöst? – Ticket {{ticket_number}}',
        intro: 'Ihr Ticket {{ticket_number}} – {{ticket_title}} – wurde soeben geschlossen. War die Lösung hilfreich?',
    },
];

class EmailTemplate {
    static migrate() {
        const db = getDatabase();
        db.exec(`
            CREATE TABLE IF NOT EXISTS email_templates (
                type TEXT PRIMARY KEY,
                label TEXT NOT NULL,
                subject TEXT NOT NULL,
                intro TEXT NOT NULL,
                updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
            );
        `);
        const stmt = db.prepare(
            'INSERT OR IGNORE INTO email_templates (type, label, subject, intro) VALUES (?, ?, ?, ?)'
        );
        for (const d of DEFAULTS) {
            stmt.run(d.type, d.label, d.subject, d.intro);
        }
    }

    static getAll() {
        const db = getDatabase();
        return db.prepare('SELECT * FROM email_templates ORDER BY type').all();
    }

    static getByType(type) {
        const db = getDatabase();
        return db.prepare('SELECT * FROM email_templates WHERE type = ?').get(type);
    }

    static update(type, { subject, intro }) {
        const db = getDatabase();
        db.prepare(
            'UPDATE email_templates SET subject = ?, intro = ?, updated_at = CURRENT_TIMESTAMP WHERE type = ?'
        ).run(subject, intro, type);
        return db.prepare('SELECT * FROM email_templates WHERE type = ?').get(type);
    }

    static reset(type) {
        const d = DEFAULTS.find(x => x.type === type);
        if (!d) return null;
        const db = getDatabase();
        db.prepare(
            'UPDATE email_templates SET subject = ?, intro = ?, updated_at = CURRENT_TIMESTAMP WHERE type = ?'
        ).run(d.subject, d.intro, type);
        return db.prepare('SELECT * FROM email_templates WHERE type = ?').get(type);
    }
}

module.exports = { EmailTemplate, EMAIL_TEMPLATE_DEFAULTS: DEFAULTS };
