"""Credit balance and transaction data models."""

from __future__ import annotations

from dataclasses import dataclass
from datetime import datetime
from typing import Optional


@dataclass
class CreditBalance:
    node_id: str
    balance: int
    reserved_balance: int = 0
    total_balance: int = 0
    updated_at: Optional[datetime] = None

    @classmethod
    def from_row(cls, row: dict) -> CreditBalance:
        return cls(
            node_id=row["node_id"],
            balance=row.get("balance", 0),
            reserved_balance=row.get("reserved_balance", 0),
            total_balance=row.get("total_balance", row.get("balance", 0) + row.get("reserved_balance", 0)),
            updated_at=row.get("updated_at"),
        )


@dataclass
class Transaction:
    id: str
    from_node_id: Optional[str]
    to_node_id: Optional[str]
    amount: int
    type: str
    task_id: Optional[str] = None
    memo: Optional[str] = None
    created_at: Optional[datetime] = None

    @classmethod
    def from_row(cls, row: dict) -> Transaction:
        return cls(
            id=row["id"],
            from_node_id=row.get("from_node_id"),
            to_node_id=row.get("to_node_id"),
            amount=row["amount"],
            type=row["type"],
            task_id=row.get("task_id"),
            memo=row.get("memo"),
            created_at=row.get("created_at"),
        )
