import logging
from typing import List
from archivingsystem.database import Database
from archivingsystem.database import ArchivedFile

log = logging.getLogger(__name__)

def all(db: Database, api: bool=False) -> List[ArchivedFile]:
    """
    List all archived files from database.
    :param api if `True` dont output to stdout
    """
    s = db.Session()
    files = s.query(ArchivedFile).all()

    if not api:
        print(f'\n# Archived files: ({len(files)})')
        print("ID – Owner - Archiv. date – Filename")
        for file in files:
            print(f'{file.FileID} – {file.OwnerName} – {file.TimeOfFirstTS} – {file.FileName}')
        print('')
    
    s.close()

    return files
    
        

def single(db: Database, file_id: int):
    """
    List single file from database with details.
    """
    s = db.Session()
    file = s.query(ArchivedFile).filter_by(FileID = file_id).first()
    
    print(f'\n# Archived file: {file.FileName}')
    print(f"\
            FileID: {file.FileID}\n\
            FileName: {file.FileName}\n\
            OwnerName: {file.OwnerName}\n\
            PackageStoragePath: {file.PackageStoragePath}\n\
            TimeOfFirstTS: {file.TimeOfFirstTS}\n\
            ExpirationDateTS: {file.ExpirationDateTS}\n\
            ")
    print('')

    s.close()

    return file