from typing import List
from sqlalchemy.orm import Session
from app.models.ppr import Journal
from app.schemas.ppr import JournalCreate


class CRUDJournal:
    def create(self, db: Session, obj_in: JournalCreate) -> Journal:
        db_obj = Journal(**obj_in.dict())
        db.add(db_obj)
        db.commit()
        db.refresh(db_obj)
        return db_obj

    def get_by_ppr_id(self, db: Session, ppr_id: int) -> List[Journal]:
        return db.query(Journal).filter(Journal.ppr_id == ppr_id).order_by(Journal.entry_dt.desc()).all()

    def log_change(
        self, 
        db: Session, 
        ppr_id: int, 
        entry: str, 
        user: str, 
        ip: str
    ) -> Journal:
        journal_in = JournalCreate(
            ppr_id=ppr_id,
            entry=entry,
            user=user,
            ip=ip
        )
        return self.create(db, journal_in)


journal = CRUDJournal()