"""补齐 gvsdsdk 权限码（可重复执行）。"""
import uuid

from django.core.management.base import BaseCommand
from django.utils import timezone

from gvsdsdk.models import Permission

# (PermCode, PermName, PermDesc, PermCategory)
EXTRA_PERMISSIONS = [
    (
        'sfbq666',
        '身份装饰标签管理',
        '管理小程序身份装饰标签（与用户管理-身份装饰标签菜单对应）',
        '用户管理',
    ),
]


class Command(BaseCommand):
    help = '写入 gvsdsdk Permission 表缺失的权限码（如 sfbq666）'

    def handle(self, *args, **options):
        sample = Permission.objects.filter(PermStatus=1).first()
        tenant_uuid = sample.TenantUUID if sample else uuid.UUID('00000000-0000-0000-0000-000000000001').bytes
        created = 0
        for code, name, desc, category in EXTRA_PERMISSIONS:
            if Permission.objects.filter(PermCode=code).exists():
                self.stdout.write(f'  已存在: {code}')
                continue
            Permission.objects.create(
                PermUUID=uuid.uuid4().bytes,
                TenantUUID=tenant_uuid,
                PermCode=code,
                PermName=name,
                PermDesc=desc,
                PermCategory=category,
                PermStatus=1,
                CreateTime=timezone.now(),
            )
            created += 1
            self.stdout.write(self.style.SUCCESS(f'  新增权限: {code} - {name}'))
        self.stdout.write(self.style.SUCCESS(f'完成，新增 {created} 条权限'))
