import sys
sys.path.insert(0, "/app")
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.team import Team
from app.core.db.repositories.league_repository import LeagueRepository
from app.core.db.repositories.team_repository import TeamRepository
from app.core.db.repositories.match_repository import MatchRepository

db = SessionLocal()
lr = LeagueRepository(db)
tr = TeamRepository(db)
mr = MatchRepository(db)

league = lr.get_by_source_id("test_league")
if not league:
    print("League not found")
    sys.exit(1)

teams = db.query(Team).limit(10).all()
if len(teams) < 2:
    print("Not enough teams")
    sys.exit(1)

now = datetime.now()
created = 0
for i in range(10):
    match_date = now + timedelta(days=i+1, hours=15)
    home = teams[i % len(teams)]
    away = teams[(i+1) % len(teams)]
    
    existing = mr.get_by_source_id(f"future_match_{i}_{int(match_date.timestamp())}")
    if existing:
        continue
    
    match = Match(
        league_id=league.id,
        home_team_id=home.id,
        away_team_id=away.id,
        match_date=match_date,
        status=MatchStatus.SCHEDULED.value,
        source_id=f"future_match_{i}_{int(match_date.timestamp())}"
    )
    db.add(match)
    created += 1

db.commit()
print(f"Created {created} future matches")
