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

from apps.core.models import Tag
from apps.customers.models import Customer


class Command(BaseCommand):
    help = 'Seed demo data for core tags and customers'

    def handle(self, *args, **options):
        user_model = get_user_model()
        owner = user_model.objects.order_by('id').first()

        if owner is None:
            self.stdout.write(self.style.WARNING('Aucun utilisateur trouve. Creez d\'abord un utilisateur.'))
            return

        tags_payload = [
            ('VIP', '#d97706'),
            ('Patrimoine', '#2563eb'),
            ('Assurance', '#059669'),
            ('Relance', '#dc2626'),
        ]
        tags = {}
        for name, color in tags_payload:
            tag, _ = Tag.objects.get_or_create(name=name, defaults={'color': color})
            if color and tag.color != color:
                tag.color = color
                tag.save(update_fields=['color'])
            tags[name] = tag

        customers_payload = [
            {
                'customer_type': Customer.CustomerType.INDIVIDUAL,
                'status': Customer.Status.PROSPECT,
                'first_name': 'Alice',
                'last_name': 'Martin',
                'email': 'alice.martin@example.com',
                'phone': '+33601020304',
                'city': 'Paris',
                'country': 'France',
                'tags': ['Relance'],
            },
            {
                'customer_type': Customer.CustomerType.COMPANY,
                'status': Customer.Status.ACTIVE,
                'company_name': 'Nova Conseil',
                'email': 'contact@novaconseil.test',
                'phone': '+33123456789',
                'city': 'Lyon',
                'country': 'France',
                'tags': ['VIP', 'Patrimoine'],
            },
            {
                'customer_type': Customer.CustomerType.INDIVIDUAL,
                'status': Customer.Status.INACTIVE,
                'first_name': 'Karim',
                'last_name': 'Benali',
                'email': 'karim.benali@example.com',
                'phone': '+33699887766',
                'city': 'Marseille',
                'country': 'France',
                'tags': ['Assurance'],
            },
        ]

        created_count = 0
        for payload in customers_payload:
            tags_names = payload.pop('tags', [])
            defaults = {**payload, 'owner': owner}
            customer, created = Customer.objects.get_or_create(
                email=payload.get('email') or '',
                defaults=defaults,
            )
            if not created:
                for key, value in defaults.items():
                    setattr(customer, key, value)
                customer.save()
            customer.tags.set([tags[name] for name in tags_names if name in tags])
            if created:
                created_count += 1

        self.stdout.write(self.style.SUCCESS(f'Demo data seeded. Customers created: {created_count}'))