from sqlalchemy.orm import Session
from app.database import SessionLocal, engine, Base
from app.models import Track, PlayHistory
from datetime import datetime, timedelta

Base.metadata.create_all(bind=engine)
db = SessionLocal()

# clear
db.query(PlayHistory).delete()
db.query(Track).delete()
db.commit()

# Create tracks
t1 = Track(id="t1", name="Midnight City", artist="M83", album="Hurry Up, We're Dreaming", duration_ms=243000, danceability=0.6, energy=0.8, valence=0.5, raw_data={})
t2 = Track(id="t2", name="Weightless", artist="Marconi Union", album="Weightless", duration_ms=480000, danceability=0.2, energy=0.1, valence=0.1, raw_data={})
t3 = Track(id="t3", name="Levitating", artist="Dua Lipa", album="Future Nostalgia", duration_ms=203000, danceability=0.8, energy=0.9, valence=0.9, raw_data={})

db.add_all([t1, t2, t3])
db.commit()

# Create history
ph1 = PlayHistory(track_id="t1", played_at=datetime.utcnow() - timedelta(minutes=10))
ph2 = PlayHistory(track_id="t2", played_at=datetime.utcnow() - timedelta(minutes=30))
ph3 = PlayHistory(track_id="t3", played_at=datetime.utcnow() - timedelta(minutes=60))

db.add_all([ph1, ph2, ph3])
db.commit()

print("Data populated")
db.close()
