-- ============================================================
--  MCLogger - MariaDB Schema
--  Author: SimolZimol
--  Minecraft 1.20-1.21 | Paper + Velocity
-- ============================================================

CREATE DATABASE IF NOT EXISTS mclogger CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE mclogger;

-- -------------------------------------------------------
-- Tabelle: servers
-- Registriert alle bekannten Server-Instanzen
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS servers (
    id          INT AUTO_INCREMENT PRIMARY KEY,
    server_name VARCHAR(100) NOT NULL,
    server_type ENUM('paper', 'velocity') NOT NULL DEFAULT 'paper',
    ip_address  VARCHAR(45),
    mc_version  VARCHAR(20),
    first_seen  TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3),
    last_seen   TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
    UNIQUE KEY uq_server_name (server_name)
) ENGINE=InnoDB;

-- -------------------------------------------------------
-- Tabelle: players
-- Bekannte Spieler mit Basisdaten
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS players (
    uuid                  VARCHAR(36)  PRIMARY KEY,
    username              VARCHAR(16)  NOT NULL,
    display_name          VARCHAR(64),
    ip_address            VARCHAR(45),
    locale                VARCHAR(20),
    first_seen            TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3),
    last_seen             TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
    total_playtime_sec    BIGINT       DEFAULT 0,
    is_op                 TINYINT(1)   DEFAULT 0,
    INDEX idx_username (username)
) ENGINE=InnoDB;

-- -------------------------------------------------------
-- Tabelle: player_sessions
-- Login-/Logout-Sitzungen
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS player_sessions (
    id              BIGINT AUTO_INCREMENT PRIMARY KEY,
    player_uuid     VARCHAR(36) NOT NULL,
    player_name     VARCHAR(16) NOT NULL,
    server_name     VARCHAR(100),
    login_time      TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3),
    logout_time     TIMESTAMP(3),
    duration_sec    INT,
    ip_address      VARCHAR(45),
    client_version  VARCHAR(20),
    INDEX idx_ps_uuid   (player_uuid),
    INDEX idx_ps_server (server_name),
    INDEX idx_ps_login  (login_time)
) ENGINE=InnoDB;

-- -------------------------------------------------------
-- Tabelle: player_chat
-- Alle Chat-Nachrichten
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS player_chat (
    id          BIGINT AUTO_INCREMENT PRIMARY KEY,
    timestamp   TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3),
    player_uuid VARCHAR(36),
    player_name VARCHAR(16),
    server_name VARCHAR(100),
    world       VARCHAR(100),
    message     TEXT NOT NULL,
    channel     VARCHAR(50) DEFAULT 'global',
    INDEX idx_chat_uuid      (player_uuid),
    INDEX idx_chat_timestamp (timestamp),
    INDEX idx_chat_server    (server_name),
    FULLTEXT INDEX ft_message (message)
) ENGINE=InnoDB;

-- -------------------------------------------------------
-- Tabelle: player_commands
-- Alle ausgeführten Befehle
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS player_commands (
    id          BIGINT AUTO_INCREMENT PRIMARY KEY,
    timestamp   TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3),
    player_uuid VARCHAR(36),
    player_name VARCHAR(16),
    server_name VARCHAR(100),
    world       VARCHAR(100),
    command     TEXT NOT NULL,
    x           DOUBLE,
    y           DOUBLE,
    z           DOUBLE,
    INDEX idx_cmd_uuid      (player_uuid),
    INDEX idx_cmd_timestamp (timestamp),
    INDEX idx_cmd_server    (server_name)
) ENGINE=InnoDB;

-- -------------------------------------------------------
-- Tabelle: block_events
-- Block-Break / Block-Place und weitere Block-Aktionen
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS block_events (
    id           BIGINT AUTO_INCREMENT PRIMARY KEY,
    timestamp    TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3),
    event_type   ENUM('break','place','ignite','burn','explode','fade','grow','dispense') NOT NULL,
    player_uuid  VARCHAR(36),
    player_name  VARCHAR(16),
    server_name  VARCHAR(100),
    world        VARCHAR(100) NOT NULL,
    x            INT NOT NULL,
    y            INT NOT NULL,
    z            INT NOT NULL,
    block_type   VARCHAR(100) NOT NULL,
    block_data   VARCHAR(255),
    tool         VARCHAR(100),
    is_silk      TINYINT(1) DEFAULT 0,
    INDEX idx_be_player    (player_uuid),
    INDEX idx_be_timestamp (timestamp),
    INDEX idx_be_world     (world),
    INDEX idx_be_type      (event_type),
    INDEX idx_be_server    (server_name)
) ENGINE=InnoDB;

