# Path: tkp/db/model.py
# class Rejectreason(Base):
#     __tablename__ = 'rejectreason'
# 
#     id = Column(Integer,  primary_key=True)
#     description = Column(String(512))
# 
# class Rejection(Base):
#     __tablename__ = 'rejection'
# 
#     id = Column(Integer, primary_key=True)
# 
#     image_id = Column('image', ForeignKey('image.id'), index=True)
#     image = relationship('Image')
# 
#     # TO DO: Rename this column to 'rejectreason_id',
#     # (rather than just 'rejectreason') so the model attribute matches
#     # the SQL column name, avoiding the current confusing name-shadowing
#     # between the SQL columns and the model attributes. (Issue #508)
#     rejectreason_id = Column('rejectreason', ForeignKey('rejectreason.id'), index=True)
#     rejectreason = relationship('Rejectreason')
# 
#     comment = Column(String(512))


import logging
from tkp.db.model import Rejectreason, Rejection

def sync_rejectreasons(session):
    """
    Check if rejectreasons are in sync. If not, insert as needed and commit.

    Args:
        session (sqlalchemy.orm.session.Session): Database session.
    """
    if session.query(Rejectreason).count() != len(reject_reasons):
        dbreason_id_rows = session.query(Rejectreason.id).all()
        dbreason_ids = [row[0] for row in dbreason_id_rows]
        for r in reject_reasons.values():
            if r.id not in dbreason_ids:
                logger.info("Added to database Rejectreason {}:'{}'".format(
                    r.id, r.description
                ))
                session.add(r)
        session.commit()


def reject(imageid, reason, comment, session):
    """
    Add a reject reason to the db for a given image.

    Args:
        imageid (int): The image ID of the image to reject
        reason (tkp.db.model.Rejectreason): Why is the image rejected
        comment (str): An optional comment with details about the reason
        session (sqlalchemy.orm.session.Session): Database session.
    """

# Path: tkp/db/model.py
# class Rejectreason(Base):
#     __tablename__ = 'rejectreason'
# 
#     id = Column(Integer,  primary_key=True)
#     description = Column(String(512))
# 
# class Rejection(Base):
#     __tablename__ = 'rejection'
# 
#     id = Column(Integer, primary_key=True)
# 
#     image_id = Column('image', ForeignKey('image.id'), index=True)
#     image = relationship('Image')
# 
#     # TO DO: Rename this column to 'rejectreason_id',
#     # (rather than just 'rejectreason') so the model attribute matches
#     # the SQL column name, avoiding the current confusing name-shadowing
#     # between the SQL columns and the model attributes. (Issue #508)
#     rejectreason_id = Column('rejectreason', ForeignKey('rejectreason.id'), index=True)
#     rejectreason = relationship('Rejectreason')
# 
#     comment = Column(String(512))


# Path: tkp/db/quality.py
import logging
from tkp.db.model import Rejectreason, Rejection


def sync_rejectreasons(session):
    """
    Check if rejectreasons are in sync. If not, insert as needed and commit.

    Args:
        session (sqlalchemy.orm.session.Session): Database session.
    """
    if session.query(Rejectreason).count() != len(reject_reasons):
        dbreason_id_rows = session.query(Rejectreason.id).all()
        dbreason_ids = [row[0] for row in dbreason_id_rows]
        for r in reject_reasons.values():
            if r.id not in dbreason_ids:
                logger.info("Added to database Rejectreason {}:'{}'".format(
                    r.id, r.description
                ))
                session.add(r)
        session.commit()


def reject(imageid, reason, comment, session):
    """
    Add a reject reason to the db for a given image.

    Args:
        imageid (int): The image ID of the image to reject
        reason (tkp.db.model.Rejectreason): Why is the image rejected
        comment (str): An optional comment with details about the reason
        session (sqlalchemy.orm.session.Session): Database session.
    """

    r = Rejection(image_id=imageid,