-- Override-System Tabellen
-- Copyright seit 2024 Webshop System

-- Module Override Tabelle
CREATE TABLE IF NOT EXISTS `ws_module_override` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `override_key` varchar(255) NOT NULL,
    `override_type` enum('class','template','controller') NOT NULL,
    `original_path` varchar(500) NOT NULL,
    `override_path` varchar(500) NOT NULL,
    `module_name` varchar(100) NOT NULL,
    `active` tinyint(1) NOT NULL DEFAULT 1,
    `priority` int(11) NOT NULL DEFAULT 50,
    `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 `override_key_module` (`override_key`, `module_name`),
    KEY `idx_override_type` (`override_type`),
    KEY `idx_module_name` (`module_name`),
    KEY `idx_active` (`active`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Override-Historie Tabelle
CREATE TABLE IF NOT EXISTS `ws_override_history` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `override_id` int(11) NOT NULL,
    `action` enum('created','updated','deactivated','reactivated') NOT NULL,
    `old_path` varchar(500) NULL,
    `new_path` varchar(500) NULL,
    `user_id` int(11) NULL,
    `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `idx_override_id` (`override_id`),
    KEY `idx_action` (`action`),
    KEY `idx_created_at` (`created_at`),
    FOREIGN KEY (`override_id`) REFERENCES `ws_module_override` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Override-Kompatibilität Tabelle
CREATE TABLE IF NOT EXISTS `ws_override_compatibility` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `override_id` int(11) NOT NULL,
    `webshop_version` varchar(20) NOT NULL,
    `prestashop_version` varchar(20) NOT NULL,
    `compatible` tinyint(1) NOT NULL DEFAULT 1,
    `notes` text NULL,
    `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `idx_override_id` (`override_id`),
    KEY `idx_webshop_version` (`webshop_version`),
    KEY `idx_prestashop_version` (`prestashop_version`),
    FOREIGN KEY (`override_id`) REFERENCES `ws_module_override` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Override-Performance Tabelle
CREATE TABLE IF NOT EXISTS `ws_override_performance` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `override_id` int(11) NOT NULL,
    `load_time` decimal(10,4) NOT NULL,
    `memory_usage` int(11) NOT NULL,
    `execution_count` int(11) NOT NULL DEFAULT 1,
    `last_executed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `idx_override_id` (`override_id`),
    KEY `idx_load_time` (`load_time`),
    KEY `idx_last_executed` (`last_executed`),
    FOREIGN KEY (`override_id`) REFERENCES `ws_module_override` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Override-Statistiken Tabelle
CREATE TABLE IF NOT EXISTS `ws_override_statistics` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `date` date NOT NULL,
    `override_type` enum('class','template','controller') NOT NULL,
    `total_overrides` int(11) NOT NULL DEFAULT 0,
    `active_overrides` int(11) NOT NULL DEFAULT 0,
    `module_count` int(11) NOT NULL DEFAULT 0,
    `avg_load_time` decimal(10,4) NULL,
    `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `date_type` (`date`, `override_type`),
    KEY `idx_date` (`date`),
    KEY `idx_override_type` (`override_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Override-Backup Tabelle
CREATE TABLE IF NOT EXISTS `ws_override_backup` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `override_id` int(11) NOT NULL,
    `backup_path` varchar(500) NOT NULL,
    `backup_size` int(11) NOT NULL,
    `backup_hash` varchar(64) NOT NULL,
    `backup_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `restored` tinyint(1) NOT NULL DEFAULT 0,
    PRIMARY KEY (`id`),
    KEY `idx_override_id` (`override_id`),
    KEY `idx_backup_date` (`backup_date`),
    FOREIGN KEY (`override_id`) REFERENCES `ws_module_override` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Override-Metadaten Tabelle
CREATE TABLE IF NOT EXISTS `ws_override_metadata` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `override_id` int(11) NOT NULL,
    `meta_key` varchar(100) NOT NULL,
    `meta_value` text NULL,
    `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 `override_meta_key` (`override_id`, `meta_key`),
    KEY `idx_meta_key` (`meta_key`),
    FOREIGN KEY (`override_id`) REFERENCES `ws_module_override` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Override-Dependencies Tabelle
CREATE TABLE IF NOT EXISTS `ws_override_dependencies` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `override_id` int(11) NOT NULL,
    `dependency_type` enum('class','template','controller','module') NOT NULL,
    `dependency_name` varchar(255) NOT NULL,
    `dependency_version` varchar(20) NULL,
    `required` tinyint(1) NOT NULL DEFAULT 1,
    `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `idx_override_id` (`override_id`),
    KEY `idx_dependency_type` (`dependency_type`),
    KEY `idx_dependency_name` (`dependency_name`),
    FOREIGN KEY (`override_id`) REFERENCES `ws_module_override` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Override-Validierung Tabelle
CREATE TABLE IF NOT EXISTS `ws_override_validation` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `override_id` int(11) NOT NULL,
    `validation_type` enum('syntax','security','performance','compatibility') NOT NULL,
    `status` enum('passed','failed','warning') NOT NULL,
    `message` text NULL,
    `validated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `idx_override_id` (`override_id`),
    KEY `idx_validation_type` (`validation_type`),
    KEY `idx_status` (`status`),
    FOREIGN KEY (`override_id`) REFERENCES `ws_module_override` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Override-Versionierung Tabelle
