from django.core.management.base import BaseCommand
from nomazra.models import GlossaryTerm
from nomazra.glossary_data import GLOSSARY_TERMS


class Command(BaseCommand):
    help = 'Seed GlossaryTerm table from hardcoded glossary data'

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

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

        created = 0
        skipped = 0
        for term_data in GLOSSARY_TERMS:
            _, was_created = GlossaryTerm.objects.get_or_create(
                term=term_data['term'],
                defaults={
                    'definition': term_data.get('definition', ''),
                    'plain_language': term_data.get('plain_language', ''),
                    'country': term_data.get('country', ''),
                    'area': term_data.get('area', ''),
                    'related_terms': term_data.get('related_terms', []),
                }
            )
            if was_created:
                created += 1
            else:
                skipped += 1

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