"""
Management command to set up initial registration fees
"""
from django.core.management.base import BaseCommand
from reports.enhanced_models import RegistrationFee
from decimal import Decimal


class Command(BaseCommand):
    help = 'Set up initial registration fees for all products'
    
    def handle(self, *args, **options):
        # Define initial registration fees
        initial_fees = [
            {
                'product_type': 'boost',
                'fee_name': 'Boost Registration Fee',
                'amount': Decimal('100.00'),
                'description': 'One-time registration fee for Boost loan product'
            },
            {
                'product_type': 'boost_plus',
                'fee_name': 'Boost Plus Registration Fee',
                'amount': Decimal('150.00'),
                'description': 'One-time registration fee for Boost Plus loan product'
            },
            {
                'product_type': 'mwamba',
                'fee_name': 'Mwamba Registration Fee',
                'amount': Decimal('200.00'),
                'description': 'One-time registration fee for Mwamba loan product'
            },
            {
                'product_type': 'imara',
                'fee_name': 'Imara Registration Fee',
                'amount': Decimal('250.00'),
                'description': 'One-time registration fee for Imara loan product'
            },
            {
                'product_type': 'account_opening',
                'fee_name': 'Account Opening Fee',
                'amount': Decimal('50.00'),
                'description': 'Fee for opening a new customer account'
            },
            {
                'product_type': 'document_processing',
                'fee_name': 'Document Processing Fee',
                'amount': Decimal('75.00'),
                'description': 'Fee for processing and verifying customer documents'
            },
            {
                'product_type': 'statement_request',
                'fee_name': 'Statement Request Fee',
                'amount': Decimal('25.00'),
                'description': 'Fee for generating and sending loan statements'
            },
            {
                'product_type': 'certificate',
                'fee_name': 'Loan Certificate Fee',
                'amount': Decimal('100.00'),
                'description': 'Fee for issuing loan completion certificates'
            },
        ]
        
        created_count = 0
        updated_count = 0
        
        for fee_data in initial_fees:
            fee, created = RegistrationFee.objects.get_or_create(
                product_type=fee_data['product_type'],
                fee_name=fee_data['fee_name'],
                defaults={
                    'amount': fee_data['amount'],
                    'description': fee_data['description'],
                    'is_active': True
                }
            )
            
            if created:
                created_count += 1
                self.stdout.write(
                    self.style.SUCCESS(f'Created: {fee.fee_name} - KES {fee.amount}')
                )
            else:
                # Update existing fee if amount is different
                if fee.amount != fee_data['amount']:
                    fee.amount = fee_data['amount']
                    fee.description = fee_data['description']
                    fee.save()
                    updated_count += 1
                    self.stdout.write(
                        self.style.WARNING(f'Updated: {fee.fee_name} - KES {fee.amount}')
                    )
                else:
                    self.stdout.write(
                        self.style.SUCCESS(f'Exists: {fee.fee_name} - KES {fee.amount}')
                    )
        
        self.stdout.write(
            self.style.SUCCESS(
                f'\nSetup complete! Created {created_count} new fees, updated {updated_count} existing fees.'
            )
        )