from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
from django.utils import timezone

User = get_user_model()


class Command(BaseCommand):
    help = 'Create sample users with email addresses for testing'

    def handle(self, *args, **options):
        # Sample admin user
        admin_user, created = User.objects.get_or_create(
            email='admin@branchbusinessadvance.com',
            defaults={
                'username': 'admin@branchbusinessadvance.com',
                'first_name': 'System',
                'last_name': 'Administrator',
                'phone_number': '+254700000010',
                'password': 'admin123',
                'role': 'admin',
                'status': 'active',
                'is_staff': True,
                'is_superuser': True,
                'is_active': True,
                'is_email_verified': True,
                'is_phone_verified': True,
            }
        )
        
        if created:
            admin_user.set_password('admin123')
            admin_user.save()
            self.stdout.write(
                self.style.SUCCESS(f'Created admin user: {admin_user.email}')
            )
        else:
            self.stdout.write(
                self.style.WARNING(f'Admin user already exists: {admin_user.email}')
            )

        # Sample loan officer
        loan_officer, created = User.objects.get_or_create(
            email='officer@branchbusinessadvance.com',
            defaults={
                'username': 'officer@branchbusinessadvance.com',
                'first_name': 'John',
                'last_name': 'Loan Officer',
                'phone_number': '+254700000011',
                'password': 'officer123',
                'role': 'loan_officer',
                'status': 'active',
                'is_staff': True,
                'is_active': True,
                'is_email_verified': True,
                'is_phone_verified': True,
            }
        )
        
        if created:
            loan_officer.set_password('officer123')
            loan_officer.save()
            self.stdout.write(
                self.style.SUCCESS(f'Created loan officer: {loan_officer.email}')
            )
        else:
            self.stdout.write(
                self.style.WARNING(f'Loan officer already exists: {loan_officer.email}')
            )

        # Sample borrower
        borrower, created = User.objects.get_or_create(
            email='client@branchbusinessadvance.com',
            defaults={
                'username': 'client@branchbusinessadvance.com',
                'first_name': 'Jane',
                'last_name': 'Client',
                'phone_number': '+254700000012',
                'password': 'client123',
                'role': 'borrower',
                'status': 'active',
                'is_active': True,
                'is_email_verified': True,
                'is_phone_verified': True,
                'business_name': 'Sample Business',
                'business_type': 'retail',
                'monthly_income': 50000.00,
            }
        )
        
        if created:
            borrower.set_password('client123')
            borrower.save()
            self.stdout.write(
                self.style.SUCCESS(f'Created borrower: {borrower.email}')
            )
        else:
            self.stdout.write(
                self.style.WARNING(f'Borrower already exists: {borrower.email}')
            )

        self.stdout.write(
            self.style.SUCCESS('Sample users created successfully!')
        )
        self.stdout.write('Login credentials:')
        self.stdout.write('Admin: admin@branchbusinessadvance.com / admin123')
        self.stdout.write('Officer: officer@branchbusinessadvance.com / officer123')
        self.stdout.write('Client: client@branchbusinessadvance.com / client123') 