#!/usr/bin/env python3
"""
Test Restaurant Scraper - Simple version to validate approach
"""

import requests
import json
import time
import os
import uuid
import re
from datetime import datetime
from urllib.parse import quote_plus
from sqlalchemy import create_engine, text
from bs4 import BeautifulSoup

def test_tripadvisor_access():
    """Test basic access to TripAdvisor"""
    print("🧪 Testing TripAdvisor access...")
    
    session = requests.Session()
    session.headers.update({
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
    })
    
    url = "https://www.tripadvisor.com/Restaurants-g189400-oa30-zfp10954-Athens_Attica.html"
    
    try:
        response = session.get(url, timeout=15)
        print(f"✅ Status: {response.status_code}")
        print(f"✅ Content length: {len(response.content)} bytes")
        
        soup = BeautifulSoup(response.content, 'html.parser')
        
        # Find restaurant links
        links = soup.find_all('a')
        restaurant_links = []
        
        for link in links:
            if hasattr(link, 'get'):
                href = link.get('href')
                if href and isinstance(href, str) and '/Restaurant_Review' in href:
                    if href.startswith('/'):
                        href = 'https://www.tripadvisor.com' + href
                    restaurant_links.append(href)
        
        print(f"✅ Found {len(restaurant_links)} potential restaurant links")
        
        # Show first few
        for i, link in enumerate(restaurant_links[:5]):
            print(f"  {i+1}. {link}")
        
        return restaurant_links[:10]  # Return first 10 for testing
        
    except Exception as e:
        print(f"❌ Error: {e}")
        return []

def test_restaurant_details(url):
    """Test extracting details from a single restaurant"""
    print(f"\n🧪 Testing restaurant details extraction...")
    print(f"🔍 URL: {url}")
    
    session = requests.Session()
    session.headers.update({
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
    })
    
    try:
        response = session.get(url, timeout=15)
        soup = BeautifulSoup(response.content, 'html.parser')
        
        # Extract name
        name = "Unknown"
        h1_tags = soup.find_all('h1')
        for h1 in h1_tags:
            text = h1.get_text(strip=True)
            if text and len(text) > 3:
                name = text
                break
        
        print(f"✅ Name: {name}")
        
        # Extract some text
        all_text = soup.get_text()[:1000]  # First 1000 chars
        print(f"✅ Text sample: {all_text[:200]}...")
        
        # Look for images
        images = soup.find_all('img')
        photo_count = 0
        for img in images:
            if hasattr(img, 'get'):
                src = img.get('src')
                if src and isinstance(src, str) and any(keyword in src.lower() for keyword in ['restaurant', 'food', 'photo']):
                    photo_count += 1
        
        print(f"✅ Found {photo_count} potential photos")
        
        # Look for prices
        price_matches = re.findall(r'[€$£¥]\s*\d+(?:[.,]\d{2})?', all_text)
        print(f"✅ Found {len(price_matches)} price mentions: {price_matches[:5]}")
        
        return {
            'name': name,
            'photos': photo_count,
            'prices': len(price_matches)
        }
        
    except Exception as e:
        print(f"❌ Error extracting details: {e}")
        return None

def test_database_connection():
    """Test database connection and structure"""
    print(f"\n🧪 Testing database connection...")
    
    try:
        DATABASE_URL = f"postgresql://postgres:{quote_plus('F@f@k0s!!')}@localhost:5432/bookbeach"
        engine = create_engine(DATABASE_URL)
        
        with engine.connect() as db:
            # Test basic queries
            result = db.execute(text("SELECT COUNT(*) FROM restaurants")).fetchone()
            current_count = result[0] if result else 0
            print(f"✅ Current restaurants in DB: {current_count}")
            
            # Check company
            company_result = db.execute(text("SELECT company_name FROM companies WHERE company_name = 'bookbeach'")).fetchone()
            print(f"✅ Company found: {company_result[0] if company_result else 'None'}")
            
            # Check currencies
            currency_result = db.execute(text("SELECT currency_code FROM currencies WHERE currency_code = 'EUR'")).fetchone()
            print(f"✅ EUR currency found: {currency_result[0] if currency_result else 'None'}")
            
            return True
            
    except Exception as e:
        print(f"❌ Database error: {e}")
        return False

