from django.core.management.base import BaseCommand
from nomazra.models import RightsCard
from nomazra.rights_cards_data import RIGHTS_CARDS


class Command(BaseCommand):
    help = 'Seed RightsCard table with pre-built know-your-rights cards'

    def add_arguments(self, parser):
        parser.add_argument('--clear', action='store_true', help='Clear existing cards before seeding')

    def handle(self, *args, **options):
        if options['clear']:
            count = RightsCard.objects.count()
            RightsCard.objects.all().delete()
            self.stdout.write(self.style.WARNING(f'Deleted {count} existing rights cards'))

        created = 0
        skipped = 0
        for card in RIGHTS_CARDS:
            _, was_created = RightsCard.objects.get_or_create(
                country=card['country'],
                category=card['category'],
                title=card['title'],
                defaults={
                    'content': card['content'],
                    'citations': card.get('citations', []),
                    'tags': card.get('tags', []),
                    'is_active': True,
                }
            )
            if was_created:
                created += 1
            else:
                skipped += 1

        self.stdout.write(self.style.SUCCESS(
            f'Rights cards seeded: {created} created, {skipped} already existed'
        ))
