from datetime import timezone
from zoneinfo import ZoneInfo
from app import create_app, db
from src.models import Article, Issue

def migrate():
    app = create_app(start_scheduler=False)
    local_tz = ZoneInfo("America/New_York")
    
    with app.app_context():
        # Migrate Articles
        for article in Article.query.all():
            # Assume existing naive datetime is UTC
            utc_dt = article.pub_date.replace(tzinfo=timezone.utc)
            article.pub_date = utc_dt.astimezone(local_tz).replace(tzinfo=None)
            
            utc_fetched = article.fetched_at.replace(tzinfo=timezone.utc)
            article.fetched_at = utc_fetched.astimezone(local_tz).replace(tzinfo=None)
            
        # Migrate Issues
        for issue in Issue.query.all():
            utc_created = issue.created_at.replace(tzinfo=timezone.utc)
            issue.created_at = utc_created.astimezone(local_tz).replace(tzinfo=None)
            
        db.session.commit()
        print("Migration complete.")

if __name__ == "__main__":
    migrate()