def test_save_sample_restaurant():
    """Test saving a sample restaurant"""
    print(f"\n🧪 Testing restaurant save...")
    
    try:
        DATABASE_URL = f"postgresql://postgres:{quote_plus('F@f@k0s!!')}@localhost:5432/bookbeach"
        engine = create_engine(DATABASE_URL)
        
        with engine.connect() as db:
            restaurant_id = str(uuid.uuid4())
            company_id = "c35388d2-0028-4002-bcc4-db4d7ed2042e"
            
            # Insert test restaurant
            db.execute(text("""
                INSERT INTO restaurants (
                    restaurant_id, restaurant_name, company_id,
                    description, cuisine_type, is_active, created_at
                ) VALUES (
                    :restaurant_id, :name, :company_id,
                    :description, :cuisine_type, true, NOW()
                )
            """), {
                'restaurant_id': restaurant_id,
                'name': 'Test Athens Restaurant',
                'company_id': company_id,
                'description': 'Test restaurant for scraper validation',
                'cuisine_type': 'Greek'
            })
            
            # Create category
            category_id = int(time.time() * 1000) % 2000000000
            db.execute(text("""
                INSERT INTO restaurant_categories (
                    category_id, restaurant_id, category_name
                ) VALUES (:category_id, :restaurant_id, :category_name)
            """), {
                'category_id': category_id,
                'restaurant_id': restaurant_id,
                'category_name': 'Greek'
            })
            
            # Add menu item
            item_id = str(uuid.uuid4())
            db.execute(text("""
                INSERT INTO restaurant_items (
                    item_id, restaurant_id, category_id, item_name,
                    price, currency_id, is_available, created_at
                ) VALUES (
                    :item_id, :restaurant_id, :category_id, :item_name,
                    :price, :currency_id, true, NOW()
                )
            """), {
                'item_id': item_id,
                'restaurant_id': restaurant_id,
                'category_id': category_id,
                'item_name': 'Test Moussaka',
                'price': 15.50,
                'currency_id': 1
            })
            
            db.commit()
            print(f"✅ Successfully saved test restaurant: {restaurant_id}")
            
            # Clean up
            db.execute(text("DELETE FROM restaurant_items WHERE restaurant_id = :id"), {'id': restaurant_id})
            db.execute(text("DELETE FROM restaurant_categories WHERE restaurant_id = :id"), {'id': restaurant_id})
            db.execute(text("DELETE FROM restaurants WHERE restaurant_id = :id"), {'id': restaurant_id})
            db.commit()
            print(f"✅ Cleaned up test data")
            
            return True
            
    except Exception as e:
        print(f"❌ Save test failed: {e}")
        return False

def main():
    """Run all tests"""
    print("🚀 Running Restaurant Scraper Tests\n")
    
    # Test 1: Basic TripAdvisor access
    restaurant_urls = test_tripadvisor_access()
    
    # Test 2: Database connection
    db_ok = test_database_connection()
    
    # Test 3: Database save functionality
    save_ok = test_save_sample_restaurant()
    
    # Test 4: Restaurant detail extraction
    if restaurant_urls:
        restaurant_data = test_restaurant_details(restaurant_urls[0])
    
    print(f"\n📊 TEST RESULTS:")
    print(f"   ✅ TripAdvisor access: {'OK' if restaurant_urls else 'FAILED'}")
    print(f"   ✅ Database connection: {'OK' if db_ok else 'FAILED'}")
    print(f"   ✅ Database save: {'OK' if save_ok else 'FAILED'}")
    print(f"   ✅ Detail extraction: {'OK' if restaurant_urls and 'restaurant_data' in locals() else 'FAILED'}")
    
    if all([restaurant_urls, db_ok, save_ok]):
        print(f"\n🎉 All tests passed! Ready to run full scraper.")
    else:
        print(f"\n⚠️ Some tests failed. Please check issues before running full scraper.")

if __name__ == "__main__":
    main()