CREATE TABLE IF NOT EXISTS `ws_override_versions` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `override_id` int(11) NOT NULL,
    `version` varchar(20) NOT NULL,
    `version_path` varchar(500) NOT NULL,
    `version_hash` varchar(64) NOT NULL,
    `changelog` text NULL,
    `is_current` tinyint(1) NOT NULL DEFAULT 0,
    `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `idx_override_id` (`override_id`),
    KEY `idx_version` (`version`),
    KEY `idx_is_current` (`is_current`),
    FOREIGN KEY (`override_id`) REFERENCES `ws_module_override` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Override-Tags Tabelle
CREATE TABLE IF NOT EXISTS `ws_override_tags` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `override_id` int(11) NOT NULL,
    `tag_name` varchar(100) NOT NULL,
    `tag_value` varchar(255) NULL,
    `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `idx_override_id` (`override_id`),
    KEY `idx_tag_name` (`tag_name`),
    FOREIGN KEY (`override_id`) REFERENCES `ws_module_override` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Override-Logs Tabelle
CREATE TABLE IF NOT EXISTS `ws_override_logs` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `override_id` int(11) NULL,
    `log_level` enum('debug','info','warning','error','critical') NOT NULL,
    `log_message` text NOT NULL,
    `log_context` json NULL,
    `user_id` int(11) NULL,
    `ip_address` varchar(45) NULL,
    `user_agent` text NULL,
    `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `idx_override_id` (`override_id`),
    KEY `idx_log_level` (`log_level`),
    KEY `idx_created_at` (`created_at`),
    FOREIGN KEY (`override_id`) REFERENCES `ws_module_override` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Override-Settings Tabelle
CREATE TABLE IF NOT EXISTS `ws_override_settings` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `setting_key` varchar(100) NOT NULL,
    `setting_value` text NULL,
    `setting_type` enum('string','integer','boolean','json','array') NOT NULL DEFAULT 'string',
    `description` text NULL,
    `is_system` 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 `setting_key` (`setting_key`),
    KEY `idx_is_system` (`is_system`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Standard-Einstellungen einfügen
INSERT INTO `ws_override_settings` (`setting_key`, `setting_value`, `setting_type`, `description`, `is_system`) VALUES
('override.enabled', '1', 'boolean', 'Override-System aktiviert', 1),
('override.cache_enabled', '1', 'boolean', 'Override-Cache aktiviert', 1),
('override.auto_backup', '1', 'boolean', 'Automatische Backups für Overrides', 1),
('override.validation_enabled', '1', 'boolean', 'Override-Validierung aktiviert', 1),
('override.max_file_size', '1048576', 'integer', 'Maximale Override-Dateigröße in Bytes', 1),
('override.allowed_extensions', '["php","tpl","js","css"]', 'json', 'Erlaubte Dateierweiterungen für Overrides', 1),
('override.backup_retention_days', '30', 'integer', 'Backup-Aufbewahrungszeit in Tagen', 1),
('override.performance_monitoring', '1', 'boolean', 'Performance-Monitoring für Overrides', 1),
('override.security_scanning', '1', 'boolean', 'Sicherheits-Scanning für Overrides', 1),
('override.compatibility_check', '1', 'boolean', 'Kompatibilitätsprüfung für Overrides', 1);

-- Override-Index Tabelle für Performance
CREATE TABLE IF NOT EXISTS `ws_override_index` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `override_key` varchar(255) NOT NULL,
    `override_type` enum('class','template','controller') NOT NULL,
    `module_name` varchar(100) NOT NULL,
    `file_path` varchar(500) NOT NULL,
    `file_hash` varchar(64) NOT NULL,
    `file_size` int(11) NOT NULL,
    `last_modified` timestamp NOT NULL,
    `is_active` tinyint(1) NOT NULL DEFAULT 1,
    `indexed_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `override_key_type` (`override_key`, `override_type`),
    KEY `idx_module_name` (`module_name`),
    KEY `idx_file_hash` (`file_hash`),
    KEY `idx_is_active` (`is_active`),
    KEY `idx_indexed_at` (`indexed_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Override-Queue Tabelle für asynchrone Verarbeitung
CREATE TABLE IF NOT EXISTS `ws_override_queue` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `queue_type` enum('backup','validation','indexing','cleanup') NOT NULL,
    `override_id` int(11) NULL,
    `queue_data` json NULL,
    `priority` int(11) NOT NULL DEFAULT 5,
    `status` enum('pending','processing','completed','failed') NOT NULL DEFAULT 'pending',
    `attempts` int(11) NOT NULL DEFAULT 0,
    `max_attempts` int(11) NOT NULL DEFAULT 3,
    `error_message` text NULL,
    `scheduled_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `started_at` timestamp NULL,
    `completed_at` timestamp NULL,
    PRIMARY KEY (`id`),
    KEY `idx_queue_type` (`queue_type`),
    KEY `idx_status` (`status`),
    KEY `idx_priority` (`priority`),
    KEY `idx_scheduled_at` (`scheduled_at`),
    FOREIGN KEY (`override_id`) REFERENCES `ws_module_override` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 