-- Schema-Dump für `d044ae9e` (erstellt am 2025-12-08 00:14:08 UTC)
-- Hinweis: Es werden nur CREATE-Anweisungen ausgegeben, bestehende Tabellen bleiben unangetastet.

CREATE DATABASE IF NOT EXISTS `d044ae9e` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
USE `d044ae9e`;
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- Tabelle: customers
CREATE TABLE IF NOT EXISTS `customers` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(150) NOT NULL,
  `slug` varchar(150) NOT NULL,
  `plan` varchar(50) NOT NULL DEFAULT 'free',
  `status` varchar(20) NOT NULL DEFAULT 'active',
  `logo_url` varchar(255) DEFAULT NULL,
  `theme_json` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`theme_json`)),
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `slug` (`slug`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- Tabelle: customer_tokens
CREATE TABLE IF NOT EXISTS `customer_tokens` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `customer_id` int(10) unsigned NOT NULL,
  `label` varchar(120) NOT NULL,
  `token_hash` char(64) NOT NULL,
  `scopes` varchar(255) DEFAULT NULL,
  `is_active` tinyint(1) NOT NULL DEFAULT 1,
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `last_used_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_token_hash` (`token_hash`),
  KEY `customer_id` (`customer_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- Tabelle: customer_users
CREATE TABLE IF NOT EXISTS `customer_users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `customer_id` int(10) unsigned NOT NULL,
  `email` varchar(190) NOT NULL,
  `password_hash` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
  `role` varchar(30) NOT NULL DEFAULT 'editor',
  `is_active` tinyint(1) NOT NULL DEFAULT 1,
  `list_sort` varchar(32) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_customer_email` (`customer_id`,`email`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- Tabelle: emailtemplate_assets
CREATE TABLE IF NOT EXISTS `emailtemplate_assets` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `customer_id` int(10) unsigned NOT NULL,
  `name` varchar(255) NOT NULL,
  `type` varchar(50) NOT NULL,
  `mime_type` varchar(100) NOT NULL,
  `size_bytes` int(10) unsigned NOT NULL DEFAULT 0,
  `public_url` varchar(1000) DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_ast_customer` (`customer_id`),
  KEY `idx_ast_updated` (`updated_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- Tabelle: emailtemplate_blocks
CREATE TABLE IF NOT EXISTS `emailtemplate_blocks` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `customer_id` int(10) unsigned NOT NULL,
  `section_id` int(10) unsigned DEFAULT NULL,
  `name` varchar(255) NOT NULL,
  `category` varchar(100) NOT NULL DEFAULT 'Default',
  `json_content` mediumtext DEFAULT NULL,
  `html` mediumtext DEFAULT NULL,
  `editor_type` varchar(32) DEFAULT NULL,
  `craft_json` mediumtext DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_blk_customer` (`customer_id`),
  KEY `idx_blk_section` (`section_id`),
  KEY `idx_blk_name` (`name`),
  CONSTRAINT `fk_blocks_section` FOREIGN KEY (`section_id`) REFERENCES `emailtemplate_sections` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- Tabelle: emailtemplate_customers
CREATE TABLE IF NOT EXISTS `emailtemplate_customers` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(150) NOT NULL,
  `slug` varchar(150) NOT NULL,
  `plan` varchar(50) NOT NULL DEFAULT 'free',
  `status` varchar(20) NOT NULL DEFAULT 'active',
  `logo_url` varchar(255) DEFAULT NULL,
  `theme_json` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`theme_json`)),
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `slug` (`slug`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- Tabelle: emailtemplate_customer_settings
CREATE TABLE IF NOT EXISTS `emailtemplate_customer_settings` (
  `customer_id` int(10) unsigned NOT NULL,
  `bridge_url` varchar(500) DEFAULT NULL,
  `bridge_token` varchar(255) DEFAULT NULL,
  `sender_token` varchar(255) DEFAULT NULL,
  `external_api_token` varchar(255) DEFAULT NULL,
  `editor_default` varchar(32) DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`customer_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- Tabelle: emailtemplate_customer_tokens
CREATE TABLE IF NOT EXISTS `emailtemplate_customer_tokens` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `customer_id` int(10) unsigned NOT NULL,
  `label` varchar(120) NOT NULL,
  `token_hash` char(64) NOT NULL,
  `scopes` varchar(255) DEFAULT NULL,
  `is_active` tinyint(1) NOT NULL DEFAULT 1,
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `last_used_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_token_hash` (`token_hash`),
  KEY `customer_id` (`customer_id`),
  CONSTRAINT `emailtemplate_customer_tokens_ibfk_1` FOREIGN KEY (`customer_id`) REFERENCES `emailtemplate_customers` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- Tabelle: emailtemplate_customer_users
CREATE TABLE IF NOT EXISTS `emailtemplate_customer_users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `customer_id` int(10) unsigned NOT NULL,
  `email` varchar(190) NOT NULL,
  `password_hash` varchar(255) NOT NULL,
  `role` varchar(30) NOT NULL DEFAULT 'editor',
  `is_active` tinyint(1) NOT NULL DEFAULT 1,
  `list_sort` varchar(32) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_customer_email` (`customer_id`,`email`),
  CONSTRAINT `fk_customer_users_customer` FOREIGN KEY (`customer_id`) REFERENCES `emailtemplate_customers` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- Tabelle: emailtemplate_sections
CREATE TABLE IF NOT EXISTS `emailtemplate_sections` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `customer_id` int(10) unsigned NOT NULL,
  `template_id` int(10) unsigned DEFAULT NULL,
  `name` varchar(255) NOT NULL,
  `json_content` mediumtext DEFAULT NULL,
  `type` varchar(50) NOT NULL DEFAULT 'html',
  `z_index` int(11) NOT NULL DEFAULT 0,
  `html` mediumtext DEFAULT NULL,
  `editor_type` varchar(32) DEFAULT NULL,
  `craft_json` mediumtext DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_sec_customer` (`customer_id`),
  KEY `idx_sec_template` (`template_id`),
  KEY `idx_sec_sort` (`template_id`,`z_index`,`id`),
  CONSTRAINT `fk_sections_template` FOREIGN KEY (`template_id`) REFERENCES `emailtemplate_templates` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- Tabelle: emailtemplate_section_items
CREATE TABLE IF NOT EXISTS `emailtemplate_section_items` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `customer_id` int(10) unsigned NOT NULL,
  `section_id` int(10) unsigned NOT NULL,
  `sort` int(11) NOT NULL DEFAULT 0,
  `ref_type` enum('block') NOT NULL,
  `ref_id` int(10) unsigned NOT NULL,
  `overrides_json` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`overrides_json`)),
  `lock_to_version` int(11) DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_sitems_section_sort` (`section_id`,`sort`),
  KEY `idx_sitems_customer` (`customer_id`),
  KEY `idx_sitems_ref` (`ref_type`,`ref_id`),
  CONSTRAINT `fk_sitems_section` FOREIGN KEY (`section_id`) REFERENCES `emailtemplate_sections` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- Tabelle: emailtemplate_sender_identities
CREATE TABLE IF NOT EXISTS `emailtemplate_sender_identities` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `customer_id` int(10) unsigned NOT NULL,
  `label` varchar(255) NOT NULL,
  `from_name` varchar(255) DEFAULT NULL,
  `from_email` varchar(255) NOT NULL,
  `reply_to` varchar(255) DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_sender_customer` (`customer_id`),
  KEY `idx_sender_email` (`from_email`),
  CONSTRAINT `fk_sender_customer` FOREIGN KEY (`customer_id`) REFERENCES `emailtemplate_customers` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- Tabelle: emailtemplate_snippets
CREATE TABLE IF NOT EXISTS `emailtemplate_snippets` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `customer_id` int(10) unsigned NOT NULL,
  `name` varchar(255) NOT NULL,
  `category` varchar(100) NOT NULL DEFAULT '',
  `json_content` mediumtext DEFAULT NULL,
  `content` mediumtext DEFAULT NULL,
  `editor_type` varchar(32) DEFAULT NULL,
  `craft_json` mediumtext DEFAULT NULL,
  `block_id` int(10) unsigned DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_snp_customer` (`customer_id`),
  KEY `idx_snp_name` (`name`),
  KEY `idx_snp_block` (`block_id`),
  CONSTRAINT `fk_snippets_block` FOREIGN KEY (`block_id`) REFERENCES `emailtemplate_blocks` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- Tabelle: emailtemplate_templates
CREATE TABLE IF NOT EXISTS `emailtemplate_templates` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `customer_id` int(10) unsigned NOT NULL,
  `name` varchar(255) NOT NULL,
  `api_name` varchar(190) NOT NULL,
  `json_content` mediumtext DEFAULT NULL,
  `html` mediumtext DEFAULT NULL,
  `editor_type` varchar(32) DEFAULT NULL,
  `craft_json` mediumtext DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_tpl_customer` (`customer_id`),
  KEY `idx_tpl_updated` (`updated_at`),
  KEY `idx_tpl_name` (`name`),
  UNIQUE KEY `uidx_tpl_customer_api_name` (`customer_id`, `api_name`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- Tabelle: emailtemplate_template_items
CREATE TABLE IF NOT EXISTS `emailtemplate_template_items` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `customer_id` int(10) unsigned NOT NULL,
  `template_id` int(10) unsigned NOT NULL,
  `sort` int(11) NOT NULL DEFAULT 0,
  `ref_type` enum('section','block') NOT NULL,
  `ref_id` int(10) unsigned NOT NULL,
  `overrides_json` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`overrides_json`)),
  `lock_to_version` int(11) DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_titems_template_sort` (`template_id`,`sort`),
  KEY `idx_titems_customer` (`customer_id`),
  KEY `idx_titems_ref` (`ref_type`,`ref_id`),
  CONSTRAINT `fk_titems_template` FOREIGN KEY (`template_id`) REFERENCES `emailtemplate_templates` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- Tabelle: emailtemplate_template_usage
CREATE TABLE IF NOT EXISTS `emailtemplate_template_usage` (
  `template_id` int(10) unsigned NOT NULL,
  `customer_id` int(10) unsigned NOT NULL,
  `render_count` int(10) unsigned NOT NULL DEFAULT 0,
  `last_rendered_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`template_id`),
  KEY `idx_usage_customer` (`customer_id`),
  CONSTRAINT `fk_usage_template` FOREIGN KEY (`template_id`) REFERENCES `emailtemplate_templates` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- Tabelle: emailtemplate_content_sections
