# update.py
#
# Project: AutoArchive
# License: GNU GPLv3
#
# Copyright (C) 2003 - 2026 Róbert Čerňanský



from AutoArchive._infrastructure.app_dir.data_dir import DataDir
from AutoArchive._infrastructure.configuration import ConfigurationBase, Options
from AutoArchive._infrastructure.utils import OsPaths
from AutoArchive._ui.cmdline._cmdline_ui import CmdlineUi
from AutoArchive._infrastructure.configuration.config_dir import ConfigDir
from AutoArchive._update._file_system import _FileSystem
from AutoArchive._update._constants import _Constants
from AutoArchive._update._user_config_path_migrator import _UserConfigPathMigrator
from AutoArchive._update._snapshots_and_storage_migrator import _SnapshotsAndStorageMigrator



class Update:

    def __init__(self, configuration: ConfigurationBase, cmdlineUi: CmdlineUi):
        self.__configuration = configuration
        self.__cmdlineUi = cmdlineUi



    def migrate(self):
        fileSystem = _FileSystem()
        self.__migrateSnapshotsAndStorage(fileSystem)
        self.__migrateUserConfigPath(fileSystem)



    def __migrateSnapshotsAndStorage(self, fileSystem):
        if self.__configuration.isOptionPresent(Options.USER_CONFIG_DIR):
            dataDir = DataDir(self.__configuration[Options.USER_CONFIG_DIR])
        else:
            dataDir = DataDir(_Constants.OLD_FACTORY_USER_CONFIG_DIR)
        _SnapshotsAndStorageMigrator(fileSystem, dataDir, DataDir(OsPaths.getUserDataDir()), self.__cmdlineUi).migrate()



    def __migrateUserConfigPath(self, fileSystem):
        if self.__configuration.isOptionPresent(Options.USER_CONFIG_DIR):
            configDir = ConfigDir(self.__configuration[Options.USER_CONFIG_DIR])
            newConfigDir = configDir
        else:
            configDir = ConfigDir(_Constants.OLD_FACTORY_USER_CONFIG_DIR)
            newConfigDir = ConfigDir(self.__configuration[Options.USER_CONFIG_DIR])
        _UserConfigPathMigrator(fileSystem, configDir, newConfigDir, self.__cmdlineUi).migrate()
