-- Migration: Create template version history table
-- Date: 2024-01-15
-- Description: Create table to store version history of notification templates

CREATE TABLE `notification_template_versions` (
  `id` varchar(36) NOT NULL DEFAULT (UUID()),
  `template_id` varchar(36) NOT NULL,
  `version` varchar(20) NOT NULL,
  `name` varchar(100) NOT NULL,
  `description` text,
  `subject` varchar(255) DEFAULT NULL,
  `preheader` varchar(255) DEFAULT NULL,
  `email_content` text,
  `push_title` varchar(100) DEFAULT NULL,
  `push_body` varchar(300) DEFAULT NULL,
  `push_icon` varchar(500) DEFAULT NULL,
  `push_url` text,
  `sms_content` text,
  `category` varchar(50) DEFAULT NULL,
  `channels` json DEFAULT NULL,
  `status` varchar(20) DEFAULT 'Draft',
  `tags` text,
  `is_personal` tinyint(1) DEFAULT '0',
  `from_name` varchar(100) DEFAULT NULL,
  `reply_to` varchar(255) DEFAULT NULL,
  `track_opens` tinyint(1) DEFAULT '1',
  `variables` json DEFAULT NULL,
  `is_active` tinyint(1) DEFAULT '1',
  `change_description` text,
  `is_current` tinyint(1) DEFAULT '0',
  `created_by` varchar(36) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_template_versions_template_id` (`template_id`),
  KEY `idx_template_versions_version` (`version`),
  KEY `idx_template_versions_created_at` (`created_at`),
  CONSTRAINT `fk_template_versions_template_id` FOREIGN KEY (`template_id`) REFERENCES `notification_templates` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Create index for better performance
CREATE INDEX `idx_template_versions_template_version` ON `notification_template_versions` (`template_id`, `version`);
CREATE INDEX `idx_template_versions_is_current` ON `notification_template_versions` (`is_current`); 