from django.core.management.base import BaseCommand
from django.db import transaction
from datetime import date
from stiftung.models import VierteljahresNachweis


class Command(BaseCommand):
    help = 'Update quarterly confirmation deadlines to semester-based system'

    def add_arguments(self, parser):
        parser.add_argument(
            '--dry-run',
            action='store_true',
            help='Show what would be updated without making changes',
        )
        parser.add_argument(
            '--year',
            type=int,
            help='Only update records for specific year (default: all years)',
        )

    def handle(self, *args, **options):
        dry_run = options['dry_run']
        year_filter = options['year']
        
        if dry_run:
            self.stdout.write(
                self.style.WARNING('DRY RUN MODE - No changes will be made')
            )
        
        # Filter queryset based on year if provided
        queryset = VierteljahresNachweis.objects.all()
        if year_filter:
            queryset = queryset.filter(jahr=year_filter)
            self.stdout.write(f'Filtering records for year: {year_filter}')
        
        updated_count = 0
        total_count = queryset.count()
        
        self.stdout.write(f'Processing {total_count} quarterly confirmation records...')
        
        with transaction.atomic():
            for nachweis in queryset:
                # Calculate new semester-based deadlines
                quarter_deadlines = {
                    1: date(nachweis.jahr, 3, 15),   # Q1 deadline: March 15 (Spring semester)
                    2: date(nachweis.jahr, 6, 15),   # Q2 deadline: June 15 (auto-approved)
                    3: date(nachweis.jahr, 9, 15),   # Q3 deadline: September 15 (Fall semester)
                    4: date(nachweis.jahr, 12, 15),  # Q4 deadline: December 15 (auto-approved)
                }
                
                new_deadline = quarter_deadlines.get(nachweis.quartal)
                
                if new_deadline and nachweis.faelligkeitsdatum != new_deadline:
                    old_deadline = nachweis.faelligkeitsdatum
                    
                    if not dry_run:
                        nachweis.faelligkeitsdatum = new_deadline
                        nachweis.save(update_fields=['faelligkeitsdatum'])
                    
                    updated_count += 1
                    
                    # Show progress for every 10 updates or if verbose
                    if updated_count % 10 == 0 or options['verbosity'] >= 2:
                        self.stdout.write(
                            f'  {nachweis.destinataer.get_full_name()}: '
                            f'{nachweis.jahr} Q{nachweis.quartal}: '
                            f'{old_deadline} → {new_deadline}'
                        )
        
        # Summary
        if dry_run:
            self.stdout.write(
                self.style.SUCCESS(
                    f'DRY RUN: Would update {updated_count} out of {total_count} records'
                )
            )
        else:
            self.stdout.write(
                self.style.SUCCESS(
                    f'Successfully updated {updated_count} out of {total_count} records'
                )
            )
        
        if updated_count == 0:
            self.stdout.write(
                self.style.WARNING('No records needed updating - all deadlines already correct')
            )
        else:
            # Show the new deadline structure
            self.stdout.write('\nNew semester-based deadline structure:')
            self.stdout.write('  Q1: March 15   (Primary semester submission)')
            self.stdout.write('  Q2: June 15    (Auto-approved when Q1 approved)')
            self.stdout.write('  Q3: September 15 (Primary semester submission)')
            self.stdout.write('  Q4: December 15  (Auto-approved when Q3 approved)')