CREATE TABLE IF NOT EXISTS schema_version (
    version INTEGER PRIMARY KEY
);

CREATE TABLE IF NOT EXISTS device_models (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL UNIQUE,
    manufacturer TEXT NOT NULL DEFAULT '',
    is_rack_mountable BOOLEAN NOT NULL DEFAULT TRUE,
    height_units INTEGER,
    front_image TEXT DEFAULT '',
    back_image TEXT DEFAULT '',
    is_patch_panel BOOLEAN NOT NULL DEFAULT FALSE,
    is_wall_socket BOOLEAN NOT NULL DEFAULT FALSE,
    is_power_strip BOOLEAN NOT NULL DEFAULT FALSE,
    created_at DATETIME NOT NULL DEFAULT (datetime('now')),
    updated_at DATETIME NOT NULL DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS device_model_ports (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    device_model_id INTEGER NOT NULL REFERENCES device_models(id) ON DELETE CASCADE,
    name TEXT NOT NULL,
    side TEXT NOT NULL CHECK(side IN ('front', 'back')),
    position INTEGER NOT NULL DEFAULT 0
);

CREATE TABLE IF NOT EXISTS racks (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL UNIQUE,
    rack_type TEXT NOT NULL CHECK(rack_type IN ('network', 'server')),
    depth TEXT NOT NULL CHECK(depth IN ('shallow', 'deep')),
    height_units INTEGER NOT NULL,
    comment TEXT DEFAULT '',
    created_at DATETIME NOT NULL DEFAULT (datetime('now')),
    updated_at DATETIME NOT NULL DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS devices (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    device_model_id INTEGER NOT NULL REFERENCES device_models(id),
    name TEXT NOT NULL UNIQUE,
    usage_description TEXT DEFAULT '',
    comment TEXT DEFAULT '',
    location TEXT DEFAULT '',
    rack_id INTEGER REFERENCES racks(id) ON DELETE SET NULL,
    rack_unit_start INTEGER,
    rack_side TEXT CHECK(rack_side IN ('front', 'back')),
    created_at DATETIME NOT NULL DEFAULT (datetime('now')),
    updated_at DATETIME NOT NULL DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS device_ports (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    device_id INTEGER NOT NULL REFERENCES devices(id) ON DELETE CASCADE,
    name TEXT NOT NULL,
    side TEXT NOT NULL CHECK(side IN ('front', 'back')),
    position INTEGER NOT NULL DEFAULT 0
);

CREATE TABLE IF NOT EXISTS connection_types (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL UNIQUE
);

CREATE TABLE IF NOT EXISTS connections (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    connection_type_id INTEGER NOT NULL REFERENCES connection_types(id),
    label_1 TEXT,
    label_2 TEXT,
    color TEXT NOT NULL DEFAULT '#808080',
    port_id_1 INTEGER REFERENCES device_ports(id) ON DELETE SET NULL,
    port_id_2 INTEGER REFERENCES device_ports(id) ON DELETE SET NULL,
    created_at DATETIME NOT NULL DEFAULT (datetime('now')),
    updated_at DATETIME NOT NULL DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS idx_device_ports_device_id ON device_ports(device_id);
CREATE INDEX IF NOT EXISTS idx_device_model_ports_model_id ON device_model_ports(device_model_id);
CREATE INDEX IF NOT EXISTS idx_devices_rack_id ON devices(rack_id);
CREATE INDEX IF NOT EXISTS idx_connections_port_1 ON connections(port_id_1);
CREATE INDEX IF NOT EXISTS idx_connections_port_2 ON connections(port_id_2);
CREATE INDEX IF NOT EXISTS idx_devices_model_id ON devices(device_model_id);

INSERT OR IGNORE INTO connection_types (name) VALUES
    ('Ethernet'),
    ('FibreChannel'),
    ('SAS'),
    ('power'),
    ('video'),
    ('audio'),
    ('serial'),
    ('USB');

INSERT OR IGNORE INTO schema_version (version) VALUES (1);
