#!/usr/bin/env python
"""
Setup script for the Reports & Statements Dashboard
Run this script to set up all necessary components
"""

import os
import sys
import django
from django.core.management import execute_from_command_line

def setup_reports_dashboard():
    """Set up the Reports & Statements Dashboard"""
    
    print("🚀 Setting up Reports & Statements Dashboard...")
    print("=" * 50)
    
    # Set up Django environment
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
    django.setup()
    
    try:
        # Step 1: Run migrations
        print("\n📊 Step 1: Running database migrations...")
        execute_from_command_line(['manage.py', 'makemigrations', 'reports'])
        execute_from_command_line(['manage.py', 'migrate'])
        print("✅ Database migrations completed successfully!")
        
        # Step 2: Set up registration fees
        print("\n💰 Step 2: Setting up registration fees...")
        execute_from_command_line(['manage.py', 'setup_registration_fees'])
        print("✅ Registration fees setup completed!")
        
        # Step 3: Collect static files (if needed)
        print("\n📁 Step 3: Collecting static files...")
        try:
            execute_from_command_line(['manage.py', 'collectstatic', '--noinput'])
            print("✅ Static files collected successfully!")
        except Exception as e:
            print(f"⚠️  Static files collection skipped: {e}")
        
        print("\n" + "=" * 50)
        print("🎉 Reports & Statements Dashboard setup completed successfully!")
        print("\n📋 What's been set up:")
        print("   ✓ Database models and migrations")
        print("   ✓ Registration fees for all products")
        print("   ✓ Admin interface configurations")
        print("   ✓ Report templates and views")
        print("   ✓ Customer request management")
        print("   ✓ Comprehensive analytics dashboard")
        
        print("\n🌐 Access your dashboard at:")
        print("   • Main Dashboard: http://localhost:8000/reports/")
        print("   • Admin Interface: http://localhost:8000/admin/")
        
        print("\n📊 Available Reports:")
        print("   • Loans Due Report")
        print("   • Delinquent Loans Report") 
        print("   • Loans in Arrears Report")
        print("   • Processing Fees Report")
        print("   • Interest Income Report")
        print("   • Registration Fees Report")
        print("   • Customer Requests Report")
        
        print("\n🔧 Management Features:")
        print("   • Customer Request Management")
        print("   • Registration Fee Settings")
        print("   • Report Scheduling")
        print("   • PDF Export Capabilities")
        
        print("\n📖 For detailed documentation, see: REPORTS_DASHBOARD_README.md")
        print("=" * 50)
        
    except Exception as e:
        print(f"\n❌ Error during setup: {e}")
        print("Please check the error message above and try again.")
        sys.exit(1)

if __name__ == '__main__':
    setup_reports_dashboard()