import modal

from ..ffmpeg_app import ffmpeg_worker_image, app, config

with ffmpeg_worker_image.imports():
    import os, time
    from datetime import datetime
    from loguru import logger
    import shutil
    from typing import Tuple, List

    hls_recording_volume = modal.Volume.from_name("stream_records", create_if_missing=True)
    hls_recording_mount_point = "/mnt/stream_records"


    # runs daily at 3 am (Hong Kong time)
    @app.function(
        volumes={hls_recording_mount_point: hls_recording_volume},
        schedule=modal.Cron("0 3 * * *", timezone="Asia/Hong_Kong")
    )
    def storage_cleanup():
        output_dir = f"{config.modal_environment}/records/hls/"
        volume_output_dir = f"{hls_recording_mount_point}/{output_dir}"
        now = time.time()
        cutoff = now - 24 * 60 * 60  # 24小时之前的时间戳
        new_folders: List[Tuple[str, datetime]] = []
        old_folders: List[Tuple[str, datetime]] = []
        for entry in os.scandir(volume_output_dir):
            if entry.is_dir():
                mtime = entry.stat().st_mtime
                if mtime < cutoff:
                    old_folders.append((entry.path, datetime.fromtimestamp(mtime)))
                else:
                    new_folders.append((entry.path, datetime.fromtimestamp(mtime)))

        for folder, timestamp in old_folders:
            logger.info(f"Removing {folder}, last modified {timestamp.isoformat()}")
            shutil.rmtree(folder)
        logger.info(f"{len(old_folders)} tasks cleaned up, {len(new_folders)} tasks remaining")
