"""UsageRecord model — metering for billing (build plan Section 5)."""

from __future__ import annotations

import uuid

from sqlalchemy import BigInteger, Index, String, Uuid
from sqlalchemy.orm import Mapped, mapped_column

from legate.db.base import Base, TimestampMixin, UUIDPrimaryKeyMixin


class UsageRecord(UUIDPrimaryKeyMixin, TimestampMixin, Base):
    __tablename__ = "usage_records"
    __table_args__ = (Index("ix_usage_ws_metric_period", "workspace_id", "metric", "period"),)

    workspace_id: Mapped[uuid.UUID] = mapped_column(Uuid, index=True, nullable=False)
    # e.g. "runs", "tokens".
    metric: Mapped[str] = mapped_column(String(50), nullable=False)
    quantity: Mapped[int] = mapped_column(BigInteger, nullable=False, default=0)
    # Billing period bucket, "YYYY-MM".
    period: Mapped[str] = mapped_column(String(7), nullable=False)

    def __repr__(self) -> str:  # pragma: no cover - debug helper
        return f"<UsageRecord ws={self.workspace_id} {self.metric}={self.quantity} {self.period}>"
