from django.core.management.base import BaseCommand
from loans.models import LoanProduct
from utils.models import SystemSetting


class Command(BaseCommand):
    help = 'Set up default loan products (Boost, Mwamba, Imara)'

    def handle(self, *args, **options):
        self.stdout.write('Setting up loan products...')
        
        # Clear existing loan products
        LoanProduct.objects.all().delete()
        self.stdout.write('Cleared existing loan products.')
        
      
        # Create Mwamba product (3 months, 20% per month, 5% processing fee)
      
        # Create Boost Plus product (30, 60, or 90 days, 20% per month, 5% processing fee monthly)
        boost_plus_product = LoanProduct.objects.create(
            name='Boost Plus',
            product_type='boost_plus',
            description='Flexible loan with 30, 60, or 90-day terms. Processing fee charged monthly like interest.',
            min_amount=1000,
            max_amount=50000,
            interest_rate=20.0,  # 20% per month
            processing_fee=5.0,  # 5% processing fee (charged monthly)
            late_payment_penalty=5.0,
            duration_months=1,
            min_duration=30,
            max_duration=90,
            available_repayment_methods=['daily', 'weekly', 'monthly'],
            requires_guarantor=False,
            requires_collateral=False,
            is_active=True
        )
        
        # Create Imara product (6 months, 10% per month, 5% processing fee)
        
        # Set up system settings for loan products
        self.setup_loan_product_settings()
        
        self.stdout.write(
            self.style.SUCCESS(
                f'Successfully created {LoanProduct.objects.count()} loan products:\n'
                f'- Boost (1 month, 20% interest, 5% processing)\n'
                f'- Boost Plus (30-90 days, 20% interest, 5% processing monthly)\n'
                f'- Mwamba (3 months, 20% interest, 5% processing)\n'
                f'- Imara (6 months, 10% interest, 5% processing)'
            )
        )
    
    def setup_loan_product_settings(self):
        """Set up system settings for loan products"""
        settings_data = [
           
            # Boost Plus Product Settings
            ('boost_plus_duration_months', '1', 'loan', 'Boost Plus loan duration in months'),
            ('boost_plus_interest_rate', '20.0', 'loan', 'Boost Plus monthly interest rate (%)'),
            ('boost_plus_processing_fee', '5.0', 'loan', 'Boost Plus processing fee (%) - charged monthly'),
            ('boost_plus_late_payment_penalty', '5.0', 'loan', 'Boost Plus daily penalty rate for late payments (%)'),
            ('boost_plus_repayment_methods', '["daily", "weekly", "monthly"]', 'loan', 'Boost Plus available repayment methods'),
            ('boost_plus_min_amount', '1000', 'loan', 'Boost Plus minimum loan amount'),
            ('boost_plus_max_amount', '50000', 'loan', 'Boost Plus maximum loan amount'),
            
        ]
            
        for key, value, category, description in settings_data:
            SystemSetting.objects.update_or_create(
                key=key,
                defaults={
                    'value': value,
                    'category': category,
                    'description': description
                }
            )
        
        self.stdout.write('Created loan product system settings.') 