CREATE TABLE locations (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    comment TEXT
) ENGINE=InnoDB;

CREATE TABLE buildings (
    id INT AUTO_INCREMENT PRIMARY KEY,
    location_id INT NOT NULL,
    name VARCHAR(255) NOT NULL,
    comment TEXT,
    FOREIGN KEY (location_id) REFERENCES locations(id)
        ON DELETE CASCADE
) ENGINE=InnoDB;

CREATE TABLE floors (
    id INT AUTO_INCREMENT PRIMARY KEY,
    building_id INT NOT NULL,
    name VARCHAR(255) NOT NULL,
    level INT,
    svg_path VARCHAR(255),
    comment TEXT,
    FOREIGN KEY (building_id) REFERENCES buildings(id)
        ON DELETE CASCADE
) ENGINE=InnoDB;

CREATE TABLE rooms (
    id INT AUTO_INCREMENT PRIMARY KEY,
    floor_id INT NOT NULL,
    name VARCHAR(255) NOT NULL,
    number VARCHAR(50),
    x INT,
    y INT,
    width INT,
    height INT,
    comment TEXT,
    FOREIGN KEY (floor_id) REFERENCES floors(id)
        ON DELETE CASCADE
) ENGINE=InnoDB;

CREATE TABLE network_outlets (
    id INT AUTO_INCREMENT PRIMARY KEY,
    room_id INT NOT NULL,
    name VARCHAR(255) NOT NULL,
    x INT,
    y INT,
    comment TEXT,
    FOREIGN KEY (room_id) REFERENCES rooms(id)
        ON DELETE CASCADE
) ENGINE=InnoDB;

CREATE TABLE network_outlet_ports (
    id INT AUTO_INCREMENT PRIMARY KEY,
    outlet_id INT NOT NULL,
    name VARCHAR(50) NOT NULL,
    port_type_id INT,
    FOREIGN KEY (outlet_id) REFERENCES network_outlets(id)
        ON DELETE CASCADE
) ENGINE=InnoDB;

CREATE TABLE racks (
    id INT AUTO_INCREMENT PRIMARY KEY,
    floor_id INT NOT NULL,
    name VARCHAR(255) NOT NULL,
    height_he INT NOT NULL,
    comment TEXT,
    FOREIGN KEY (floor_id) REFERENCES floors(id)
        ON DELETE CASCADE
) ENGINE=InnoDB;

CREATE TABLE device_types (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    category ENUM('switch','server','patchpanel','other') NOT NULL,
    image_path VARCHAR(255),
    image_type ENUM('svg','bitmap') NOT NULL,
    comment TEXT
) ENGINE=InnoDB;

CREATE TABLE device_type_ports (
    id INT AUTO_INCREMENT PRIMARY KEY,
    device_type_id INT NOT NULL,
    name VARCHAR(50) NOT NULL,
    port_type_id INT,
    x INT NOT NULL,
    y INT NOT NULL,
    metadata JSON,
    FOREIGN KEY (device_type_id) REFERENCES device_types(id)
        ON DELETE CASCADE
) ENGINE=InnoDB;

CREATE TABLE devices (
    id INT AUTO_INCREMENT PRIMARY KEY,
    device_type_id INT NOT NULL,
    rack_id INT,
    name VARCHAR(255) NOT NULL,
    rack_position_he INT,
    rack_height_he INT,
    serial_number VARCHAR(255),
    comment TEXT,
    FOREIGN KEY (device_type_id) REFERENCES device_types(id),
    FOREIGN KEY (rack_id) REFERENCES racks(id)
        ON DELETE SET NULL
) ENGINE=InnoDB;

CREATE TABLE device_ports (
    id INT AUTO_INCREMENT PRIMARY KEY,
    device_id INT NOT NULL,
    name VARCHAR(50) NOT NULL,
    port_type_id INT,
    status ENUM('active','disabled') DEFAULT 'active',
    mode VARCHAR(50),
    vlan_config JSON,
    FOREIGN KEY (device_id) REFERENCES devices(id)
        ON DELETE CASCADE
) ENGINE=InnoDB;

CREATE TABLE port_types (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    medium ENUM('copper','fiber','coax','other') NOT NULL,
    comment TEXT
) ENGINE=InnoDB;

CREATE TABLE connection_types (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    medium ENUM('copper','fiber','coax','other') NOT NULL,
    duplex ENUM('half','full','custom') DEFAULT 'custom',
    max_speed VARCHAR(50),
    color VARCHAR(20),
    line_style ENUM('solid','dashed','dotted') DEFAULT 'solid',
    comment TEXT
) ENGINE=InnoDB;

CREATE TABLE modules (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    module_type VARCHAR(100),
    comment TEXT
) ENGINE=InnoDB;

CREATE TABLE module_ports (
    id INT AUTO_INCREMENT PRIMARY KEY,
    module_id INT NOT NULL,
    name VARCHAR(50) NOT NULL,
    port_type_id INT,
    FOREIGN KEY (module_id) REFERENCES modules(id)
        ON DELETE CASCADE
) ENGINE=InnoDB;

CREATE TABLE device_port_modules (
    id INT AUTO_INCREMENT PRIMARY KEY,
    device_port_id INT NOT NULL,
    module_id INT NOT NULL,
    FOREIGN KEY (device_port_id) REFERENCES device_ports(id)
        ON DELETE CASCADE,
    FOREIGN KEY (module_id) REFERENCES modules(id)
        ON DELETE CASCADE
) ENGINE=InnoDB;

CREATE TABLE vlans (
    id INT AUTO_INCREMENT PRIMARY KEY,
    vlan_id INT NOT NULL,
    name VARCHAR(255),
    comment TEXT
) ENGINE=InnoDB;

CREATE TABLE connections (
    id INT AUTO_INCREMENT PRIMARY KEY,
    connection_type_id INT NOT NULL,

    port_a_type ENUM('device','module','outlet') NOT NULL,
    port_a_id INT NOT NULL,

    port_b_type ENUM('device','module','outlet') NOT NULL,
    port_b_id INT NOT NULL,

    vlan_config JSON,
    mode VARCHAR(50),
    comment TEXT,

    FOREIGN KEY (connection_type_id) REFERENCES connection_types(id)
) ENGINE=InnoDB;

