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.simplepay import SimplePay
from app.schemas.simplepay import SimplePayCreate, SimplePayUpdate

from app.utils import random_name

class CRUDSimplePay(CRUDBase[SimplePay, SimplePayCreate, SimplePayUpdate]):
    def create_with_payment_data(
        self, db: Session, *, obj_in: SimplePayCreate, 
        epayment:str,
        remark: str
    ) -> SimplePay:
        
        obj_in_data = jsonable_encoder(obj_in)
        db_obj = self.model(**obj_in_data, 
                            epayment=epayment,
                            remark=remark)
        db.add(db_obj)
        db.commit()
        db.refresh(db_obj)
        return db_obj
      
    def get_all_desc(self, db: Session) -> List[SimplePay]:
        return (
            db.query(self.model)
            .order_by(desc(SimplePay.id))
            .all()
        )
        
simplepay = CRUDSimplePay(SimplePay)