from datetime import datetime, timezone

from packages.adapters.sqlalchemy_impl.models import TitleLibraryModel


def mark_title_used_for_generation(db, task) -> None:
    if not task.strategy_id:
        return
    title = db.query(TitleLibraryModel).filter(TitleLibraryModel.id == task.strategy_id).first()
    if title is None:
        return
    title.usage_count = int(title.usage_count or 0) + 1
    title.updated_at = datetime.now(timezone.utc)
    db.add(title)
    db.commit()
