-- Graph session metrics (direct MariaDB ingest; no ETL).
-- Run as root on existing servers: mysql -u root -p androidcast_crashes < sql/migrations/006_graph_sessions.sql

CREATE TABLE IF NOT EXISTS graph_sessions (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    session_id VARCHAR(96) NOT NULL,
    company_id INT UNSIGNED NOT NULL DEFAULT 1,
    user_id INT UNSIGNED NULL,
    device_id VARCHAR(128) NULL,
    app_version VARCHAR(64) NULL,
    sdk_int INT NULL,
    started_at_ms BIGINT NOT NULL,
    ended_at_ms BIGINT NOT NULL,
    duration_s INT NOT NULL,
    completed TINYINT(1) NOT NULL DEFAULT 1,
    direction ENUM('sender','receiver') NOT NULL DEFAULT 'sender',
    transport VARCHAR(24) NOT NULL DEFAULT 'wifi',
    avg_kbps INT NOT NULL DEFAULT 0,
    recv_kbps INT NOT NULL DEFAULT 0,
    packet_loss_pct DECIMAL(6,3) NOT NULL DEFAULT 0.0,
    ntp_source VARCHAR(32) NOT NULL DEFAULT 'device',
    ntp_correction_s INT NULL,
    install_source VARCHAR(32) NOT NULL DEFAULT 'direct',
    meta_json LONGTEXT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY uq_graph_session_id (session_id),
    KEY idx_graph_company_time (company_id, started_at_ms),
    KEY idx_graph_device (device_id),
    KEY idx_graph_app_version (app_version)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
