"""
Management command to create sample categories and businesses for testing
"""
from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
from business.models import Category, Business
from decimal import Decimal

User = get_user_model()


class Command(BaseCommand):
    help = 'Creates sample categories and businesses for testing'

    def handle(self, *args, **kwargs):
        self.stdout.write('Creating sample data...')

        # Create a sample user if not exists
        user, created = User.objects.get_or_create(
            email='owner@example.com',
            defaults={
                'first_name': 'Business',
                'last_name': 'Owner',
                'is_email_verified': True,
            }
        )
        if created:
            user.set_password('password123')
            user.save()
            self.stdout.write(self.style.SUCCESS(f'Created user: {user.email}'))

        # Create Categories
        categories_data = [
            {'name': 'Carpenter', 'description': 'Wood work and furniture making'},
            {'name': 'Painter', 'description': 'House painting services'},
            {'name': 'Plumber', 'description': 'Plumbing and water line services'},
            {'name': 'Electrician', 'description': 'Electrical wiring and repair'},
            {'name': 'Mason', 'description': 'Construction and masonry work'},
            {'name': 'Welder', 'description': 'Metal welding services'},
            {'name': 'Tailor', 'description': 'Clothing stitching and alteration'},
            {'name': 'Mechanic', 'description': 'Vehicle repair and maintenance'},
        ]

        categories = []
        for cat_data in categories_data:
            category, created = Category.objects.get_or_create(
                name=cat_data['name'],
                defaults={'description': cat_data['description']}
            )
            categories.append(category)
            if created:
                self.stdout.write(self.style.SUCCESS(f'Created category: {category.name}'))

        # Create Sample Businesses (Lahore, Pakistan locations)
        businesses_data = [
            {
                'title': 'Ahmad Carpentry Services',
                'description': 'Professional carpenter with 10+ years experience in custom furniture and interior woodwork.',
                'latitude': Decimal('31.5204'),
                'longitude': Decimal('74.3587'),
                'address': 'Model Town, Lahore',
                'city': 'Lahore',
                'country': 'Pakistan',
                'phone_number': '+923001234567',
                'provides_home_service': True,
                'categories': [0],  # Carpenter
            },
            {
                'title': 'Khan Painting Services',
                'description': 'Expert house painters providing quality painting services for residential and commercial properties.',
                'latitude': Decimal('31.4697'),
                'longitude': Decimal('74.2728'),
                'address': 'Johar Town, Lahore',
                'city': 'Lahore',
                'country': 'Pakistan',
                'phone_number': '+923009876543',
                'provides_home_service': True,
                'categories': [1],  # Painter
            },
            {
                'title': 'Hassan Plumbing Solutions',
                'description': 'Complete plumbing solutions for homes and offices. Available 24/7 for emergency services.',
                'latitude': Decimal('31.4504'),
                'longitude': Decimal('74.3587'),
                'address': 'DHA Phase 5, Lahore',
                'city': 'Lahore',
                'country': 'Pakistan',
                'phone_number': '+923112233445',
                'provides_home_service': True,
                'categories': [2],  # Plumber
            },
            {
                'title': 'Ali Electrical Works',
                'description': 'Licensed electrician providing electrical installation, maintenance and repair services.',
                'latitude': Decimal('31.5820'),
                'longitude': Decimal('74.3298'),
                'address': 'Gulberg III, Lahore',
                'city': 'Lahore',
                'country': 'Pakistan',
                'phone_number': '+923334455667',
                'provides_home_service': True,
                'categories': [3],  # Electrician
            },
            {
                'title': 'Raza Construction & Masonry',
                'description': 'Quality construction and masonry services for residential and commercial projects.',
                'latitude': Decimal('31.4187'),
                'longitude': Decimal('74.2456'),
                'address': 'Township, Lahore',
                'city': 'Lahore',
                'country': 'Pakistan',
                'phone_number': '+923445566778',
                'provides_home_service': False,
                'categories': [4],  # Mason
            },
            {
                'title': 'Imran Welding Workshop',
                'description': 'Professional welding services for gates, grills, and metal fabrication.',
                'latitude': Decimal('31.5546'),
                'longitude': Decimal('74.3572'),
                'address': 'Faisal Town, Lahore',
                'city': 'Lahore',
                'country': 'Pakistan',
                'phone_number': '+923556677889',
                'provides_home_service': False,
                'categories': [5],  # Welder
            },
            {
                'title': 'Fatima Tailoring House',
                'description': 'Expert tailoring services for men and women clothing with custom stitching.',
                'latitude': Decimal('31.4824'),
                'longitude': Decimal('74.3587'),
                'address': 'Allama Iqbal Town, Lahore',
                'city': 'Lahore',
                'country': 'Pakistan',
                'phone_number': '+923667788990',
                'provides_home_service': False,
                'categories': [6],  # Tailor
            },
            {
                'title': 'Bilal Auto Mechanics',
                'description': 'Complete vehicle repair and maintenance services with experienced mechanics.',
                'latitude': Decimal('31.4924'),
                'longitude': Decimal('74.3143'),
                'address': 'Wapda Town, Lahore',
                'city': 'Lahore',
                'country': 'Pakistan',
                'phone_number': '+923778899001',
                'provides_home_service': False,
                'categories': [7],  # Mechanic
            },
            {
                'title': 'Master Furniture Works',
                'description': 'Custom furniture manufacturing and interior design services.',
                'latitude': Decimal('31.5310'),
                'longitude': Decimal('74.3487'),
                'address': 'Garden Town, Lahore',
                'city': 'Lahore',
                'country': 'Pakistan',
                'phone_number': '+923889900112',
                'website_link': 'https://masterfurniture.com',
                'provides_home_service': True,
                'categories': [0],  # Carpenter
                'average_rating': Decimal('4.80'),
                'total_reviews': 45,
            },
            {
                'title': 'Pro Painters Lahore',
                'description': 'Professional painting services with quality materials and skilled painters.',
                'latitude': Decimal('31.5015'),
                'longitude': Decimal('74.3235'),
                'address': 'Cavalry Ground, Lahore',
                'city': 'Lahore',
                'country': 'Pakistan',
                'phone_number': '+923990011223',
                'provides_home_service': True,
                'categories': [1],  # Painter
                'average_rating': Decimal('4.65'),
                'total_reviews': 38,
            },
        ]

        for biz_data in businesses_data:
            category_indices = biz_data.pop('categories')

            business, created = Business.objects.get_or_create(
                title=biz_data['title'],
                defaults={
                    'owner': user,
                    **biz_data
                }
            )

            if created:
                # Add categories
                for idx in category_indices:
                    business.categories.add(categories[idx])

                self.stdout.write(self.style.SUCCESS(f'Created business: {business.title}'))

        self.stdout.write(self.style.SUCCESS('\nSample data created successfully!'))
        self.stdout.write(self.style.SUCCESS(f'Total Categories: {Category.objects.count()}'))
        self.stdout.write(self.style.SUCCESS(f'Total Businesses: {Business.objects.count()}'))
        self.stdout.write(self.style.SUCCESS('\nYou can now test the APIs!'))
