from sqlmodel import Session, select, func
from database import engine
from models import Tour, Show

def fix_tour_dates():
    with Session(engine) as session:
        tours = session.exec(select(Tour)).all()
        print(f"Fixing dates for {len(tours)} tours...")
        
        for tour in tours:
            # Find min/max show date
            result = session.exec(
                select(func.min(Show.date), func.max(Show.date))
                .where(Show.tour_id == tour.id)
            ).first()
            
            if result and result[0]:
                tour.start_date = result[0]
                tour.end_date = result[1]
                session.add(tour)
                # print(f"  {tour.name}: {tour.start_date.date()} - {tour.end_date.date()}")
            # else:
                # print(f"  {tour.name}: No shows found")
                
        session.commit()
        print("Done.")

if __name__ == "__main__":
    fix_tour_dates()
