from django.core.management.base import BaseCommand
from nomazra.models import LawEntry
from nomazra.law_entries_data import LAW_ENTRIES


class Command(BaseCommand):
    help = 'Seed LawEntry table with foundational laws and statutes'

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

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

        created = 0
        skipped = 0
        for entry in LAW_ENTRIES:
            _, was_created = LawEntry.objects.get_or_create(
                country=entry['country'],
                area=entry['area'],
                title=entry['title'],
                defaults={
                    'full_text': entry['full_text'],
                    'summary': entry['summary'],
                    'effective_date': entry.get('effective_date'),
                    'last_amended': entry.get('last_amended'),
                    'tags': entry.get('tags', []),
                    'source_url': entry.get('source_url', ''),
                    'is_active': True,
                }
            )
            if was_created:
                created += 1
            else:
                skipped += 1

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