from typing import Optional, List


class DiskFileVersioning:
    def __init__(self):
        self._mappings = {
            'groups': 'scan2report/plugins/groups.json',
            'profiles': 'scan2report/plugins/profiles.json',
            'aliases':  'scan2report/plugins/aliases.json',
        }

    def _file_name_to_path(self, filename: str) -> Optional[str]:
        return self._mappings.get(filename, None)

    def list_available_files(self) -> List[str]:
        return list(self._mappings.keys())

    def get_file_from_disk(self, filename: str) -> Optional[str]:
        filepath = self._file_name_to_path(filename)
        if filepath is None:
            return None

        # todo: use versioning in DB

        with open(filepath, encoding="utf8") as f:
            return f.read()

    def save_file_to_disk(self, filename: str, content: str):
        filepath = self._file_name_to_path(filename)
        if filepath is None:
            return None

        # todo: use versioning in DB

        with open(filepath, "w", encoding="utf8") as f:
            return f.write(content)
