import logging
from typing import List
from archivingsystem.database import Database
from archivingsystem.database import ArchivedFile
from archivingsystem.validation.validator import validation_task

log = logging.getLogger(__name__)

def all(db: Database, config: dict, api: bool=False) -> List[int]:
    """
    Validate all archived files in database. Returns list of invalid file IDs.
    :param api if `True` dont output to stdout
    """
    s = db.Session()
    files = s.query(ArchivedFile).all()

    invalid_ids = []

    for file in files:
        if not validation_task(config, file.FileID):
            invalid_ids.append(file.FileID)
            if not api:
                log.error(f"File {file.FileID} is invalid!")
        else:
            if not api:
                log.info(f"File {file.FileID} validation successfull.")

    return invalid_ids
    
def single(config: dict, file_id: int, api: bool=False) -> bool:
    """
    Validate single file in database. If valid returns `True` else `False`.
    :param api if `True` dont output to stdout
    """
    if not validation_task(config, file_id):
        if not api:
            log.error(f"File {file_id} is invalid!")
        return False
    else:
        if not api:
            log.info(f"File {file_id} validation successfull.")
        return True