-- Infrastructure Inventory Database Schema
-- PostgreSQL schema for tracking infrastructure components

-- Sites table
CREATE TABLE IF NOT EXISTS sites (
    id VARCHAR(50) PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    location VARCHAR(255),
    timezone VARCHAR(50) DEFAULT 'UTC',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Nodes table (Proxmox, Kubernetes, etc.)
CREATE TABLE IF NOT EXISTS nodes (
    id VARCHAR(50) PRIMARY KEY,
    site_id VARCHAR(50) REFERENCES sites(id) ON DELETE CASCADE,
    name VARCHAR(255) NOT NULL,
    type VARCHAR(50) NOT NULL, -- 'proxmox', 'kubernetes', etc.
    ip_address INET,
    status VARCHAR(20) DEFAULT 'unknown', -- 'online', 'offline', 'maintenance'
    cpu_cores INTEGER,
    memory_gb INTEGER,
    storage_gb INTEGER,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Virtual machines table
CREATE TABLE IF NOT EXISTS vms (
    id VARCHAR(50) PRIMARY KEY,
    node_id VARCHAR(50) REFERENCES nodes(id) ON DELETE CASCADE,
    site_id VARCHAR(50) REFERENCES sites(id) ON DELETE CASCADE,
    name VARCHAR(255) NOT NULL,
    vmid INTEGER,
    status VARCHAR(20) DEFAULT 'unknown',
    cpu_cores INTEGER,
    memory_gb INTEGER,
    disk_gb INTEGER,
    ip_address INET,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Network devices table
CREATE TABLE IF NOT EXISTS network_devices (
    id VARCHAR(50) PRIMARY KEY,
    site_id VARCHAR(50) REFERENCES sites(id) ON DELETE CASCADE,
    name VARCHAR(255) NOT NULL,
    type VARCHAR(50) NOT NULL, -- 'switch', 'router', 'access_point', 'gateway'
    model VARCHAR(255),
    ip_address INET,
    mac_address MACADDR,
    status VARCHAR(20) DEFAULT 'unknown',
    firmware_version VARCHAR(50),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Storage pools table
CREATE TABLE IF NOT EXISTS storage_pools (
    id VARCHAR(50) PRIMARY KEY,
    site_id VARCHAR(50) REFERENCES sites(id) ON DELETE CASCADE,
    name VARCHAR(255) NOT NULL,
    type VARCHAR(50) NOT NULL, -- 'local', 'ceph', 'nfs', etc.
    total_gb BIGINT,
    used_gb BIGINT,
    available_gb BIGINT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Networks/VLANs table
CREATE TABLE IF NOT EXISTS networks (
    id VARCHAR(50) PRIMARY KEY,
    site_id VARCHAR(50) REFERENCES sites(id) ON DELETE CASCADE,
    name VARCHAR(255) NOT NULL,
    vlan_id INTEGER,
    subnet CIDR,
    gateway INET,
    description TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Inventory history table (for change tracking)
CREATE TABLE IF NOT EXISTS inventory_history (
    id SERIAL PRIMARY KEY,
    table_name VARCHAR(50) NOT NULL,
    record_id VARCHAR(50) NOT NULL,
    action VARCHAR(20) NOT NULL, -- 'create', 'update', 'delete'
    changes JSONB,
    changed_by VARCHAR(255),
    changed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Indexes
CREATE INDEX IF NOT EXISTS idx_nodes_site_id ON nodes(site_id);
CREATE INDEX IF NOT EXISTS idx_vms_node_id ON vms(node_id);
CREATE INDEX IF NOT EXISTS idx_vms_site_id ON vms(site_id);
CREATE INDEX IF NOT EXISTS idx_network_devices_site_id ON network_devices(site_id);
CREATE INDEX IF NOT EXISTS idx_storage_pools_site_id ON storage_pools(site_id);
CREATE INDEX IF NOT EXISTS idx_networks_site_id ON networks(site_id);
CREATE INDEX IF NOT EXISTS idx_inventory_history_record ON inventory_history(table_name, record_id);

-- Function to update updated_at timestamp
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$ language 'plpgsql';

-- Triggers for updated_at
CREATE TRIGGER update_sites_updated_at BEFORE UPDATE ON sites
    FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TRIGGER update_nodes_updated_at BEFORE UPDATE ON nodes
    FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TRIGGER update_vms_updated_at BEFORE UPDATE ON vms
    FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TRIGGER update_network_devices_updated_at BEFORE UPDATE ON network_devices
    FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TRIGGER update_storage_pools_updated_at BEFORE UPDATE ON storage_pools
    FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TRIGGER update_networks_updated_at BEFORE UPDATE ON networks
    FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

