# Copyright (C) 2016-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 os
import random
import tempfile
import string

import pytest

from rhodecode.apps.file_store import utils as store_utils


@pytest.fixture()
def file_store_instance(ini_settings):
    config = ini_settings
    f_store = store_utils.get_filestore_backend(config=config, always_init=True)
    return f_store


@pytest.fixture
def random_binary_file():
    # Generate random binary data
    data = bytearray(random.getrandbits(8) for _ in range(1024 * 512))  # 512 KB of random data

    # Create a temporary file
    temp_file = tempfile.NamedTemporaryFile(delete=False)
    filename = temp_file.name

    try:
        # Write the random binary data to the file
        temp_file.write(data)
        temp_file.seek(0)  # Rewind the file pointer to the beginning
        yield filename, temp_file
    finally:
        # Close and delete the temporary file after the test
        temp_file.close()
        os.remove(filename)


def generate_random_filename(length=10):
    return "".join(random.choices(string.ascii_letters + string.digits, k=length))
