# Copyright (C) 2015-2024 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3
# (only), as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# This program is dual-licensed. If you wish to learn more about the
# RhodeCode Enterprise Edition, including its added features, Support services,
# and proprietary license terms, please see https://rhodecode.com/licenses/

import logging

from .backends.fanout_cache import FileSystemFanoutCache
from .backends.objectstore_cache import ObjectStoreCache

from .utils import archive_iterator  # noqa
from .lock import ArchiveCacheGenerationLock  # noqa

log = logging.getLogger(__name__)


cache_meta = None


def includeme(config):
    return  # don't init cache currently for faster startup time

    # init our cache at start
    # settings = config.get_settings()
    # get_archival_cache_store(settings)


def get_archival_config(config):
    final_config = {}

    for k, v in config.items():
        if k.startswith("archive_cache"):
            final_config[k] = v

    return final_config


def get_archival_cache_store(config, always_init=False):
    global cache_meta
    if cache_meta is not None and not always_init:
        return cache_meta

    config = get_archival_config(config)
    backend = config["archive_cache.backend.type"]

    archive_cache_locking_url = config["archive_cache.locking.url"]

    match backend:
        case "filesystem":
            d_cache = FileSystemFanoutCache(locking_url=archive_cache_locking_url, **config)
        case "objectstore":
            d_cache = ObjectStoreCache(locking_url=archive_cache_locking_url, **config)
        case _:
            raise ValueError(f'archive_cache.backend.type only supports "filesystem" or "objectstore" got {backend} ')

    cache_meta = d_cache
    return cache_meta
