-- Migration: Update notification_templates table with additional fields
-- Description: Add missing fields required by the template creation form

-- Add new columns to notification_templates table
ALTER TABLE notification_templates 
ADD COLUMN description TEXT,
ADD COLUMN preheader VARCHAR(255),
ADD COLUMN push_icon VARCHAR(500),
ADD COLUMN push_url TEXT,
ADD COLUMN sms_content TEXT,
ADD COLUMN category VARCHAR(50),
ADD COLUMN channels JSON,
ADD COLUMN status VARCHAR(20) DEFAULT 'Draft',
ADD COLUMN version VARCHAR(20) DEFAULT '1.0',
ADD COLUMN tags TEXT,
ADD COLUMN is_personal BOOLEAN DEFAULT false,
ADD COLUMN from_name VARCHAR(100),
ADD COLUMN reply_to VARCHAR(255),
ADD COLUMN track_opens BOOLEAN DEFAULT true,
ADD COLUMN created_by VARCHAR(36),
ADD COLUMN updated_by VARCHAR(36);

-- Add check constraint for status
ALTER TABLE notification_templates 
ADD CONSTRAINT chk_template_status 
CHECK (status IN ('Draft', 'Active', 'Archived'));

-- Update existing templates to have default values for new fields
UPDATE notification_templates 
SET 
    status = 'Active',
    version = '1.0',
    is_personal = false,
    track_opens = true,
    channels = JSON_ARRAY('email')
WHERE status IS NULL;

-- Create index for better performance on commonly queried fields
CREATE INDEX idx_notification_templates_status ON notification_templates(status);
CREATE INDEX idx_notification_templates_category ON notification_templates(category);
CREATE INDEX idx_notification_templates_created_by ON notification_templates(created_by); 