# Generated by Django 4.2.24 on 2025-11-14 02:52

import logging

from django.db import migrations

logger = logging.getLogger(__name__)

# Default badges to create on installation
DEFAULT_BADGES = [
    # First Post Badge
    {
        "name": "First Steps",
        "description": "Awarded for posting your first message",
        "icon": "message-circle",
        "color": "#10b981",  # green
        "badge_type": "GLOBAL",
        "is_auto_awarded": True,
        "criteria_config": {"type": "first_post"},
    },
    # Message Count Badges
    {
        "name": "Conversationalist",
        "description": "Posted 10 messages",
        "icon": "message-square",
        "color": "#3b82f6",  # blue
        "badge_type": "GLOBAL",
        "is_auto_awarded": True,
        "criteria_config": {"type": "message_count", "value": 10},
    },
    {
        "name": "Active Contributor",
        "description": "Posted 50 messages",
        "icon": "messages-square",
        "color": "#8b5cf6",  # purple
        "badge_type": "GLOBAL",
        "is_auto_awarded": True,
        "criteria_config": {"type": "message_count", "value": 50},
    },
    {
        "name": "Discussion Leader",
        "description": "Posted 100 messages",
        "icon": "megaphone",
        "color": "#ec4899",  # pink
        "badge_type": "GLOBAL",
        "is_auto_awarded": True,
        "criteria_config": {"type": "message_count", "value": 100},
    },
    # Reputation Badges
    {
        "name": "Helpful",
        "description": "Earned 10 reputation points",
        "icon": "thumbs-up",
        "color": "#22c55e",  # green
        "badge_type": "GLOBAL",
        "is_auto_awarded": True,
        "criteria_config": {"type": "reputation_threshold", "value": 10},
    },
    {
        "name": "Valued Contributor",
        "description": "Earned 50 reputation points",
        "icon": "star",
        "color": "#eab308",  # yellow
        "badge_type": "GLOBAL",
        "is_auto_awarded": True,
        "criteria_config": {"type": "reputation_threshold", "value": 50},
    },
    {
        "name": "Expert",
        "description": "Earned 100 reputation points",
        "icon": "award",
        "color": "#f97316",  # orange
        "badge_type": "GLOBAL",
        "is_auto_awarded": True,
        "criteria_config": {"type": "reputation_threshold", "value": 100},
    },
    {
        "name": "Master",
        "description": "Earned 500 reputation points",
        "icon": "trophy",
        "color": "#ef4444",  # red
        "badge_type": "GLOBAL",
        "is_auto_awarded": True,
        "criteria_config": {"type": "reputation_threshold", "value": 500},
    },
    # Message Upvotes Badge
    {
        "name": "Popular Post",
        "description": "Received 10 upvotes on a single message",
        "icon": "heart",
        "color": "#f59e0b",  # amber
        "badge_type": "GLOBAL",
        "is_auto_awarded": True,
        "criteria_config": {"type": "message_upvotes", "value": 10},
    },
]


def install_default_badges(apps, schema_editor):
    """Create default global badges for the platform."""
    Badge = apps.get_model("badges", "Badge")
    User = apps.get_model("users", "User")

    # Get or create a system user for badge creation
    system_user, _ = User.objects.get_or_create(
        username="system",
        defaults={
            "email": "system@opencontracts.local",
            "is_active": False,  # System user should not be able to login
        },
    )

    created_count = 0
    existing_count = 0

    for badge_data in DEFAULT_BADGES:
        badge, created = Badge.objects.get_or_create(
            name=badge_data["name"],
            badge_type=badge_data["badge_type"],
            defaults={
                "description": badge_data["description"],
                "icon": badge_data["icon"],
                "color": badge_data["color"],
                "is_auto_awarded": badge_data["is_auto_awarded"],
                "criteria_config": badge_data["criteria_config"],
                "creator": system_user,
            },
        )

        if created:
            created_count += 1
            logger.info(f"Created default badge: {badge.name}")
        else:
            existing_count += 1
            logger.info(f"Badge already exists: {badge.name}")

    logger.info(
        f"Default badges installation complete. Created: {created_count}, Already existed: {existing_count}"
    )


def remove_default_badges(apps, schema_editor):
    """Remove default badges (for migration rollback)."""
    Badge = apps.get_model("badges", "Badge")

    deleted_count = 0

    for badge_data in DEFAULT_BADGES:
        deleted, _ = Badge.objects.filter(
            name=badge_data["name"], badge_type=badge_data["badge_type"]
        ).delete()

        if deleted:
            deleted_count += deleted
            logger.info(f"Removed default badge: {badge_data['name']}")

    logger.info(f"Removed {deleted_count} default badges")


class Migration(migrations.Migration):

    dependencies = [
        ("badges", "0004_alter_badge_options"),
    ]

    operations = [
        migrations.RunPython(install_default_badges, remove_default_badges),
    ]
