from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
from django.utils import timezone
from rest_framework.authtoken.models import Token

User = get_user_model()

class Command(BaseCommand):
    help = '创建测试用户：1个管理员，2个组长，4个组员'

    def handle(self, *args, **kwargs):
        # 创建管理员 - 技术部管理员
        admin, created = User.objects.get_or_create(
            username='admin',
            defaults={
                'email': 'admin@example.com',
                'name': '张管理',
                'role': 'admin',
                'is_staff': True,
                'is_superuser': True,
                'last_login': timezone.now()
            }
        )
        if created:
            admin.set_password('admin123')
            admin.save()
            token = Token.objects.create(user=admin)
            self.stdout.write(self.style.SUCCESS(
                f'成功创建管理员用户: {admin.username}（{admin.name}）, Token: {token.key}'
            ))
        else:
            self.stdout.write(self.style.WARNING(f'管理员用户已存在: {admin.username}'))

        # 创建组长 - 研发部组长和测试部组长
        leaders = [
            {
                'username': 'leader1',
                'password': 'leader123',
                'email': 'leader1@example.com',
                'name': '李研发',
                'department': '研发部',
                'role': 'leader'
            },
            {
                'username': 'leader2',
                'password': 'leader123',
                'email': 'leader2@example.com',
                'name': '王测试',
                'department': '测试部',
                'role': 'leader'
            }
        ]

        for leader_data in leaders:
            leader, created = User.objects.get_or_create(
                username=leader_data['username'],
                defaults={
                    'email': leader_data['email'],
                    'name': leader_data['name'],
                    'role': leader_data['role'],
                    'department': leader_data['department'],
                    'is_staff': False,
                    'last_login': timezone.now()
                }
            )
            if created:
                leader.set_password(leader_data['password'])
                leader.save()
                token = Token.objects.create(user=leader)
                self.stdout.write(self.style.SUCCESS(
                    f'成功创建组长用户: {leader.username}（{leader.name}）, Token: {token.key}'
                ))
            else:
                self.stdout.write(self.style.WARNING(f'组长用户已存在: {leader.username}'))

        # 创建组员 - 2个开发组员，2个测试组员
        members = [
            {
                'username': 'member1',
                'password': 'member123',
                'email': 'member1@example.com',
                'name': '赵开发',
                'department': '研发部',
                'role': 'member',
                'group': '前端组'
            },
            {
                'username': 'member2',
                'password': 'member123',
                'email': 'member2@example.com',
                'name': '钱开发',
                'department': '研发部',
                'role': 'member',
                'group': '后端组'
            },
            {
                'username': 'member3',
                'password': 'member123',
                'email': 'member3@example.com',
                'name': '孙测试',
                'department': '测试部',
                'role': 'member',
                'group': '功能测试组'
            },
            {
                'username': 'member4',
                'password': 'member123',
                'email': 'member4@example.com',
                'name': '周测试',
                'department': '测试部',
                'role': 'member',
                'group': '自动化测试组'
            }
        ]

        for member_data in members:
            member, created = User.objects.get_or_create(
                username=member_data['username'],
                defaults={
                    'email': member_data['email'],
                    'name': member_data['name'],
                    'role': member_data['role'],
                    'department': member_data['department'],
                    'group': member_data['group'],
                    'is_staff': False,
                    'last_login': timezone.now()
                }
            )
            if created:
                member.set_password(member_data['password'])
                member.save()
                token = Token.objects.create(user=member)
                self.stdout.write(self.style.SUCCESS(
                    f'成功创建组员用户: {member.username}（{member.name}）, Token: {token.key}'
                ))
            else:
                self.stdout.write(self.style.WARNING(f'组员用户已存在: {member.username}'))

        self.stdout.write(self.style.SUCCESS('所有测试用户创建完成!'))
