from django.core.management.base import BaseCommand
from utils.models import SystemSetting

class Command(BaseCommand):
    help = 'Create default system settings'

    def handle(self, *args, **options):
        self.stdout.write('Creating default settings...')

        # Define default settings
        settings_data = [
            # Boost Product Settings
            ('boost_interest_rate', '20.0', 'loan', 'Monthly interest rate for Boost loans (%)'),
            ('boost_processing_fee', '5.0', 'loan', 'Processing fee for Boost loans (%)'),
            ('boost_late_payment_penalty', '5.0', 'loan', 'Monthly penalty rate for late Boost loan payments (%)'),
            ('boost_min_amount', '1000', 'loan', 'Minimum amount for Boost loans'),
            ('boost_max_amount', '50000', 'loan', 'Maximum amount for Boost loans'),
            ('boost_duration_months', '1', 'loan', 'Duration in months for Boost loans'),
            ('boost_repayment_methods', '["daily", "weekly", "monthly"]', 'loan', 'Available repayment methods for Boost loans'),


            # Boost Plus Product Settings
            ('boost_plus_interest_rate', '20.0', 'loan', 'Monthly interest rate for Boost Plus loans (%)'),
            ('boost_plus_processing_fee', '5.0', 'loan', 'Processing fee for Boost Plus loans (%) - charged monthly'),
            ('boost_plus_late_payment_penalty', '5.0', 'loan', 'Monthly penalty rate for late Boost Plus loan payments (%)'),
            ('boost_plus_min_amount', '1000', 'loan', 'Minimum amount for Boost Plus loans'),
            ('boost_plus_max_amount', '50000', 'loan', 'Maximum amount for Boost Plus loans'),
            ('boost_plus_duration_months', '1', 'loan', 'Duration in months for Boost Plus loans'),
            ('boost_plus_repayment_methods', '["daily", "weekly", "monthly"]', 'loan', 'Available repayment methods for Boost Plus loans'),


            # Mwamba Product Settings
            ('mwamba_interest_rate', '20.0', 'loan', 'Monthly interest rate for Mwamba loans (%)'),
            ('mwamba_processing_fee', '5.0', 'loan', 'Processing fee for Mwamba loans (%)'),
            ('mwamba_late_payment_penalty', '5.0', 'loan', 'Monthly penalty rate for late Mwamba loan payments (%)'),
            ('mwamba_min_amount', '5000', 'loan', 'Minimum amount for Mwamba loans'),
            ('mwamba_max_amount', '200000', 'loan', 'Maximum amount for Mwamba loans'),
            ('mwamba_duration_months', '3', 'loan', 'Duration in months for Mwamba loans'),
            ('mwamba_repayment_methods', '["weekly", "monthly"]', 'loan', 'Available repayment methods for Mwamba loans'),


            # Imara Product Settings
            ('imara_interest_rate', '10.0', 'loan', 'Monthly interest rate for Imara loans (%)'),
            ('imara_processing_fee', '5.0', 'loan', 'Processing fee for Imara loans (%)'),
            ('imara_late_payment_penalty', '3.0', 'loan', 'Monthly penalty rate for late Imara loan payments (%)'),
            ('imara_min_amount', '10000', 'loan', 'Minimum amount for Imara loans'),
            ('imara_max_amount', '500000', 'loan', 'Maximum amount for Imara loans'),
            ('imara_duration_months', '6', 'loan', 'Duration in months for Imara loans'),
            ('imara_repayment_methods', '["monthly"]', 'loan', 'Available repayment methods for Imara loans'),


            # General Loan Settings
            ('late_payment_penalty', '5.0', 'loan', 'Monthly penalty rate for late payments (%)'),
            ('auto_approval_min_score', '80', 'loan', 'Minimum credit score for auto-approval'),
            ('auto_approval_max_amount', '25000', 'loan', 'Maximum amount for auto-approval'),

            # System Settings
            ('company_name', 'HAVEN GRAZURI ADVANCE', 'system', 'Company name'),
            ('company_email', 'info@branchbusiness.co.ke', 'system', 'Company email address'),
            ('company_phone', '+254700000000', 'system', 'Company phone number'),
            ('company_address', 'Nairobi, Kenya', 'system', 'Company physical address'),
            ('currency', 'KES', 'system', 'Default currency'),
            ('timezone', 'Africa/Nairobi', 'system', 'System timezone'),

            # Notification Settings
            ('email_notifications', 'true', 'notification', 'Enable email notifications'),
            ('sms_notifications', 'true', 'notification', 'Enable SMS notifications'),
            ('loan_reminders', 'true', 'notification', 'Enable loan repayment reminders'),
            ('due_date_reminder_days', '3', 'notification', 'Days before due date to send reminder'),

            # Document Settings
            ('max_file_size', '5242880', 'document', 'Maximum file size in bytes (5MB)'),
            ('allowed_file_types', '["pdf", "doc", "docx", "jpg", "jpeg", "png"]', 'document', 'Allowed file types'),
        ]

        # Create or update settings
        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(f'Created/updated setting: {key}')

        self.stdout.write(self.style.SUCCESS('Successfully created default settings'))