#!/usr/bin/env python3
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from utils.backup import create_backup_with_github, create_server_backup
from datetime import datetime
import logging

# Setup logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler('/opt/v2-Docker/logs/scheduled_backup.log'),
        logging.StreamHandler()
    ]
)

logger = logging.getLogger(__name__)

def run_scheduled_backup():
    """Run scheduled daily backups"""
    logger.info("Starting scheduled backup process")
    
    # Create database backup
    logger.info("Creating database backup...")
    db_success, db_result = create_backup_with_github(
        backup_type="scheduled",
        created_by="cron",
        push_to_github=True,
        delete_local=True
    )
    
    if db_success:
        logger.info(f"Database backup successful: {db_result}")
    else:
        logger.error(f"Database backup failed: {db_result}")
    
    # Create server backup
    logger.info("Creating server backup...")
    server_success, server_result = create_server_backup(
        created_by="cron",
        push_to_github=True,
        delete_local=True
    )
    
    if server_success:
        logger.info(f"Server backup successful: {server_result}")
    else:
        logger.error(f"Server backup failed: {server_result}")
    
    logger.info("Scheduled backup process completed")
    return db_success and server_success

if __name__ == "__main__":
    success = run_scheduled_backup()
    sys.exit(0 if success else 1)