import os
import requests
import json
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

API_KEY = os.getenv("GOOGLE_API_KEY")
PLACE_ID = os.getenv("PLACE_ID")

def fetch_place_reviews(place_id, api_key):
    url = f"https://places.googleapis.com/v1/places/{place_id}"
    
    params = {
        "languageCode": "id"
    }
    
    headers = {
        "Content-Type": "application/json",
        "X-Goog-Api-Key": api_key,
        "X-Goog-FieldMask": "id,displayName,reviews"
    }
    
    try:
        response = requests.get(url, headers=headers, params=params)
        response.raise_for_status()
        data = response.json()
        
        if "reviews" in data:
            for review in data["reviews"]:
                lang = review.get("originalText", {}).get("languageCode")
                if lang and lang != "en":
                    print(f"Found non-English review! Language: {lang}")
                    print(json.dumps(review, indent=2))
        
        return data
    except requests.exceptions.HTTPError as http_err:
        print(f"HTTP error occurred: {http_err}")
    except Exception as err:
        print(f"An error occurred: {err}")
    return None

if __name__ == "__main__":
    print(f"Fetching reviews for Place ID: {PLACE_ID}...")
    data = fetch_place_reviews(PLACE_ID, API_KEY)
    
    if data:
        print("Successfully fetched data!")
        print("\n--- SAMPLE RESPONSE STARTS ---")
        print(json.dumps(data, indent=2))
        print("--- SAMPLE RESPONSE ENDS ---\n")
    else:
        print("Failed to fetch data.")