-- -------------------------------------------------------
-- Tabelle: player_deaths
-- Tode mit vollem Kontext
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS player_deaths (
    id             BIGINT AUTO_INCREMENT PRIMARY KEY,
    timestamp      TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3),
    player_uuid    VARCHAR(36) NOT NULL,
    player_name    VARCHAR(16) NOT NULL,
    server_name    VARCHAR(100),
    world          VARCHAR(100),
    x              DOUBLE,
    y              DOUBLE,
    z              DOUBLE,
    death_message  TEXT,
    cause          VARCHAR(100),
    killer_uuid    VARCHAR(36),
    killer_name    VARCHAR(100),
    killer_type    VARCHAR(100),
    exp_level      INT,
    items_lost     JSON,
    INDEX idx_deaths_uuid      (player_uuid),
    INDEX idx_deaths_timestamp (timestamp)
) ENGINE=InnoDB;

-- -------------------------------------------------------
-- Tabelle: entity_events
-- Entity-Spawns, -Tode, Schaden usw.
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS entity_events (
    id              BIGINT AUTO_INCREMENT PRIMARY KEY,
    timestamp       TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3),
    event_type      ENUM('spawn','death','damage','tame','breed','transform','explode') NOT NULL,
    server_name     VARCHAR(100),
    world           VARCHAR(100),
    x               DOUBLE,
    y               DOUBLE,
    z               DOUBLE,
    entity_type     VARCHAR(100) NOT NULL,
    entity_uuid     VARCHAR(36),
    entity_name     VARCHAR(100),
    player_uuid     VARCHAR(36),
    player_name     VARCHAR(16),
    cause           VARCHAR(100),
    damage          DOUBLE,
    details         JSON,
    INDEX idx_ee_timestamp  (timestamp),
    INDEX idx_ee_type       (event_type),
    INDEX idx_ee_entity     (entity_type),
    INDEX idx_ee_player     (player_uuid),
    INDEX idx_ee_server     (server_name)
) ENGINE=InnoDB;

-- -------------------------------------------------------
-- Tabelle: player_teleports
-- Alle Teleportationen (Commands, Death, etc.)
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS player_teleports (
    id           BIGINT AUTO_INCREMENT PRIMARY KEY,
    timestamp    TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3),
    player_uuid  VARCHAR(36) NOT NULL,
    player_name  VARCHAR(16) NOT NULL,
    server_name  VARCHAR(100),
    from_world   VARCHAR(100),
    from_x       DOUBLE,
    from_y       DOUBLE,
    from_z       DOUBLE,
    to_world     VARCHAR(100),
    to_x         DOUBLE,
    to_y         DOUBLE,
    to_z         DOUBLE,
    cause        VARCHAR(100),
    INDEX idx_tp_uuid      (player_uuid),
    INDEX idx_tp_timestamp (timestamp)
) ENGINE=InnoDB;

-- -------------------------------------------------------
-- Tabelle: inventory_events
-- Item-Picks, Drops, Inventory-Klicks
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS inventory_events (
    id           BIGINT AUTO_INCREMENT PRIMARY KEY,
    timestamp    TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3),
    event_type   ENUM('pickup','drop','click','craft','enchant','anvil','trade') NOT NULL,
    player_uuid  VARCHAR(36) NOT NULL,
    player_name  VARCHAR(16) NOT NULL,
    server_name  VARCHAR(100),
    world        VARCHAR(100),
    x            DOUBLE,
    y            DOUBLE,
    z            DOUBLE,
    item_type    VARCHAR(100),
    item_amount  INT,
    item_meta    JSON,
    slot         INT,
    inventory_type VARCHAR(100),
    INDEX idx_inv_uuid      (player_uuid),
    INDEX idx_inv_timestamp (timestamp),
    INDEX idx_inv_type      (event_type)
) ENGINE=InnoDB;

-- -------------------------------------------------------
-- Tabelle: player_stats
-- Gamemode-Wechsel, Level-Änderungen, OP-Status usw.
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS player_stats (
    id           BIGINT AUTO_INCREMENT PRIMARY KEY,
    timestamp    TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3),
    event_type   VARCHAR(100) NOT NULL,
    player_uuid  VARCHAR(36) NOT NULL,
    player_name  VARCHAR(16) NOT NULL,
    server_name  VARCHAR(100),
    old_value    VARCHAR(255),
    new_value    VARCHAR(255),
    details      JSON,
    INDEX idx_pst_uuid      (player_uuid),
    INDEX idx_pst_timestamp (timestamp),
    INDEX idx_pst_type      (event_type)
) ENGINE=InnoDB;

