import base64

from core.logger import DBLogger
from data.abc.data_seeder_abc import DataSeederABC
from data.schemas.system.file_dao import fileDao
from service.file_service import FileService

logger = DBLogger(__name__)


class FileHashSeeder(DataSeederABC):
    def __init__(self):
        DataSeederABC.__init__(self)

    async def seed(self):
        files = await fileDao.get_all()

        for file in files:
            if file.file_hash is not None and file.file_hash != "":
                continue

            logger.info(f"Calculating hash for file: {file.full_path}")
            file.file_hash = FileService.get_hash(
                base64.b64encode(FileService.get_file(file.full_path))
            )
            await fileDao.update(file)
