"""Migration 006: Add stale_flagged_at and last_confirmed_at to knowledge_entries.

The knowledge.py code uses these columns for staleness tracking:
- stale_flagged_at: when an entry was flagged as stale
- last_confirmed_at: when an entry was last confirmed as still relevant

Both were referenced in code but never added to the schema.
"""
from . import register


@register(6, "Add stale_flagged_at and last_confirmed_at to knowledge_entries")
def migrate(db) -> None:
    """Add missing columns to knowledge_entries (idempotent)."""
    columns = db.execute("PRAGMA table_info(knowledge_entries)")
    col_names = [c["name"] for c in columns]
    if "stale_flagged_at" not in col_names:
        db.execute(
            "ALTER TABLE knowledge_entries ADD COLUMN stale_flagged_at TEXT"
        )
    if "last_confirmed_at" not in col_names:
        db.execute(
            "ALTER TABLE knowledge_entries ADD COLUMN last_confirmed_at TEXT"
        )
