"""Article 3 is filed on the Pulmonology discipline, not the Bronchiolitis leaf.

Through the service the route uses, not raw SQL: the validation that matters —
the article exists, the category exists, the claim is priced before it is
staked — is in that path and nowhere else.
"""
from app.database import SessionLocal
from app.models.article import Article, ArticleTopicClaim
from app.models.question_category import QuestionCategory
from app.services import topic_claims

db = SessionLocal()
article = db.get(Article, 3)
leaf = db.get(QuestionCategory, 15110)
print("article:", article.id, article.title, "category_id:", article.category_id)
print("leaf:", leaf.id, leaf.name, "parent:", leaf.parent_id)

article.category_id = leaf.id
db.commit()

claim = db.query(ArticleTopicClaim).filter_by(
    article_id=3, category_id=15110, section_id=None).first()
if claim is None:
    claim = ArticleTopicClaim(article_id=3, category_id=15110, section_id=None,
                              include_subtopics=True, fill_only=True, user_id=None)
    db.add(claim)
    db.commit()
    db.refresh(claim)

priced = topic_claims.preview(db, claim)
print("priced:", priced)
made = topic_claims.apply(db, claim)
print("linked:", made)
db.close()
