#!/usr/bin/env python
"""
Simple verification script for Grazuri loan products
"""
import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

from loans.models import LoanProduct
from decimal import Decimal

print("\n" + "="*80)
print("GRAZURI LOAN PRODUCTS VERIFICATION")
print("="*80)

# Check Biashara Loan
biashara = LoanProduct.objects.filter(product_type='biashara').first()
if biashara:
    print(f"\n✓ BIASHARA LOAN")
    print(f"  ID: {biashara.id}")
    print(f"  Name: {biashara.name}")
    print(f"  Account Type: {biashara.grazuri_account_type}")
    print(f"  GL Code: {biashara.gl_code}")
    print(f"  Amount: KES {biashara.min_amount:,.2f} - {biashara.max_amount:,.2f}")
    print(f"  Interest: {biashara.interest_rate}% monthly")
    print(f"  Processing Fee: {biashara.processing_fee}%")
    print(f"  Duration: {biashara.min_duration}-{biashara.max_duration} days")
    print(f"  Active: {biashara.is_active}")
    
    # Test calculation without SystemSetting
    test_amount = Decimal('50000.00')
    test_days = 30
    months = Decimal(str(test_days)) / Decimal('30')
    months = max(Decimal('1'), months)
    
    monthly_rate = biashara.interest_rate / Decimal('100')
    interest = test_amount * monthly_rate * months
    
    fee_rate = biashara.processing_fee / Decimal('100')
    fee = test_amount * fee_rate
    
    total = test_amount + interest + fee
    
    print(f"\n  CALCULATION TEST (KES 50,000 for 30 days):")
    print(f"    Principal: KES {test_amount:,.2f}")
    print(f"    Interest: KES {interest:,.2f}")
    print(f"    Processing Fee: KES {fee:,.2f}")
    print(f"    Total: KES {total:,.2f}")
else:
    print("\n✗ Biashara Loan NOT found")

# Check Log Book Loan
logbook = LoanProduct.objects.filter(product_type='logbook').first()
if logbook:
    print(f"\n✓ LOG BOOK LOAN")
    print(f"  ID: {logbook.id}")
    print(f"  Name: {logbook.name}")
    print(f"  Account Type: {logbook.grazuri_account_type}")
    print(f"  GL Code: {logbook.gl_code}")
    print(f"  Amount: KES {logbook.min_amount:,.2f} - {logbook.max_amount:,.2f}")
    print(f"  Interest: {logbook.interest_rate}% monthly")
    print(f"  Processing Fee: {logbook.processing_fee}%")
    print(f"  Duration: {logbook.min_duration}-{logbook.max_duration} days")
    print(f"  Requires Collateral: {logbook.requires_collateral}")
    print(f"  Active: {logbook.is_active}")
    
    # Test calculation without SystemSetting
    test_amount = Decimal('100000.00')
    test_days = 60
    months = Decimal(str(test_days)) / Decimal('30')
    months = max(Decimal('1'), months)
    
    monthly_rate = logbook.interest_rate / Decimal('100')
    interest = test_amount * monthly_rate * months
    
    fee_rate = logbook.processing_fee / Decimal('100')
    fee = test_amount * fee_rate
    
    total = test_amount + interest + fee
    
    print(f"\n  CALCULATION TEST (KES 100,000 for 60 days):")
    print(f"    Principal: KES {test_amount:,.2f}")
    print(f"    Interest: KES {interest:,.2f}")
    print(f"    Processing Fee: KES {fee:,.2f}")
    print(f"    Total: KES {total:,.2f}")
else:
    print("\n✗ Log Book Loan NOT found")

print("\n" + "="*80)
if biashara and logbook:
    print("✓ ALL GRAZURI LOAN PRODUCTS VERIFIED")
else:
    print("✗ SOME PRODUCTS MISSING")
print("="*80 + "\n")
