#!/usr/bin/env python3
"""Force cancel stuck automation runs and clear cache locks"""
import os
import sys
import django

# Setup Django
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'igny8_core.settings')
django.setup()

from igny8_core.business.automation.models import AutomationRun
from django.core.cache import cache
from django.utils import timezone

print("=" * 80)
print("AUTOMATION RUN FORCE CANCEL & CLEANUP")
print("=" * 80)

# Check and cancel active runs
runs = AutomationRun.objects.filter(status__in=['running', 'paused']).order_by('-started_at')
print(f"\nFound {runs.count()} active run(s)")

if runs.count() == 0:
    print("  No runs to cancel\n")
else:
    for r in runs:
        duration = (timezone.now() - r.started_at).total_seconds() / 60
        print(f"\nRun ID: {r.run_id}")
        print(f"  Site: {r.site_id}")
        print(f"  Status: {r.status}")
        print(f"  Stage: {r.current_stage}")
        print(f"  Started: {r.started_at} ({duration:.1f}m ago)")
        print(f"  Credits: {r.total_credits_used}")
        
        # Force cancel
        print(f"  >>> FORCE CANCELLING...")
        r.status = 'cancelled'
        r.save()
        print(f"  >>> Status: {r.status}")
        
        # Clear cache lock
        lock_key = f'automation_lock_{r.site_id}'
        cache.delete(lock_key)
        print(f"  >>> Lock cleared: {lock_key}")

print("\n" + "=" * 40)
print("Cache lock status:")
for site_id in [5, 16]:
    lock_key = f'automation_lock_{site_id}'
    lock_val = cache.get(lock_key)
    status = lock_val or 'UNLOCKED ✓'
    print(f"  Site {site_id}: {status}")

print("\n" + "=" * 80)
print("✓ CLEANUP COMPLETE - You can now start a new automation run")
print("=" * 80)
