from sqlalchemy.orm import Session
from app.models.transaction import Transaction
from app.core.constants import *


def get_transaction_by_payme_id(db: Session, payme_id: str):
    return db.query(Transaction).filter(
        Transaction.payme_transaction_id == payme_id
    ).first()


def create_transaction(db: Session, order_id: str, payme_id: str, amount: int):
    transaction = Transaction(
        order_id=order_id,
        payme_transaction_id=payme_id,
        amount=amount,
        state=STATE_CREATED,
    )
    db.add(transaction)
    db.commit()
    db.refresh(transaction)
    return transaction


def update_transaction_state(db: Session, transaction: Transaction, state: int):
    transaction.state = state
    db.commit()
    db.refresh(transaction)
    return transaction