"""Usage Log Model"""
from sqlalchemy import Column, String, DateTime, ForeignKey, Integer, Numeric
from sqlalchemy.dialects.postgresql import UUID, JSONB
from sqlalchemy.sql import func
import uuid
from app.database import Base


class UsageLog(Base):
    __tablename__ = "usage_logs"

    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    user_id = Column(UUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL"))
    job_id = Column(UUID(as_uuid=True), ForeignKey("jobs.id", ondelete="SET NULL"))

    # What was used
    module = Column(String(100), nullable=False)
    action = Column(String(100), nullable=False)
    api_provider = Column(String(100))
    api_model = Column(String(100))

    # Metrics
    tokens_input = Column(Integer)
    tokens_output = Column(Integer)
    api_credits_used = Column(Numeric(10, 4))
    estimated_cost_usd = Column(Numeric(10, 4))
    processing_time_ms = Column(Integer)

    # Request details
    request_metadata = Column(JSONB)
    response_metadata = Column(JSONB)

    created_at = Column(DateTime(timezone=True), server_default=func.now())
