# Copyright (c) 2024 CloudZero - ALL RIGHTS RESERVED - PROPRIETARY AND CONFIDENTIAL
# Unauthorized copying of this file and/or project, via any medium is strictly prohibited.
# Direct all questions to legal@cloudzero.com

from uuid import uuid4

from czpulumi.sumo_logic.folders import get_personal_folder
from czpulumi.sumo_logic.sumo_client import delete_content, get_sumo_client


class TemporarySumoFolder:
    def __init__(self):
        self.client = get_sumo_client()
        self.uuid = str(uuid4())[:8]

    def __enter__(self):
        personal_folder_id = get_personal_folder()['id']
        res = self.client.create_folder(
            name=f'temp_folder_{self.uuid}',
            description='Temporary folder. This should be automatically cleaned up.',
            parent_folder_id=personal_folder_id,
        ).json()
        self.folder_id = res['id']
        return res

    def __exit__(self, _exc_type, _exc_val, _exc_tb):
        delete_content(content_id=self.folder_id)
