-- Super Admin Tabellen für PinRepair Multi-Tenant System

-- Super Admins Tabelle
CREATE TABLE IF NOT EXISTS `super_admins` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `username` varchar(50) NOT NULL UNIQUE,
    `email` varchar(100) NOT NULL UNIQUE,
    `password` varchar(255) NOT NULL,
    `is_active` tinyint(1) DEFAULT 1,
    `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
    `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Locations Tabelle (für Multi-Tenant)
CREATE TABLE IF NOT EXISTS `locations` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(100) NOT NULL,
    `subdomain` varchar(50) NOT NULL UNIQUE,
    `database_name` varchar(100) NOT NULL UNIQUE,
    `description` text,
    `contact_email` varchar(100),
    `contact_phone` varchar(20),
    `is_active` tinyint(1) DEFAULT 1,
    `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
    `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    INDEX `idx_subdomain` (`subdomain`),
    INDEX `idx_database_name` (`database_name`),
    INDEX `idx_is_active` (`is_active`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Location Admins Tabelle
CREATE TABLE IF NOT EXISTS `location_admins` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `location_id` int(11) NOT NULL,
    `username` varchar(50) NOT NULL,
    `email` varchar(100) NOT NULL,
    `password` varchar(255) NOT NULL,
    `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
    `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    FOREIGN KEY (`location_id`) REFERENCES `locations`(`id`) ON DELETE CASCADE,
    INDEX `idx_location_id` (`location_id`),
    INDEX `idx_username` (`username`),
    INDEX `idx_email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Standard Super-Admin Benutzer erstellen
-- Passwort: admin123 (sollte nach der Installation geändert werden)
INSERT IGNORE INTO `super_admins` (`username`, `email`, `password`, `is_active`) 
VALUES ('admin', 'admin@pinrepair.com', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 1);

-- Beispiel-Location (kann gelöscht werden)
INSERT IGNORE INTO `locations` (`name`, `subdomain`, `database_name`, `description`, `contact_email`, `is_active`) 
VALUES ('Hauptstandort', 'main', 'pinrepair_main', 'Hauptstandort für Flipper-Verwaltung', 'admin@pinrepair.com', 1);
