"""
transaksi.py — Model untuk mencatat semua transaksi (top-up, pemakaian, refund).
"""

from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Enum as SQLEnum
from datetime import datetime, timezone
from app.core.database import Base

def _utcnow():
    return datetime.now(timezone.utc)

class Transaction(Base):
    __tablename__ = "transactions"

    id          = Column(Integer, primary_key=True, index=True)
    user_id     = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
    amount      = Column(Integer, nullable=False)          # positif = top-up, negatif = pemakaian
    service_type = Column(SQLEnum("topup", "transcription", "ba", "admin_adjustment", name="service_type_enum"), nullable=False)
    task_id     = Column(Integer, ForeignKey("transkripsi_tasks.id", ondelete="SET NULL"), nullable=True)
    ba_id       = Column(Integer, ForeignKey("berita_acara.id", ondelete="SET NULL"), nullable=True)
    description = Column(String, nullable=True)
    created_at  = Column(DateTime(timezone=True), default=_utcnow)