-- Migration: 081_better_email_templates
-- Created: 2026-02-01

-- Create email_templates table
CREATE TABLE IF NOT EXISTS email_templates (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    slug VARCHAR(255) NOT NULL,
    subject VARCHAR(255) NOT NULL,
    body TEXT NOT NULL,
    category VARCHAR(50) DEFAULT 'general',
    description TEXT,
    variables JSONB DEFAULT '{}',
    is_system BOOLEAN DEFAULT FALSE,
    customer_id INTEGER REFERENCES customers(id) ON DELETE CASCADE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    UNIQUE(slug, customer_id) -- A slug must be unique per customer (or global if customer_id is null)
);

-- Index for faster lookups
CREATE INDEX idx_email_templates_slug ON email_templates(slug);
CREATE INDEX idx_email_templates_customer ON email_templates(customer_id);

-- Migrate existing settings if they exist
DO $$
DECLARE
    internal_subj TEXT;
    internal_body TEXT;
    external_subj TEXT;
    external_body TEXT;
    nc_subj TEXT;
    nc_body TEXT;
BEGIN
    -- Intern besked
    SELECT value INTO internal_subj FROM settings WHERE key = 'email_template_internal_subject';
    SELECT value INTO internal_body FROM settings WHERE key = 'email_template_internal_body';
    
    IF internal_subj IS NOT NULL AND internal_body IS NOT NULL THEN
        INSERT INTO email_templates (name, slug, subject, body, category, description, variables, is_system)
        VALUES (
            'Intern besked',
            'internal_message',
            internal_subj,
            internal_body,
            'internal',
            'Standard skabelon til interne beskeder',
            '{"message": "Selve beskeden"}'::jsonb,
            TRUE
        ) ON CONFLICT DO NOTHING;
    END IF;

    -- Ekstern besked
    SELECT value INTO external_subj FROM settings WHERE key = 'email_template_external_subject';
    SELECT value INTO external_body FROM settings WHERE key = 'email_template_external_body';
    
    IF external_subj IS NOT NULL AND external_body IS NOT NULL THEN
        INSERT INTO email_templates (name, slug, subject, body, category, description, variables, is_system)
        VALUES (
            'Generel kundebesked',
            'external_message',
            external_subj,
            external_body,
            'general',
            'Standard skabelon til emails sendt til kunder',
            '{"customer_name": "Navnet på kunden", "message": "Selve beskeden"}'::jsonb,
            TRUE
        ) ON CONFLICT DO NOTHING;
    END IF;

    -- Nextcloud velkomst
    SELECT value INTO nc_subj FROM settings WHERE key = 'nextcloud_user_welcome_subject';
    SELECT value INTO nc_body FROM settings WHERE key = 'nextcloud_user_welcome_body';
    
    IF nc_subj IS NOT NULL AND nc_body IS NOT NULL THEN
        INSERT INTO email_templates (name, slug, subject, body, category, description, variables, is_system)
        VALUES (
            'Nextcloud Velkomst',
            'nextcloud_welcome',
            nc_subj,
            nc_body,
            'nextcloud',
            'Sendes til nye brugere når deres konto oprettes',
            '{"name": "Modtagerens navn", "url": "URL til Nextcloud instans", "username": "Brugernavn", "password": "Det autogenererede password"}'::jsonb,
            TRUE
        ) ON CONFLICT DO NOTHING;
    END IF;

END $$;

-- Cleanup old settings (optional, keeping them for backward compatibility if needed, but usually safe to remove if code is updated simultaneously)
-- DELETE FROM settings WHERE key IN (
--    'email_template_internal_subject', 'email_template_internal_body',
--    'email_template_external_subject', 'email_template_external_body',
--    'nextcloud_user_welcome_subject', 'nextcloud_user_welcome_body'
-- );
