from typing import Union
from uuid import uuid4

import inject
from sqlmodel import Session, select

from src.core.events import EventDescription
from src.core.models import Context, File
from src.core.services import Storage, Streamer
from src.utils.miscellaneous import get_file_hash


@inject.params(streamer=Streamer, storage=Storage)
def get_or_create_file(
    session: Session,
    preffix: str,
    filename: str,
    file: Union[str, bytes],
    context: Context,
    streamer: Streamer,
    storage: Storage,
) -> File:
    file_hash = get_file_hash(file)
    file_obj = session.exec(select(File).where(File.hash == file_hash)).first()
    extension = filename.split(".")[-1]

    if not file_obj:
        streamer.send_event(
            EventDescription.UPLOAD_FILE, context=context, file={"filename": filename, "hash": file_hash}
        )
        file_obj = File(bucket_key=f"{preffix}-{uuid4()}.{extension}", hash=file_hash)
        session.add(file_obj)

    if not storage.check_file_exists(file_obj.bucket_key):
        storage.upload_file(file, key=file_obj.bucket_key)
        streamer.send_event(
            EventDescription.UPLOAD_FILE,
            context=context,
            file={"filename": filename, "hash": file_hash, "bucket_key": file_obj.bucket_key},
        )

    return file_obj


@inject.params(storage=Storage)
def check_file_exists(bucket_key: str, storage: Storage) -> bool:
    return storage.check_file_exists(bucket_key)

uuid.uuid4 sqlmodel.select inject.params src.utils.miscellaneous.get_file_hash
[((289, 338), 'inject.params', 'inject.params', ([], {'streamer': 'Streamer', 'storage': 'Storage'}), '(streamer=Streamer, storage=Storage)\n', (302, 338), False, 'import inject\n'), ((1318, 1348), 'inject.params', 'inject.params', ([], {'storage': 'Storage'}), '(storage=Storage)\n', (1331, 1348), False, 'import inject\n'), ((546, 565), 'src.utils.miscellaneous.get_file_hash', 'get_file_hash', (['file'], {}), '(file)\n', (559, 565), False, 'from src.utils.miscellaneous import get_file_hash\n'), ((594, 606), 'sqlmodel.select', 'select', (['File'], {}), '(File)\n', (600, 606), False, 'from sqlmodel import Session, select\n'), ((901, 908), 'uuid.uuid4', 'uuid4', ([], {}), '()\n', (906, 908), False, 'from uuid import uuid4\n')]