-- Module-System Schema
-- Erstellt die Tabellen für PrestaShop-kompatible Module

-- Module-Tabelle
CREATE TABLE IF NOT EXISTS `ws_module` (
    `id_module` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(64) NOT NULL,
    `display_name` varchar(255) DEFAULT NULL,
    `description` text,
    `version` varchar(32) DEFAULT '1.0.0',
    `author` varchar(255) DEFAULT 'Unknown',
    `tab` varchar(64) DEFAULT 'administration',
    `need_instance` tinyint(1) DEFAULT 0,
    `bootstrap` tinyint(1) DEFAULT 0,
    `active` tinyint(1) DEFAULT 1,
    `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
    `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id_module`),
    UNIQUE KEY `name` (`name`),
    KEY `active` (`active`),
    KEY `tab` (`tab`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Hook-Module-Tabelle (Verbindung zwischen Hooks und Modulen)
CREATE TABLE IF NOT EXISTS `ws_hook_module` (
    `id_hook_module` int(11) NOT NULL AUTO_INCREMENT,
    `hook_name` varchar(64) NOT NULL,
    `module_name` varchar(64) NOT NULL,
    `position` int(11) DEFAULT 0,
    `active` tinyint(1) DEFAULT 1,
    `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
    `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id_hook_module`),
    UNIQUE KEY `hook_module` (`hook_name`, `module_name`),
    KEY `hook_name` (`hook_name`),
    KEY `module_name` (`module_name`),
    KEY `active` (`active`),
    KEY `position` (`position`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Module-Konfiguration-Tabelle
CREATE TABLE IF NOT EXISTS `ws_module_config` (
    `id_module_config` int(11) NOT NULL AUTO_INCREMENT,
    `module_name` varchar(64) NOT NULL,
    `config_key` varchar(255) NOT NULL,
    `config_value` text,
    `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
    `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id_module_config`),
    UNIQUE KEY `module_config` (`module_name`, `config_key`),
    KEY `module_name` (`module_name`),
    KEY `config_key` (`config_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Module-Logs-Tabelle
CREATE TABLE IF NOT EXISTS `ws_module_log` (
    `id_module_log` int(11) NOT NULL AUTO_INCREMENT,
    `module_name` varchar(64) NOT NULL,
    `action` varchar(64) NOT NULL,
    `message` text,
    `level` enum('info', 'warning', 'error') DEFAULT 'info',
    `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id_module_log`),
    KEY `module_name` (`module_name`),
    KEY `action` (`action`),
    KEY `level` (`level`),
    KEY `created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Module-Dependencies-Tabelle
CREATE TABLE IF NOT EXISTS `ws_module_dependency` (
    `id_module_dependency` int(11) NOT NULL AUTO_INCREMENT,
    `module_name` varchar(64) NOT NULL,
    `dependency_name` varchar(64) NOT NULL,
    `dependency_version` varchar(32) DEFAULT NULL,
    `required` tinyint(1) DEFAULT 1,
    `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id_module_dependency`),
    KEY `module_name` (`module_name`),
    KEY `dependency_name` (`dependency_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Module-Override-Tabelle
CREATE TABLE IF NOT EXISTS `ws_module_override` (
    `id_module_override` int(11) NOT NULL AUTO_INCREMENT,
    `module_name` varchar(64) NOT NULL,
    `override_type` enum('class', 'template', 'controller') NOT NULL,
    `original_path` varchar(255) NOT NULL,
    `override_path` varchar(255) NOT NULL,
    `active` tinyint(1) DEFAULT 1,
    `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
    `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id_module_override`),
    KEY `module_name` (`module_name`),
    KEY `override_type` (`override_type`),
    KEY `active` (`active`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Standard-Hooks einfügen
INSERT IGNORE INTO `ws_hook_module` (`hook_name`, `module_name`, `position`) VALUES
-- Display Hooks
('displayHeader', 'system', 0),
('displayTop', 'system', 0),
('displayNav', 'system', 0),
('displayFooter', 'system', 0),
('displayHome', 'system', 0),

-- Product Hooks
('displayProductAdditionalInfo', 'system', 0),
('displayProductListReviews', 'system', 0),
('displayProductButtons', 'system', 0),

-- Cart Hooks
('displayShoppingCart', 'system', 0),
('actionCartUpdateQuantityBefore', 'system', 0),
('actionCartUpdateQuantityAfter', 'system', 0),

-- Order Hooks
('displayOrderConfirmation', 'system', 0),
('actionValidateOrder', 'system', 0),

-- Customer Hooks
('displayCustomerAccount', 'system', 0),
('actionCustomerAccountAdd', 'system', 0),

-- Admin Hooks
('displayAdminOrder', 'system', 0),
('displayAdminProducts', 'system', 0),

-- Payment Hooks
('displayPayment', 'system', 0),
('actionPaymentConfirmation', 'system', 0),

-- Search Hooks
('displaySearch', 'system', 0),
('actionSearch', 'system', 0),

-- Newsletter Hooks
('displayNewsletterRegistration', 'system', 0),
('actionNewsletterRegistrationAfter', 'system', 0),

-- Security Hooks
('actionAuthentication', 'system', 0),
('actionCustomerLogoutAfter', 'system', 0);

-- System-Modul eintragen
INSERT IGNORE INTO `ws_module` (`name`, `display_name`, `description`, `version`, `author`, `tab`, `need_instance`, `bootstrap`, `active`) VALUES
('system', 'System', 'Kern-System-Modul', '1.0.0', 'Webshop System', 'administration', 0, 0, 1); 