from django.core.management.base import BaseCommand
from shop_management.models.data.customer import Customer
from shop_management.models.data.shop import Shop
from shop_management.models.data.utils import Currency
from faker import Faker
import random

class Command(BaseCommand):
    help = 'Seed 50 customers with random data'

    def handle(self, *args, **kwargs):
        fake = Faker()
        
        shops = list(Shop.objects.all())
        currencies = list(Currency.objects.all())
        
        if not shops:
            self.stdout.write(self.style.ERROR('No shops found. Please create shops first.'))
            return
        
        if not currencies:
            self.stdout.write(self.style.ERROR('No currencies found. Please create currencies first.'))
            return

        customers_created = 0
        
        for i in range(50):
            try:
                customer = Customer.objects.create(
                    name=fake.name(),
                    email=fake.email(),
                    contact=fake.phone_number()[:20],
                    address=fake.address()[:200],
                    shop=random.choice(shops),
                    opening_balance=round(random.uniform(0, 5000), 2) if random.choice([True, False]) else 0,
                    opening_balance_currency=random.choice(currencies) if random.choice([True, False]) else None
                )
                customers_created += 1
                self.stdout.write(self.style.SUCCESS(f'Created customer: {customer.name}'))
            except Exception as e:
                self.stdout.write(self.style.ERROR(f'Error creating customer {i+1}: {str(e)}'))
        
        self.stdout.write(self.style.SUCCESS(f'\nSuccessfully created {customers_created} customers!'))
