import sys
sys.path.insert(0, "/app")

from sqlalchemy.orm import Session
from datetime import datetime, timedelta
from app.core.db.database import SessionLocal
from app.core.db.models.match import Match, MatchStatus
from app.core.db.models.feature import Feature
from app.ml.features import FeatureBuilder
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

db = SessionLocal()

# Get all finished matches
from_date = datetime.now() - timedelta(days=180)
matches = db.query(Match).filter(
    Match.status == MatchStatus.FINISHED.value,
    Match.home_score.isnot(None),
    Match.away_score.isnot(None),
    Match.match_date >= from_date
).all()

logger.info(f"Found {len(matches)} finished matches")

# Get existing features
existing_feature_ids = {f.match_id for f in db.query(Feature.match_id).all()}
logger.info(f"Found {len(existing_feature_ids)} existing features")

# Filter matches without features
matches_to_process = [m for m in matches if m.id not in existing_feature_ids]
logger.info(f"Need to create features for {len(matches_to_process)} matches")

if not matches_to_process:
    logger.info("All matches already have features")
else:
    feature_builder = FeatureBuilder(db)
    created = 0
    errors = 0
    
    for i, match in enumerate(matches_to_process):
        try:
            existing = db.query(Feature).filter(Feature.match_id == match.id).first()
            if existing:
                continue
            
            feature = feature_builder.build_features_for_match(match)
            db.add(feature)
            created += 1
            
            if created % 50 == 0:
                db.commit()
                logger.info(f"Created {created} features...")
                
        except Exception as e:
            logger.error(f"Error for match {match.id}: {e}")
            errors += 1
            db.rollback()
            continue
    
    db.commit()
    logger.info(f"Created {created} new features, {errors} errors")

total_features = db.query(Feature).count()
logger.info(f"Total features: {total_features}")
