import sqlite3

# Function to create the 'validation_severity' database
def create_validation_severity_database():
    connection = sqlite3.connect('validation_severity.db')
    cursor = connection.cursor()

    # Create the 'validation_severity' table with columns for details and the 'severity' column
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS validation_severity (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            protocol TEXT,
            source TEXT,
            destination TEXT,
            length INTEGER,
            vulnerability_info TEXT,
            severity TEXT
        )
    ''')
    connection.commit()
    connection.close()

# Function to calculate and set severity in 'validation_severity' based on 'length'
def calculate_and_set_severity():
    # Connect to the 'vulnerabilities' database
    connection = sqlite3.connect('vulnerabilities.db')
    cursor = connection.cursor()

    # Connect to the 'validation_severity' database
    severity_connection = sqlite3.connect('validation_severity.db')
    severity_cursor = severity_connection.cursor()

    # Query to retrieve vulnerabilities from 'vulnerabilities' database
    cursor.execute("SELECT protocol, source, destination, length, vulnerability_info FROM vulnerabilities")
    vulnerabilities = cursor.fetchall()

    for vulnerability in vulnerabilities:
        protocol, source, destination, length, vulnerability_info = vulnerability

        # Calculate severity based on 'length'
        if length < 500:
            severity = 'Informational'
        elif 500 <= length < 1000:
            severity = 'Low'
        elif 1000 <= length < 2000:
            severity = 'Medium'
        elif 2000 <= length < 3000:
            severity = 'High'
        else:
            severity = 'Critical'

        # Insert data into 'validation_severity' database with the calculated severity
        severity_cursor.execute("INSERT INTO validation_severity (protocol, source, destination, length, vulnerability_info, severity) VALUES (?, ?, ?, ?, ?, ?)",
                               (protocol, source, destination, length, vulnerability_info, severity))

    severity_connection.commit()
    connection.close()
    severity_connection.close()

# Create the 'validation_severity' database and calculate severity
create_validation_severity_database()
calculate_and_set_severity()
