CREATE TABLE users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    username TEXT NOT NULL UNIQUE,
    password TEXT NOT NULL
);
CREATE TABLE users_meta (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    name TEXT,
    email TEXT,
    timezone TEXT,
    avatar TEXT,
    bio TEXT,
    FOREIGN KEY (user_id) REFERENCES users(id)
);

CREATE TABLE users_rights (
    user_id INTEGER,
    right_id INTEGER,
    PRIMARY KEY (user_id, right_id),
    FOREIGN KEY (user_id) REFERENCES users(id),
    FOREIGN KEY (right_id) REFERENCES rights(id)
);
CREATE TABLE rights (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL UNIQUE
);

CREATE TABLE login_attempts (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    ip_address TEXT NOT NULL,
    username TEXT NOT NULL,
    attempted_at TEXT DEFAULT (DATETIME('now')),
);

CREATE TABLE ip_whitelist (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    ip_address TEXT NOT NULL UNIQUE,
    is_network BOOLEAN DEFAULT 0 CHECK(is_network IN (0, 1)),
    description TEXT,
    created_at TEXT DEFAULT (DATETIME('now')),
    created_by TEXT,
);

INSERT OR IGNORE INTO ip_whitelist (ip_address, is_network, description, created_by) VALUES
('127.0.0.1', 0, 'localhost IPv4', 'system'),
('::1', 0, 'localhost IPv6', 'system'),
('10.0.0.0/8', 1, 'Private network', 'system'),
('172.16.0.0/12', 1, 'Private network', 'system'),
('192.168.0.0/16', 1, 'Private network', 'system');

CREATE TABLE ip_blacklist (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    ip_address TEXT NOT NULL UNIQUE,
    is_network BOOLEAN DEFAULT 0 CHECK(IS_NETWORK IN (0,1)),
    reason TEXT,
    expiry_time TEXT,
    created_at TEXT DEFAULT (DATETIME('now')),
    created_by TEXT,
);

INSERT OR IGNORE INTO ip_blacklist (ip_address, is_network, reason, created_by) VALUES
('0.0.0.0/8', 1, 'Reserved address space - RFC 1122', 'system'),
('100.64.0.0/10', 1, 'Carrier-grade NAT space - RFC 6598', 'system'),
('192.0.2.0/24', 1, 'TEST-NET-1 Documentation space - RFC 5737', 'system'),
('198.51.100.0/24', 1, 'TEST-NET-2 Documentation space - RFC 5737', 'system'),
('203.0.113.0/24', 1, 'TEST-NET-3 Documentation space - RFC 5737', 'system');

CREATE TABLE logs (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGET NOT NULL,
    time TEXT DEFAULT (DATETIME('now')),
    scope TEXT NOT NULL,
    message TEXT NOT NULL,
    FOREIGN KEY (user_id) REFERENCES users(id)
);

CREATE TABLE platforms (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL UNIQUE,
    jitsi_url TEXT NOT NULL,
    jilo_database TEXT NOT NULL
);

CREATE TABLE hosts (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    address TEXT NOT NULL,
    port INTEGER NOT NULL,
    platform_id INTEGER NOT NULL,
    name TEXT,
    FOREIGN KEY(platform_id) REFERENCES platforms(id)
);

CREATE TABLE jilo_agents (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    platform_id INTEGER NOT NULL,
    agent_type_id INTEGER NOT NULL,
    url TEXT NOT NULL,
    secret_key TEXT,
    check_period INTEGER DEFAULT 0,
    FOREIGN KEY (platform_id) REFERENCES platforms(id),
    FOREIGN KEY (agent_type_id) REFERENCES jilo_agent_types(id)
);
CREATE TABLE jilo_agent_types (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    description TEXT,
    endpoint TEXT
);
CREATE TABLE jilo_agent_checks (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    agent_id INTEGER,
    timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
    status_code INTEGER,
    response_time_ms INTEGER,
    response_content TEXT,
    FOREIGN KEY (agent_id) REFERENCES jilo_agents(id)
);