CREATE TABLE IF NOT EXISTS `emailtemplate_content_sections` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `customer_id` int(10) unsigned NOT NULL,
  `name` varchar(255) NOT NULL,
  `slug` varchar(190) NOT NULL,
  `position` int(11) NOT NULL DEFAULT 0,
  `is_template` tinyint(1) NOT NULL DEFAULT 0,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_sections_customer_slug` (`customer_id`,`slug`),
  UNIQUE KEY `uq_sections_customer_name` (`customer_id`,`name`),
  KEY `idx_sections_customer` (`customer_id`),
  KEY `idx_sections_sort` (`customer_id`,`position`,`id`),
  CONSTRAINT `fk_content_sections_customer` FOREIGN KEY (`customer_id`) REFERENCES `emailtemplate_customers` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- Tabelle: emailtemplate_content_items
CREATE TABLE IF NOT EXISTS `emailtemplate_content_items` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `customer_id` int(10) unsigned NOT NULL,
  `section_id` int(10) unsigned NOT NULL,
  `name` varchar(255) NOT NULL,
  `api_name` varchar(190) DEFAULT NULL,
  `category` varchar(100) DEFAULT NULL,
  `json_content` mediumtext DEFAULT NULL,
  `html` mediumtext DEFAULT NULL,
  `editor_type` varchar(32) DEFAULT NULL,
  `craft_json` mediumtext DEFAULT NULL,
  `settings_json` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`settings_json`)),
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_items_api` (`customer_id`,`api_name`),
  KEY `idx_items_customer` (`customer_id`),
  KEY `idx_items_section` (`section_id`),
  KEY `idx_items_name` (`name`),
  CONSTRAINT `fk_content_items_section` FOREIGN KEY (`section_id`) REFERENCES `emailtemplate_content_sections` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `fk_content_items_customer` FOREIGN KEY (`customer_id`) REFERENCES `emailtemplate_customers` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

SET FOREIGN_KEY_CHECKS = 1;
-- Ende des Schema-Dumps
