#!/usr/bin/env python3
"""
Verify Google Places API integration for social_media_audit.py.

This script tests the GooglePlacesSearcher class directly without
requiring database access.
"""
import os
import sys
from pathlib import Path

# Add user site-packages to path
sys.path.insert(0, '/Users/maciejpi/Library/Python/3.9/lib/python/site-packages')

# Load .env file from project root
try:
    from dotenv import load_dotenv
    script_dir = Path(__file__).resolve().parent
    project_root = script_dir.parent
    env_path = project_root / '.env'
    if env_path.exists():
        load_dotenv(env_path)
        print(f"Loaded .env from {env_path}")
except ImportError:
    print("python-dotenv not installed, using system environment")

# Import GooglePlacesSearcher from social_media_audit
from social_media_audit import GooglePlacesSearcher, BraveSearcher

def main():
    api_key = os.getenv('GOOGLE_PLACES_API_KEY')
    print(f"\nGoogle Places API Key: {'*' * 10 + api_key[-8:] if api_key else 'NOT SET'}")

    if not api_key or api_key == 'your_places_api_key_here':
        print("ERROR: GOOGLE_PLACES_API_KEY not configured in .env")
        sys.exit(1)

    # Test GooglePlacesSearcher directly
    print("\n" + "=" * 60)
    print("Testing GooglePlacesSearcher for INPI company")
    print("=" * 60)

    searcher = GooglePlacesSearcher(api_key=api_key)

    # Step 1: Find place
    print("\n1. Finding place for 'INPI' in Wejherowo...")
    place_id = searcher.find_place('INPI', 'Wejherowo')

    if not place_id:
        print("   ERROR: Could not find INPI on Google Places")
        print("   This could mean:")
        print("   - The company doesn't have a Google Business Profile")
        print("   - The API key doesn't have Places API enabled")
        print("   - The search query needs adjustment")
        sys.exit(1)

    print(f"   Found place_id: {place_id}")

    # Step 2: Get place details
    print("\n2. Getting place details...")
    details = searcher.get_place_details(place_id)

    print(f"\n   Results:")
    print(f"   - google_rating: {details.get('google_rating')}")
    print(f"   - google_reviews_count: {details.get('google_reviews_count')}")
    print(f"   - business_status: {details.get('business_status')}")
    print(f"   - opening_hours: {'Yes' if details.get('opening_hours') else 'Not available'}")

    if details.get('opening_hours'):
        hours = details.get('opening_hours', {})
        if hours.get('weekday_text'):
            print("   - Hours:")
            for day in hours['weekday_text']:
                print(f"     {day}")

    # Step 3: Test via BraveSearcher.search_google_reviews
    print("\n" + "=" * 60)
    print("Testing BraveSearcher.search_google_reviews()")
    print("=" * 60)

    brave = BraveSearcher()
    result = brave.search_google_reviews('INPI', 'Wejherowo')

    print(f"\n   Results:")
    print(f"   - google_rating: {result.get('google_rating')}")
    print(f"   - google_reviews_count: {result.get('google_reviews_count')}")
    print(f"   - business_status: {result.get('business_status')}")
    print(f"   - opening_hours: {'Yes' if result.get('opening_hours') else 'Not available'}")

    # Verify expected data
    print("\n" + "=" * 60)
    print("VERIFICATION SUMMARY")
    print("=" * 60)

    success = True

    if result.get('google_rating') is not None:
        print(f"[PASS] google_rating present: {result['google_rating']}")
    else:
        print("[FAIL] google_rating is None")
        success = False

    if result.get('google_reviews_count') is not None:
        print(f"[PASS] google_reviews_count present: {result['google_reviews_count']}")
    else:
        print("[FAIL] google_reviews_count is None")
        success = False

    if success:
        print("\n[SUCCESS] Google Places API integration is working!")
        print("The social_media_audit.py can now collect Google reviews data.")
    else:
        print("\n[WARNING] Some data was not retrieved.")
        print("Check if the business has a Google Business Profile with reviews.")

    return 0 if success else 1


if __name__ == '__main__':
    sys.exit(main())
