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



import os.path
from typing import override

from AutoArchive._infrastructure.app_dir.app_dir import AppDir
from AutoArchive._ui.cmdline._cmdline_ui import CmdlineUi
from AutoArchive._update._file_system import _FileSystem
from AutoArchive._update._user_dir_migrator_base import _UserDirMigratorBase



class _SnapshotsAndStorageMigrator(_UserDirMigratorBase):

    def __init__(self, fileSystem: _FileSystem, oldDataDir: AppDir, newDataDir: AppDir, cmdlineUi: CmdlineUi):
        super().__init__(fileSystem, oldDataDir, newDataDir, cmdlineUi)



    @property
    def dirName_(self) -> str:
        return "user data"



    @override
    def moveContent_(self, oldUserDir: AppDir, newUserDir: AppDir):
        self.fileSystem_.moveWithBackup(
            self.__getOldStorageDir(), os.path.join(newUserDir.path, "storage"), self.cmdLineUi_)
        self.fileSystem_.moveWithBackup(
            self.__getOldSnapshotsDir(), os.path.join(newUserDir.path, "snapshots"), self.cmdLineUi_)



    @override
    def isOldUserDirExisting_(self) -> bool:
        return self.fileSystem_.exists(self.__getOldStorageDir()) or \
            self.fileSystem_.exists(self.__getOldSnapshotsDir())



    # SMELL: String 'storage' is a duplicate; it shall be retrieved from Storage component.
    def __getOldStorageDir(self):
        return os.path.join(self.oldUserDir_.path, "storage")



    # SMELL: String 'snapshots' is a duplicate; it shall be retrieved from Archiver component.
    def __getOldSnapshotsDir(self):
        return os.path.join(self.oldUserDir_.path, "snapshots")
