import modal

from ..video import downloader_image, app, config

with downloader_image.imports():
    import os
    from BowongModalFunctions.models.media_model import MediaSource, MediaCacheStatus


    @app.function(cpu=1, timeout=300,
                  max_containers=config.video_downloader_concurrency,
                  volumes={
                      "/mntS3": modal.CloudBucketMount(
                          bucket_name=config.S3_bucket_name,
                          secret=modal.Secret.from_name("aws-s3-secret", environment_name=config.modal_environment),
                      ),
                  })
    @modal.concurrent(max_inputs=10)
    async def cache_delete(cache: MediaSource) -> MediaSource:
        if os.path.exists(cache.cache_filepath):
            os.remove(cache.cache_filepath)
            cache.status = MediaCacheStatus.deleted
        else:
            cache.status = MediaCacheStatus.missing
        return cache
