"""billing.metering — Token usage recording."""
from __future__ import annotations
from agentpaas.db.session import get_db
from agentpaas.db.models import gen_id, now_utc


def record_usage(tenant_id: str, agent_id: str, run_id: str,
                 model: str, input_tokens: int, output_tokens: int,
                 tool_calls: int = 0, duration_ms: int = 0, cost_usd: float = 0.0):
    db = get_db()
    db.execute(
        "INSERT INTO usage_records (id, tenant_id, agent_id, run_id, model, "
        "input_tokens, output_tokens, tool_calls, duration_ms, cost_usd, created_at) "
        "VALUES (?,?,?,?,?,?,?,?,?,?,?)",
        (gen_id("usg_"), tenant_id, agent_id, run_id, model,
         input_tokens, output_tokens, tool_calls, duration_ms, cost_usd, now_utc())
    )
    db.commit()
