-- portspoof_concentrator schema

CREATE TABLE IF NOT EXISTS nodes (
    id          INT UNSIGNED    NOT NULL AUTO_INCREMENT,
    name        VARCHAR(100)    NOT NULL,
    api_url     VARCHAR(255)    NOT NULL COMMENT 'e.g. https://192.168.1.10:8080',
    username    VARCHAR(100)    NOT NULL DEFAULT 'admin',
    password    VARCHAR(255)    NOT NULL,
    verify_ssl  TINYINT(1)      NOT NULL DEFAULT 0,
    enabled     TINYINT(1)      NOT NULL DEFAULT 1,
    last_fetched_at DATETIME    NULL,
    last_event_at   DATETIME(6) NULL COMMENT 'timestamp of the newest ingested event from this node',
    created_at  DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    UNIQUE KEY uq_api_url (api_url)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS connections (
    id          BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    node_id     INT UNSIGNED    NOT NULL,
    occurred_at DATETIME(6)     NOT NULL COMMENT 'timestamp from portspoof_py',
    src_ip      VARCHAR(45)     NOT NULL,
    src_port    SMALLINT UNSIGNED NOT NULL,
    dst_port    SMALLINT UNSIGNED NOT NULL,
    banner_hex  TEXT            NULL,
    banner_len  INT UNSIGNED    NOT NULL DEFAULT 0,
    created_at  DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    KEY idx_node_occurred (node_id, occurred_at),
    KEY idx_src_ip (src_ip),
    KEY idx_dst_port (dst_port),
    CONSTRAINT fk_conn_node FOREIGN KEY (node_id) REFERENCES nodes (id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS settings (
    key_name    VARCHAR(64)  NOT NULL,
    value       TEXT         NOT NULL,
    PRIMARY KEY (key_name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT IGNORE INTO settings (key_name, value) VALUES ('retention_days', '7');
