import psutil

def check_ports(config):
    """
    Checks if required ports are available.
    Returns: list of (bool, str) - (Success, Message)
    """
    required_ports = config.get("required_ports", [])
    if not required_ports:
        return [(True, "No required ports specified")]

    results = []
    # Get all active connections
    connections = psutil.net_connections()
    
    in_use = {}
    for conn in connections:
        if conn.laddr.port in required_ports and conn.status == 'LISTEN':
            # Try to find the process name
            try:
                process = psutil.Process(conn.pid)
                name = process.name()
            except (psutil.NoSuchProcess, psutil.AccessDenied):
                name = "Unknown"
            in_use[conn.laddr.port] = name

    for port in required_ports:
        if port in in_use:
            results.append((False, f"Port {port} already in use (Process: {in_use[port]})"))
        else:
            results.append((True, f"Port {port} is available"))
            
    return results
