import datetime

from backend.repository import (
    archive_old_news,
    create_news,
    create_translation,
    delete_archived_news,
    get_translation,
)


def test_translation_persistence(db_session):
    item = create_news(
        db=db_session,
        headline="AI chip demand surges",
        summary="Demand for AI chips increased this quarter.",
    )
    create_translation(
        db=db_session,
        news_item_id=item.id,
        language="ml",
        headline="എഐ ചിപ്പ് ആവശ്യകത ഉയർന്നു",
        summary="ഈ പാദത്തിൽ എഐ ചിപ്പ് ആവശ്യകത ഉയർന്നു.",
    )
    translation = get_translation(db_session, item.id, "ml")
    assert translation is not None
    assert translation.language == "ml"


def test_archiving_and_deletion_workflow(db_session):
    old_item = create_news(
        db=db_session,
        headline="Old item",
        summary="Old item summary",
        published_at=datetime.datetime.utcnow() - datetime.timedelta(days=120),
    )
    old_item.created_at = datetime.datetime.utcnow() - datetime.timedelta(days=120)
    db_session.commit()

    archived_count = archive_old_news(db_session, retention_days=30)
    assert archived_count >= 1

    deleted_count = delete_archived_news(db_session, days_after_archive=60)
    assert deleted_count >= 1
