from typing import List

from fastapi.encoders import jsonable_encoder
from sqlalchemy.orm import Session
from sqlalchemy import desc
from app.crud.base import CRUDBase
from app.models.payment import Payment
from app.schemas.payment import PaymentCreate, PaymentUpdate

from app.utils import random_name

class CRUDPayment(CRUDBase[Payment, PaymentCreate, PaymentUpdate]):
    def create_with_payment_data(
        self, db: Session, *, obj_in: PaymentCreate, 
        owner_id:int,
        epayment:str,
        remark: str
    ) -> Payment:
        
        obj_in_data = jsonable_encoder(obj_in)
        db_obj = self.model(**obj_in_data, 
                            owner_id=owner_id,
                            epayment=epayment,
                            remark=remark)
        print(db_obj)
        db.add(db_obj)
        db.commit()
        db.refresh(db_obj)
        return db_obj
      
    def get_all_desc(self, db: Session) -> List[Payment]:
        return (
            db.query(self.model)
            .order_by(desc(Payment.id))
            .all()
        )
        
payment = CRUDPayment(Payment)