-- -------------------------------------------------------
-- Tabelle: world_events
-- Wetter, Zeit, Explosionen, Portale usw.
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS world_events (
    id           BIGINT AUTO_INCREMENT PRIMARY KEY,
    timestamp    TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3),
    event_type   VARCHAR(100) NOT NULL,
    server_name  VARCHAR(100),
    world        VARCHAR(100),
    x            DOUBLE,
    y            DOUBLE,
    z            DOUBLE,
    details      JSON,
    INDEX idx_we_timestamp  (timestamp),
    INDEX idx_we_world      (world),
    INDEX idx_we_type       (event_type)
) ENGINE=InnoDB;

-- -------------------------------------------------------
-- Tabelle: server_events
-- Server-Start, -Stop, Plugin-Events, Konsolen-Commands
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS server_events (
    id           BIGINT AUTO_INCREMENT PRIMARY KEY,
    timestamp    TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3),
    event_type   VARCHAR(100) NOT NULL,
    server_name  VARCHAR(100),
    message      TEXT,
    details      JSON,
    INDEX idx_se_timestamp  (timestamp),
    INDEX idx_se_type       (event_type),
    INDEX idx_se_server     (server_name)
) ENGINE=InnoDB;

-- -------------------------------------------------------
-- Tabelle: proxy_events
-- Velocity: Login, Disconnect, Server-Wechsel
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS proxy_events (
    id           BIGINT AUTO_INCREMENT PRIMARY KEY,
    timestamp    TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3),
    event_type   ENUM('login','disconnect','server_switch','command','chat','kick','proxy_start','proxy_stop') NOT NULL,
    player_uuid  VARCHAR(36),
    player_name  VARCHAR(16),
    proxy_name   VARCHAR(100),
    from_server  VARCHAR(100),
    to_server    VARCHAR(100),
    ip_address   VARCHAR(45),
    details      JSON,
    INDEX idx_pe_uuid      (player_uuid),
    INDEX idx_pe_timestamp (timestamp),
    INDEX idx_pe_type      (event_type),
    INDEX idx_pe_proxy     (proxy_name)
) ENGINE=InnoDB;

-- -------------------------------------------------------
-- Tabelle: sign_edits
-- SignChangeEvents
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS sign_edits (
    id           BIGINT AUTO_INCREMENT PRIMARY KEY,
    timestamp    TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3),
    player_uuid  VARCHAR(36),
    player_name  VARCHAR(16),
    server_name  VARCHAR(100),
    world        VARCHAR(100),
    x            INT,
    y            INT,
    z            INT,
    line1        VARCHAR(255),
    line2        VARCHAR(255),
    line3        VARCHAR(255),
    line4        VARCHAR(255),
    INDEX idx_sign_uuid      (player_uuid),
    INDEX idx_sign_timestamp (timestamp)
) ENGINE=InnoDB;

-- -------------------------------------------------------
-- Tabelle: plugin_events
-- LuckPerms Berechtigungsänderungen und andere Plugin-Events
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS plugin_events (
    id           BIGINT AUTO_INCREMENT PRIMARY KEY,
    timestamp    TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3),
    event_type   VARCHAR(100) NOT NULL,
    plugin_name  VARCHAR(100),
    server_name  VARCHAR(100),
    player_uuid  VARCHAR(36),
    player_name  VARCHAR(16),
    actor_uuid   VARCHAR(36),
    actor_name   VARCHAR(64),
    target_type  VARCHAR(50),
    target_id    VARCHAR(100),
    action       VARCHAR(255),
    details      JSON,
    INDEX idx_plev_uuid      (player_uuid),
    INDEX idx_plev_timestamp (timestamp),
    INDEX idx_plev_type      (event_type),
    INDEX idx_plev_plugin    (plugin_name),
    INDEX idx_plev_actor     (actor_uuid)
) ENGINE=InnoDB;

-- -------------------------------------------------------
-- View: v_recent_activity - Letzte 24h kompakt
-- -------------------------------------------------------
CREATE OR REPLACE VIEW v_recent_activity AS
SELECT 'chat'     AS source, timestamp, player_name, server_name, message  AS detail FROM player_chat     WHERE timestamp >= NOW() - INTERVAL 24 HOUR
UNION ALL
SELECT 'command'  AS source, timestamp, player_name, server_name, command  AS detail FROM player_commands WHERE timestamp >= NOW() - INTERVAL 24 HOUR
UNION ALL
SELECT 'block'    AS source, timestamp, player_name, server_name, CONCAT(event_type,' ',block_type,' at ',world,' ',x,',',y,',',z) AS detail FROM block_events WHERE timestamp >= NOW() - INTERVAL 24 HOUR
UNION ALL
SELECT 'death'    AS source, timestamp, player_name, server_name, death_message AS detail FROM player_deaths WHERE timestamp >= NOW() - INTERVAL 24 HOUR
ORDER BY timestamp DESC;
