from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
from django.db import transaction
from apps.banking.models import Account

class Command(BaseCommand):
    help = 'Initializes basic banking data: creates accounts for test users.'

    def handle(self, *args, **options):
        User = get_user_model()

        self.stdout.write(self.style.SUCCESS('--- Initializing Banking Data ---'))

        try:
            regular_user = User.objects.get(email='user@example.com')
            superuser = User.objects.get(email='admin@example.com')
        except User.DoesNotExist:
            self.stdout.write(self.style.ERROR('Test users not found. Please run init_auth_data first.'))
            return

        with transaction.atomic():
            # Create CURRENT account for regular user
            if not Account.objects.filter(owner=regular_user, type=Account.AccountType.CURRENT).exists():
                self.stdout.write(f'Creating CURRENT account for {regular_user.email}...')
                Account.objects.create(
                    owner=regular_user,
                    name=f'Compte Courant {regular_user.username}',
                    type=Account.AccountType.CURRENT,
                    balance=0.00,
                    account_number='ACC001',
                    created_by=superuser # Admin creates the account
                )
                self.stdout.write(self.style.SUCCESS('CURRENT account created.'))
            else:
                self.stdout.write(self.style.WARNING(f'CURRENT account for {regular_user.email} already exists.'))

            # Create LOAN_FUND account for superuser (cooperative's loan fund)
            if not Account.objects.filter(owner=superuser, type=Account.AccountType.LOAN_FUND).exists():
                self.stdout.write(f'Creating LOAN_FUND account for {superuser.email}...')
                Account.objects.create(
                    owner=superuser,
                    name='Fonds de Prêt Coopératif',
                    type=Account.AccountType.LOAN_FUND,
                    balance=0.00,
                    account_number='LOAN001',
                    created_by=superuser
                )
                self.stdout.write(self.style.SUCCESS('LOAN_FUND account created.'))
            else:
                self.stdout.write(self.style.WARNING(f'LOAN_FUND account for {superuser.email} already exists.'))

            # Create SHARE_CAPITAL account for superuser (cooperative's share capital)
            if not Account.objects.filter(owner=superuser, type=Account.AccountType.SHARE_CAPITAL).exists():
                self.stdout.write(f'Creating SHARE_CAPITAL account for {superuser.email}...')
                Account.objects.create(
                    owner=superuser,
                    name='Capital Social Coopératif',
                    type=Account.AccountType.SHARE_CAPITAL,
                    balance=0.00,
                    account_number='SHARE001',
                    created_by=superuser
                )
                self.stdout.write(self.style.SUCCESS('SHARE_CAPITAL account created.'))
            else:
                self.stdout.write(self.style.WARNING(f'SHARE_CAPITAL account for {superuser.email} already exists.'))

        self.stdout.write(self.style.SUCCESS('--- Banking Data Initialization Complete ---'))
