from elasticsearch import Elasticsearch

# Initialize the Elasticsearch client
es = Elasticsearch(
    [
        "https://elastic:8oKIqy312EBsAPzWT64NUzji@scic-elasticsearch.es.us-central1.gcp.cloud.es.io:443"
    ]
)

# Define the index name, organization, and username
index_name = "risk_insurance_education"
organization_name = (
    "Risk & Insurance Education Alliance"  # Replace with actual organization name
)
username = "Alice"  # Replace with actual username

# Create the search query to match both organization and username
query = {
    "query": {
        "bool": {
            "must": [
                {"match": {"organization_name": organization_name}},
                {"match": {"username": username}},
            ]
        }
    }
}

# Search the index for matching records
response = es.search(index=index_name, body=query)

# Print the matching records
for hit in response["hits"]["hits"]:
    print(hit["_source"])
    if hit["_source"]["assigned_tokens"] and not hit["_source"]["overall_tokens"]:
        activate_conv = True
    elif hit["_source"]["assigned_tokens"] and hit["_source"]["overall_tokens"]:

        assigned_tokens = hit["_source"]["assigned_tokens"]
        overall_tokens = hit["_source"]["overall_tokens"]

        # Calculate the difference
        sub_res = assigned_tokens - overall_tokens

        # Check conditions
        if sub_res < 0 or sub_res <= 100:
            activate_conv = False
        else:
            activate_conv = True
