#!/usr/bin/env python
"""Verify all status fixes"""
import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'igny8_core.settings')
django.setup()

from igny8_core.business.planning.models import Keywords, Clusters, ContentIdeas
from igny8_core.business.content.models import Tasks
from django.db.models import Count

print("=" * 60)
print("STATUS VERIFICATION REPORT")
print("=" * 60)

# Keywords
print("\n1. KEYWORDS STATUS:")
kw_status = Keywords.objects.values('status').annotate(count=Count('id')).order_by('status')
for item in kw_status:
    print(f"   {item['status']}: {item['count']}")
print(f"   Total: {Keywords.objects.count()}")

# Clusters
print("\n2. CLUSTERS STATUS:")
cl_status = Clusters.objects.values('status').annotate(count=Count('id')).order_by('status')
for item in cl_status:
    print(f"   {item['status']}: {item['count']}")
print(f"   Total: {Clusters.objects.count()}")

# Content Ideas
print("\n3. IDEAS STATUS:")
idea_status = ContentIdeas.objects.values('status').annotate(count=Count('id')).order_by('status')
for item in idea_status:
    print(f"   {item['status']}: {item['count']}")
print(f"   Total: {ContentIdeas.objects.count()}")

# Verify idea-task sync
print("\n4. IDEA-TASK STATUS SYNC:")
completed_tasks = Tasks.objects.filter(status='completed', idea__isnull=False)
mismatched = 0
for task in completed_tasks:
    if task.idea and task.idea.status != 'completed':
        mismatched += 1
        print(f"   MISMATCH: Task {task.id} completed, Idea {task.idea.id} is {task.idea.status}")

if mismatched == 0:
    print(f"   ✓ All {completed_tasks.count()} completed tasks have ideas with 'completed' status")
else:
    print(f"   ✗ {mismatched} mismatches found")

# Check for old status values
print("\n5. OLD STATUS VALUES CHECK:")
old_keywords = Keywords.objects.filter(status__in=['pending', 'active', 'archived']).count()
old_clusters = Clusters.objects.filter(status__in=['active']).exclude(status='mapped').exclude(status='new').count()
old_ideas = ContentIdeas.objects.filter(status__in=['scheduled', 'published']).count()

if old_keywords == 0 and old_clusters == 0 and old_ideas == 0:
    print("   ✓ No old status values found")
else:
    print(f"   ✗ Found old values:")
    if old_keywords > 0:
        print(f"      Keywords with pending/active/archived: {old_keywords}")
    if old_clusters > 0:
        print(f"      Clusters with old 'active': {old_clusters}")
    if old_ideas > 0:
        print(f"      Ideas with scheduled/published: {old_ideas}")

print("\n" + "=" * 60)
print("VERIFICATION COMPLETE")
print("=" * 60)
