# services/external/app/jobs/rotate_data.py
"""
Kubernetes CronJob - Monthly data rotation (24-month window)
"""

import asyncio
import argparse
import sys
import logging
import structlog

from app.ingestion.ingestion_manager import DataIngestionManager
from app.core.database import database_manager

logger = structlog.get_logger()


async def main():
    """Rotate 24-month data window"""
    logger.info("Starting monthly data rotation job")

    try:
        manager = DataIngestionManager()
        await manager.rotate_monthly_data()

        logger.info("✅ Data rotation completed successfully")
        sys.exit(0)

    except Exception as e:
        logger.error("❌ Fatal error during rotation", error=str(e))
        sys.exit(1)
    finally:
        await database_manager.close_connections()


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Rotate historical data")
    parser.add_argument("--log-level", default="INFO", help="Log level")
    parser.add_argument("--notify-slack", type=bool, default=False, help="Send Slack notification")

    args = parser.parse_args()

    # Convert string log level to logging constant
    log_level = getattr(logging, args.log_level.upper(), logging.INFO)

    structlog.configure(
        wrapper_class=structlog.make_filtering_bound_logger(log_level)
    )

    asyncio.run(main())
