from app.database import SessionLocal
from app.models.article import Article, QuestionArticleLink
from app.services import sso_roles

# What the sync will make of each at their next sign-in.
for groups, who in [(["pedshub-members"], "daniel (now only pedshub-members)"),
                    (["pedshub-admins"], "danitex (pedshub-admins)")]:
    try:
        print(f"{who}: role_for -> {sso_roles.role_for(groups)}")
    except Exception as exc:
        print(f"{who}: {type(exc).__name__} {exc}")

db = SessionLocal()
# The two EB questions move to the new article.
moved = 0
for qid in (1285, 3011):
    rows = db.query(QuestionArticleLink).filter_by(question_id=qid).all()
    if not db.get(Article, 480):
        print("!! article 480 not found"); break
    for row in rows:
        if row.article_id != 480:
            row.article_id, row.section_id = 480, None
            moved += 1
    if not rows:
        db.add(QuestionArticleLink(question_id=qid, article_id=480,
                                   section_id=None, user_id=None))
        moved += 1
db.commit()
print("EB links pointed at 480:", moved)
db.close()
