# Session cleanup: since deletes now keep history, remove history rows left by TEST_ tasks deleted during this session
import psycopg
import pytest
from dotenv import dotenv_values

ENV = dotenv_values("/app/backend/.env")


@pytest.fixture(scope="session", autouse=True)
def cleanup_test_history():
    with psycopg.connect(ENV["DATABASE_URL"]) as c:
        start = c.execute("SELECT coalesce(max(id), 0) FROM todo_history").fetchone()[0]
    yield
    with psycopg.connect(ENV["DATABASE_URL"], autocommit=True) as c:
        c.execute("""DELETE FROM todo_history WHERE id > %s AND todo_id IS NULL AND task_title LIKE 'TEST\\_%%'""",
                  (start,))
