-- Adatbázis létrehozása a queue rendszerhez
CREATE DATABASE IF NOT EXISTS queue_demo CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

USE queue_demo;

-- Events tábla - koncert események
CREATE TABLE IF NOT EXISTS events (
    id VARCHAR(100) PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    description TEXT,
    max_concurrent_users INT DEFAULT 100,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- Queue tábla - várakozási sor
CREATE TABLE IF NOT EXISTS queue_entries (
    id INT AUTO_INCREMENT PRIMARY KEY,
    event_id VARCHAR(100) NOT NULL,
    socket_id VARCHAR(100) NOT NULL,
    position INT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_event_position (event_id, position),
    INDEX idx_socket_event (socket_id, event_id),
    FOREIGN KEY (event_id) REFERENCES events(id) ON DELETE CASCADE
);

-- Active sessions tábla - aktív felhasználók akik vásárolhatnak
CREATE TABLE IF NOT EXISTS active_sessions (
    id INT AUTO_INCREMENT PRIMARY KEY,
    event_id VARCHAR(100) NOT NULL,
    socket_id VARCHAR(100) NOT NULL,
    jwt_token TEXT NOT NULL,
    expires_at TIMESTAMP NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_event_socket (event_id, socket_id),
    INDEX idx_expires (expires_at),
    FOREIGN KEY (event_id) REFERENCES events(id) ON DELETE CASCADE
);

-- Tickets tábla - jegyek
CREATE TABLE IF NOT EXISTS tickets (
    id INT AUTO_INCREMENT PRIMARY KEY,
    event_id VARCHAR(100) NOT NULL,
    type VARCHAR(100) NOT NULL, -- 'VIP', 'Normal', 'Student'
    price DECIMAL(10,2) NOT NULL,
    total_quantity INT NOT NULL,
    sold_quantity INT DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_event (event_id),
    FOREIGN KEY (event_id) REFERENCES events(id) ON DELETE CASCADE
);

-- Orders tábla - rendelések
CREATE TABLE IF NOT EXISTS orders (
    id INT AUTO_INCREMENT PRIMARY KEY,
    event_id VARCHAR(100) NOT NULL,
    socket_id VARCHAR(100) NOT NULL,
    ticket_type VARCHAR(100) NOT NULL,
    quantity INT NOT NULL,
    total_price DECIMAL(10,2) NOT NULL,
    status ENUM('pending', 'completed', 'cancelled') DEFAULT 'pending',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_event (event_id),
    INDEX idx_socket (socket_id),
    FOREIGN KEY (event_id) REFERENCES events(id) ON DELETE CASCADE
);

-- Példa adatok beszúrása
INSERT IGNORE INTO events (id, name, description, max_concurrent_users) VALUES
('pamkutya', 'Pam Kutya Koncert', 'Legendás underground koncert a városban', 100),
('rock-fest', 'Rock Fesztivál 2025', 'Három napos rock fesztivál', 150);

INSERT IGNORE INTO tickets (event_id, type, price, total_quantity) VALUES
('pamkutya', 'Normal', 5000.00, 500),
('pamkutya', 'VIP', 8000.00, 50),
('pamkutya', 'Student', 3000.00, 100),
('rock-fest', 'Normal', 12000.00, 1000),
('rock-fest', 'VIP', 25000.00, 200);
