from django.core.management.base import BaseCommand
from apps.business.models import Business
from apps.accounting.models import Account, AccountType


class Command(BaseCommand):
    help = 'Setup default chart of accounts for a business'

    def add_arguments(self, parser):
        parser.add_argument('business_id', type=int, help='Business ID')

    def handle(self, *args, **options):
        business_id = options['business_id']
        
        try:
            business = Business.objects.get(id=business_id)
        except Business.DoesNotExist:
            self.stdout.write(self.style.ERROR(f'Business {business_id} not found'))
            return

        default_accounts = [
            # Assets
            ('1000', 'Cash', AccountType.ASSET, 'Cash on hand and in bank'),
            ('1100', 'Petty Cash', AccountType.ASSET, 'Petty cash fund'),
            ('1200', 'Accounts Receivable', AccountType.ASSET, 'Money owed by customers'),
            ('1300', 'Inventory', AccountType.ASSET, 'Inventory on hand'),
            ('1500', 'Fixed Assets', AccountType.ASSET, 'Property and equipment'),
            
            # Liabilities
            ('2000', 'Accounts Payable', AccountType.LIABILITY, 'Money owed to suppliers'),
            ('2100', 'Credit Card Payable', AccountType.LIABILITY, 'Credit card balances'),
            ('2200', 'Sales Tax Payable', AccountType.LIABILITY, 'Sales tax collected'),
            
            # Equity
            ('3000', 'Owner\'s Equity', AccountType.EQUITY, 'Owner\'s investment'),
            ('3100', 'Retained Earnings', AccountType.EQUITY, 'Accumulated profits'),
            
            # Revenue
            ('4000', 'Sales Revenue', AccountType.REVENUE, 'Revenue from sales'),
            ('4100', 'Service Revenue', AccountType.REVENUE, 'Revenue from services'),
            
            # Cost of Goods Sold
            ('5000', 'Cost of Goods Sold', AccountType.COST_OF_GOODS_SOLD, 'Direct cost of products sold'),
            
            # Expenses
            ('6000', 'Rent Expense', AccountType.EXPENSE, 'Rent payments'),
            ('6100', 'Commission Expense', AccountType.EXPENSE, 'Sales commissions'),
            ('6200', 'Utilities Expense', AccountType.EXPENSE, 'Electricity, water, etc.'),
            ('6300', 'Salary Expense', AccountType.EXPENSE, 'Employee salaries'),
            ('6400', 'Office Supplies', AccountType.EXPENSE, 'Office supplies and materials'),
            ('6500', 'Insurance Expense', AccountType.EXPENSE, 'Insurance premiums'),
            ('6600', 'Advertising Expense', AccountType.EXPENSE, 'Marketing and advertising'),
            ('6700', 'Depreciation Expense', AccountType.EXPENSE, 'Asset depreciation'),
            ('6800', 'Bank Charges', AccountType.EXPENSE, 'Bank fees and charges'),
            ('6900', 'Miscellaneous Expense', AccountType.EXPENSE, 'Other expenses'),
        ]

        created_count = 0
        
        for code, name, acc_type, description in default_accounts:
            account, created = Account.objects.get_or_create(
                business=business,
                account_code=code,
                defaults={
                    'account_name': name,
                    'account_type': acc_type,
                    'description': description,
                    'is_active': True
                }
            )
            
            if created:
                created_count += 1
                self.stdout.write(
                    self.style.SUCCESS(f'Created account: {code} - {name}')
                )
            else:
                self.stdout.write(
                    self.style.WARNING(f'Account already exists: {code} - {name}')
                )

        self.stdout.write(
            self.style.SUCCESS(
                f'\nSetup complete! Created {created_count} new accounts for {business.name}'
            )
        )