CREATE TABLE IF NOT EXISTS internet_connections_connections (
    id SERIAL PRIMARY KEY,
    parent_id INTEGER REFERENCES internet_connections_connections(id) ON DELETE SET NULL,
    name VARCHAR(255) NOT NULL,
    connection_type VARCHAR(50) NOT NULL DEFAULT 'fiber',
    provider VARCHAR(255),
    circuit_number VARCHAR(255),
    customer_id INTEGER,
    address VARCHAR(500),
    speed_mbps INTEGER,
    upload_mbps INTEGER,
    download_mbps INTEGER,
    technology VARCHAR(100),
    status VARCHAR(50) NOT NULL DEFAULT 'active',
    monthly_cost NUMERIC(12,2) DEFAULT 0,
    sales_price NUMERIC(12,2) DEFAULT 0,
    monitoring_url TEXT,
    contract_start DATE,
    contract_end DATE,
    notes TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP
);

CREATE TABLE IF NOT EXISTS internet_connections_ip_ranges (
    id SERIAL PRIMARY KEY,
    connection_id INTEGER REFERENCES internet_connections_connections(id) ON DELETE CASCADE,
    name VARCHAR(255) NOT NULL,
    cidr VARCHAR(32) NOT NULL,
    description TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP
);

CREATE TABLE IF NOT EXISTS internet_connections_ip_addresses (
    id SERIAL PRIMARY KEY,
    range_id INTEGER REFERENCES internet_connections_ip_ranges(id) ON DELETE CASCADE,
    ip_address VARCHAR(64) NOT NULL,
    status VARCHAR(50) NOT NULL DEFAULT 'available',
    assigned_to VARCHAR(255),
    assigned_type VARCHAR(50),
    comment TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP
);

CREATE TABLE IF NOT EXISTS internet_connections_history (
    id SERIAL PRIMARY KEY,
    connection_id INTEGER REFERENCES internet_connections_connections(id) ON DELETE CASCADE,
    event_type VARCHAR(100) NOT NULL,
    summary TEXT NOT NULL,
    details JSONB DEFAULT '{}'::jsonb,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    created_by INTEGER
);

CREATE TABLE IF NOT EXISTS internet_connections_pricing (
    id SERIAL PRIMARY KEY,
    connection_id INTEGER REFERENCES internet_connections_connections(id) ON DELETE CASCADE,
    effective_from DATE NOT NULL,
    purchase_price NUMERIC(12,2) DEFAULT 0,
    sales_price NUMERIC(12,2) DEFAULT 0,
    notes TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    created_by INTEGER
);

CREATE INDEX IF NOT EXISTS idx_internet_connections_parent_id ON internet_connections_connections(parent_id);
CREATE INDEX IF NOT EXISTS idx_internet_connections_status ON internet_connections_connections(status);
CREATE INDEX IF NOT EXISTS idx_internet_connections_ip_ranges_connection_id ON internet_connections_ip_ranges(connection_id);
CREATE INDEX IF NOT EXISTS idx_internet_connections_ip_addresses_range_id ON internet_connections_ip_addresses(range_id);
CREATE INDEX IF NOT EXISTS idx_internet_connections_history_connection_id ON internet_connections_history(connection_id);
