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 Boost product (1 month, 20% per month, 5% processing fee)
        boost_product = LoanProduct.objects.create(
            name='Boost',
            product_type='boost',
            description='Quick 1-month loan with flexible repayment options. Perfect for short-term financial needs.',
            min_amount=1000,
            max_amount=50000,
            interest_rate=20.0,  # 20% per month
            processing_fee=5.0,  # 5% processing fee
            late_payment_penalty=5.0,
            duration_months=1,
            min_duration=30,
            max_duration=30,
            available_repayment_methods=['daily', 'weekly', 'monthly'],
            requires_guarantor=False,
            requires_collateral=False,
            is_active=True
        )
        
        # Create Mwamba product (3 months, 20% per month, 5% processing fee)
        mwamba_product = LoanProduct.objects.create(
            name='Mwamba',
            product_type='mwamba',
            description='3-month loan with weekly or monthly repayment options. Ideal for medium-term financing.',
            min_amount=5000,
            max_amount=200000,
            interest_rate=20.0,  # 20% per month
            processing_fee=5.0,  # 5% processing fee
            late_payment_penalty=5.0,
            duration_months=3,
            min_duration=90,
            max_duration=90,
            available_repayment_methods=['weekly', 'monthly'],
            requires_guarantor=False,
            requires_collateral=False,
            is_active=True
        )
        
        # Create Imara product (6 months, 10% per month, 5% processing fee)
        imara_product = LoanProduct.objects.create(
            name='Imara',
            product_type='imara',
            description='6-month loan with monthly repayment. Perfect for long-term financial planning.',
            min_amount=10000,
            max_amount=500000,
            interest_rate=10.0,  # 10% per month
            processing_fee=5.0,  # 5% processing fee
            late_payment_penalty=5.0,
            duration_months=6,
            min_duration=180,
            max_duration=180,
            available_repayment_methods=['monthly'],
            requires_guarantor=False,
            requires_collateral=False,
            is_active=True
        )
        
        # 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'- 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 Product Settings
            ('boost_duration_months', '1', 'loan', 'Boost loan duration in months'),
            ('boost_interest_rate', '20.0', 'loan', 'Boost monthly interest rate (%)'),
            ('boost_processing_fee', '5.0', 'loan', 'Boost processing fee (%)'),
            ('boost_repayment_methods', '["daily", "weekly", "monthly"]', 'loan', 'Boost available repayment methods'),
            ('boost_min_amount', '1000', 'loan', 'Boost minimum loan amount'),
            ('boost_max_amount', '50000', 'loan', 'Boost maximum loan amount'),
            
            # Mwamba Product Settings
            ('mwamba_duration_months', '3', 'loan', 'Mwamba loan duration in months'),
            ('mwamba_interest_rate', '20.0', 'loan', 'Mwamba monthly interest rate (%)'),
            ('mwamba_processing_fee', '5.0', 'loan', 'Mwamba processing fee (%)'),
            ('mwamba_repayment_methods', '["weekly", "monthly"]', 'loan', 'Mwamba available repayment methods'),
            ('mwamba_min_amount', '5000', 'loan', 'Mwamba minimum loan amount'),
            ('mwamba_max_amount', '200000', 'loan', 'Mwamba maximum loan amount'),
            
            # Imara Product Settings
            ('imara_duration_months', '6', 'loan', 'Imara loan duration in months'),
            ('imara_interest_rate', '10.0', 'loan', 'Imara monthly interest rate (%)'),
            ('imara_processing_fee', '5.0', 'loan', 'Imara processing fee (%)'),
            ('imara_repayment_methods', '["monthly"]', 'loan', 'Imara available repayment methods'),
            ('imara_min_amount', '10000', 'loan', 'Imara minimum loan amount'),
            ('imara_max_amount', '500000', 'loan', 'Imara 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.') 