#!/usr/bin/env python3
"""Find surprise matches from database."""

import psycopg2
from psycopg2.extras import RealDictCursor
import os
from datetime import datetime

# Use the same DSN as the main project
conn = psycopg2.connect(os.environ.get('DATABASE_URL', 'postgresql://suggestbet:SuGGesT2026SecuRe@localhost:15432/boilerplate_db'))
cur = conn.cursor(cursor_factory=RealDictCursor)

# Bayern Munich vs Augsburg - 24 Jan 2026
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,
       th.name as home_name, ta.name as away_name, l.name as league
FROM matches m
JOIN teams th ON m.home_team_id = th.id
JOIN teams ta ON m.away_team_id = ta.id
JOIN leagues l ON m.league_id = l.id
WHERE th.name ILIKE '%bayern%' AND ta.name ILIKE '%augsburg%'
AND m.mst_utc >= EXTRACT(EPOCH FROM '2026-01-20'::timestamp) * 1000
AND m.mst_utc <= EXTRACT(EPOCH FROM '2026-01-30'::timestamp) * 1000
ORDER BY m.mst_utc DESC
LIMIT 5
""")
print('=== Bayern vs Augsburg (24 Jan 2026) ===')
for row in cur.fetchall():
    match_date = datetime.fromtimestamp(row['mst_utc'] / 1000)
    print(f"ID: {row['id']}")
    print(f"Match: {row['home_name']} vs {row['away_name']}")
    print(f"Score: {row['score_home']}-{row['score_away']} (HT: {row['ht_score_home']}-{row['ht_score_away']})")
    print(f"Date: {match_date}")
    print(f"League: {row['league']}")
    print()

# Benfica vs Real Madrid - 18 Feb 2026
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,
       th.name as home_name, ta.name as away_name, l.name as league
FROM matches m
JOIN teams th ON m.home_team_id = th.id
JOIN teams ta ON m.away_team_id = ta.id
JOIN leagues l ON m.league_id = l.id
WHERE th.name ILIKE '%benfica%' AND ta.name ILIKE '%real madrid%'
AND m.mst_utc >= EXTRACT(EPOCH FROM '2026-02-15'::timestamp) * 1000
AND m.mst_utc <= EXTRACT(EPOCH FROM '2026-02-20'::timestamp) * 1000
ORDER BY m.mst_utc DESC
LIMIT 5
""")
print('=== Benfica vs Real Madrid (18 Feb 2026) ===')
for row in cur.fetchall():
    match_date = datetime.fromtimestamp(row['mst_utc'] / 1000)
    print(f"ID: {row['id']}")
    print(f"Match: {row['home_name']} vs {row['away_name']}")
    print(f"Score: {row['score_home']}-{row['score_away']} (HT: {row['ht_score_home']}-{row['ht_score_away']})")
    print(f"Date: {match_date}")
    print(f"League: {row['league']}")
    print()

# Find all 1/2 and 2/1 HT/FT results in recent matches
cur.execute("""
SELECT m.id, m.score_home, m.score_away, m.ht_score_home, m.ht_score_away,
       th.name as home_name, ta.name as away_name, l.name as league, m.mst_utc
FROM matches m
JOIN teams th ON m.home_team_id = th.id
JOIN teams ta ON m.away_team_id = ta.id
JOIN leagues l ON m.league_id = l.id
WHERE m.mst_utc >= EXTRACT(EPOCH FROM '2026-01-01'::timestamp) * 1000
AND m.score_home IS NOT NULL AND m.score_away IS NOT NULL
AND m.ht_score_home IS NOT NULL AND m.ht_score_away IS NOT NULL
AND (
    (m.ht_score_home > m.ht_score_away AND m.score_home < m.score_away)  -- 1/2 reversal
    OR (m.ht_score_home < m.ht_score_away AND m.score_home > m.score_away)  -- 2/1 reversal
)
ORDER BY m.mst_utc DESC
LIMIT 30
""")
print('=== Recent HT/FT Reversals (1/2 or 2/1) ===')
for row in cur.fetchall():
    match_date = datetime.fromtimestamp(row['mst_utc'] / 1000)
    ht_result = "1" if row['ht_score_home'] > row['ht_score_away'] else ("2" if row['ht_score_home'] < row['ht_score_away'] else "X")
    ft_result = "1" if row['score_home'] > row['score_away'] else ("2" if row['score_home'] < row['score_away'] else "X")
    print(f"{row['home_name']} vs {row['away_name']} | HT: {row['ht_score_home']}-{row['ht_score_away']} FT: {row['score_home']}-{row['score_away']} | {ht_result}/{ft_result} | {match_date}")

conn.close()
