# -*- coding: utf-8 -*-

from django.core.management.base import BaseCommand, CommandError

from ...models import *


class Command(BaseCommand):
    args = ''
    help = 'Regen doc.'

    def handle(self, *args, **options):

        workshops = WorkshopProperties.objects.filter(user__is_superuser=True).distinct()
        documents = Document.objects.filter(user__is_superuser=True).distinct()

        ws = []
        docs = []

        for user in get_user_model().objects.filter(is_staff=True, is_superuser=False):
            print(user)
            for workshop in workshops:
                try:
                    w = WorkshopProperties.objects.get(workshop=workshop.workshop, user=user)
                except:
                    w = WorkshopProperties(workshop=workshop.workshop,
                                           name=workshop.name,
                                           user=user,
                                           is_deleted=workshop.is_deleted,
                                           master=True,
                                           trainer=workshop.trainer,
                                           broadcastable=workshop.broadcastable,
                                           hidden=workshop.hidden,
                                           enable_point=workshop.enable_point,
                                           order=None)
                    ws.append(w)

            for document in documents:
                try:
                    d = Document.objects.get(
                        training=document.training,
                        user=user,
                        doc_file=document.doc_file)
                except:
                    d = Document(name=document.name,
                                 doc_file=document.doc_file,
                                 doc_file_comment=document.doc_file_comment,
                                 training=document.training,
                                 user=user,
                                 master=True,
                                 is_deleted=document.is_deleted,
                                 trainer=document.trainer,
                                 broadcastable=document.broadcastable,
                                 hidden=document.hidden,
                                 order=None)
                    docs.append(d)

        print("%d workshops généré" % (len(ws)))
        print("%d documents généré" % (len(docs)))

        for w in ws:
            w.save()
        for d in docs:
            d.save()
