import random
from django.core.management.base import BaseCommand
from django.utils import timezone
from device_interaction.models import DeviceType, DeviceBatch, Device

class Command(BaseCommand):
    help = '创建一批测试用设备'

    def add_arguments(self, parser):
        parser.add_argument('--type-count', type=int, default=3, help='创建的设备类型数量')
        parser.add_argument('--batch-count', type=int, default=2, help='每个类型的批次数量')
        parser.add_argument('--device-count', type=int, default=10, help='每个批次的设备数量')

    def handle(self, *args, **options):
        type_count = options['type_count']
        batch_count = options['batch_count']
        device_count = options['device_count']
        
        self.stdout.write(self.style.SUCCESS(f'开始创建测试设备: {type_count}种类型 x {batch_count}个批次 x {device_count}台设备'))
        
        # 创建设备类型
        device_types = []
        for i in range(1, type_count + 1):
            name = f"测试设备类型-{i}"
            code = f"T{i:02d}"
            
            device_type, created = DeviceType.objects.get_or_create(
                name=name,
                defaults={
                    'code': code,
                    'description': f'测试用设备类型{i}的描述'
                }
            )
            
            if created:
                self.stdout.write(self.style.SUCCESS(f'创建设备类型: {name} (代码: {code})'))
            else:
                self.stdout.write(self.style.WARNING(f'设备类型已存在: {name} (代码: {code})'))
            
            device_types.append(device_type)
        
        # 为每个设备类型创建批次
        for device_type in device_types:
            for j in range(1, batch_count + 1):
                batch_number = f"B{device_type.code}{j:02d}"
                production_date = timezone.now().date()
                
                batch, created = DeviceBatch.objects.get_or_create(
                    batch_number=batch_number,
                    defaults={
                        'device_type': device_type,
                        'production_date': production_date,
                        'quantity': device_count,
                        'description': f'{device_type.name}的第{j}批次'
                    }
                )
                
                if created:
                    self.stdout.write(self.style.SUCCESS(f'创建批次: {batch_number} (设备类型: {device_type.name})'))
                else:
                    self.stdout.write(self.style.WARNING(f'批次已存在: {batch_number} (设备类型: {device_type.name})'))
                
                # 为每个批次创建设备
                for k in range(1, device_count + 1):
                    serial_number = f"{k:05d}"
                    
                    # 随机生成MAC地址
                    mac_parts = ['00', '11', '22', '33', '44', '55'] + [format(random.randint(0, 255), '02x') for _ in range(2)]
                    random.shuffle(mac_parts)
                    mac_address = ':'.join(mac_parts[:6])
                    
                    device, created = Device.objects.get_or_create(
                        device_type=device_type,
                        batch=batch,
                        serial_number=serial_number,
                        defaults={
                            'mac_address': mac_address,
                            'is_active': False
                        }
                    )
                    
                    if created:
                        self.stdout.write(f'创建设备: {device.device_code} (MAC: {mac_address})')
                    else:
                        self.stdout.write(self.style.WARNING(f'设备已存在: {device.device_code}'))
        
        # 统计结果
        total_types = DeviceType.objects.count()
        total_batches = DeviceBatch.objects.count()
        total_devices = Device.objects.count()
        
        self.stdout.write(self.style.SUCCESS(
            f'\n测试设备创建完成!\n'
            f'总计: {total_types}种设备类型, {total_batches}个批次, {total_devices}台设备'
        )) 