import os
import requests
import json
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

NOTION_API_KEY = os.getenv("NOTION_API_KEY")
NOTION_DB_ID = "2ec88f4285448014ab38ea664b4c2b81"  # ID from the user's link

if not NOTION_API_KEY:
    print("Error: NOTION_API_KEY not found in environment.")
    exit(1)

headers = {
    "Authorization": f"Bearer {NOTION_API_KEY}",
    "Notion-Version": "2022-06-28",
    "Content-Type": "application/json"
}

def get_vertical_data(vertical_name):
    url = f"https://api.notion.com/v1/databases/{NOTION_DB_ID}/query"
    payload = {
        "filter": {
            "property": "Vertical",
            "title": {
                "contains": vertical_name
            }
        }
    }
    
    response = requests.post(url, headers=headers, json=payload)
    
    if response.status_code != 200:
        print(f"Error fetching data for '{vertical_name}': {response.status_code} - {response.text}")
        return None

    results = response.json().get("results", [])
    if not results:
        print(f"No entry found for vertical '{vertical_name}'")
        return None
    
    # Assuming the first result is the correct one
    page = results[0]
    props = page["properties"]
    
    # Extract Pains
    pains_prop = props.get("Pains", {}).get("rich_text", [])
    pains = pains_prop[0]["plain_text"] if pains_prop else "N/A"
    
    # Extract Gains
    gains_prop = props.get("Gains", {}).get("rich_text", [])
    gains = gains_prop[0]["plain_text"] if gains_prop else "N/A"
    
    # Extract Ops Focus (Checkbox) if available
    # The property name might be "Ops. Focus: Secondary" based on user description
    # Let's check keys to be sure, but user mentioned "Ops. Focus: Secondary"
    # Actually, let's just dump the keys if needed, but for now try to guess
    ops_focus = "Unknown"
    if "Ops. Focus: Secondary" in props:
        ops_focus = props["Ops. Focus: Secondary"].get("checkbox", False)
    elif "Ops Focus" in props: # Fallback guess
         ops_focus = props["Ops Focus"].get("checkbox", False)

    # Extract Product Categories
    primary_product = "N/A"
    secondary_product = "N/A"
    
    # Assuming these are Select or Multi-select fields, or Relations.
    # User mentioned "Primary Product Category" and "Secondary Product Category".
    if "Primary Product Category" in props:
         pp_data = props["Primary Product Category"].get("select") or props["Primary Product Category"].get("multi_select")
         if pp_data:
             if isinstance(pp_data, list):
                 primary_product = ", ".join([item["name"] for item in pp_data])
             else:
                 primary_product = pp_data["name"]
    
    if "Secondary Product Category" in props:
         sp_data = props["Secondary Product Category"].get("select") or props["Secondary Product Category"].get("multi_select")
         if sp_data:
             if isinstance(sp_data, list):
                 secondary_product = ", ".join([item["name"] for item in sp_data])
             else:
                 secondary_product = sp_data["name"]

    return {
        "name": vertical_name,
        "pains": pains,
        "gains": gains,
        "ops_focus_secondary": ops_focus,
        "primary_product": primary_product,
        "secondary_product": secondary_product
    }

verticals_to_check = [
    "Krankenhaus", 
    "Pflege", # Might be "Altenheim" or similar
    "Hotel", 
    "Industrie", # Might be "Manufacturing"
    "Logistik",
    "Einzelhandel",
    "Facility Management"
]

print("-" * 60)
for v in verticals_to_check:
    data = get_vertical_data(v)
    if data:
        print(f"VERTICAL: {data['name']}")
        print(f"  Primary Product: {data['primary_product']}")
        print(f"  Secondary Product: {data['secondary_product']}")
        print(f"  Ops. Focus Secondary: {data['ops_focus_secondary']}")
        print(f"  PAINS: {data['pains']}")
        print(f"  GAINS: {data['gains']}")
        print("-" * 60)
