#!/usr/bin/env python3
"""Check Bayern vs Augsburg match details"""

import sys
sys.path.insert(0, 'ai-engine')

import psycopg2
from psycopg2.extras import RealDictCursor
import os

# Read .env file manually
db_url = None
with open('.env') as f:
    for line in f:
        if line.startswith('DATABASE_URL'):
            db_url = line.split('=', 1)[1].strip().strip('"').strip("'")
            break

if not db_url:
    print("DATABASE_URL not found in .env")
    sys.exit(1)

# Remove schema parameter if present (psycopg2 doesn't support it)
if "?schema=" in db_url:
    db_url = db_url.split("?schema=")[0]

conn = psycopg2.connect(db_url)
cur = conn.cursor(cursor_factory=RealDictCursor)

# Find the match
cur.execute("""
SELECT 
    m.id, m.home_team_id, m.away_team_id,
    m.score_home, m.score_away, m.ht_score_home, m.ht_score_away,
    m.mst_utc, m.league_id,
    ht.name as home_team_name,
    at.name as away_team_name,
    l.name as league_name
FROM matches m
JOIN teams ht ON ht.id = m.home_team_id
JOIN teams at ON at.id = m.away_team_id
JOIN leagues l ON l.id = m.league_id
WHERE ht.name ILIKE '%bayern%' AND at.name ILIKE '%augsburg%'
AND m.mst_utc >= EXTRACT(EPOCH FROM '2026-01-01'::timestamp)
ORDER BY m.mst_utc DESC
LIMIT 5
""")

matches = cur.fetchall()
for m in matches:
    print(f"Match ID: {m['id']}")
    print(f"Teams: {m['home_team_name']} vs {m['away_team_name']}")
    print(f"Score: HT {m['ht_score_home']}-{m['ht_score_away']}, FT {m['score_home']}-{m['score_away']}")
    print(f"Timestamp: {m['mst_utc']}")
    print(f"League: {m['league_name']}")
    print()

cur.close()
conn.close()
