from django.db import models
from wagtail.models import Page, Orderable
from modelcluster.fields import ParentalKey
from wagtail.admin.panels import FieldPanel, StreamFieldPanel, InlinePanel


class CompetencePage(Page):
    """This is the page where the competences and Fullfillment criterias are manged
     For one Learning Path"""


    content_panels = Page.content_panels + [
        InlinePanel('competences', label="Competences"),
    ]

    subpage_types = ['learnpath.Circle']

    parent_page_types = ['learnpath.LearningPath']


    class Meta:
        verbose_name = "Learning Path"

    def __str__(self):
        return f"{self.title}"


class Competence(Orderable):
    """ In VBV Terms this is a "Handlungskompetenz"""
    category_short = models.CharField(max_length=3, default='')
    name = models.CharField(max_length=2048)


    competence_page = ParentalKey('learnpath.CompetencePage',
                                null=True,
                                blank=True,
                                on_delete=models.CASCADE,
                                related_name='competences',
                                )



    def get_short_info(self):
        return f"{self.category_short}{self.sort_order}"

    def __str__(self):
        return f"{self.get_short_info()}: {self.name}"


    class Meta:
        verbose_name = "Competence"


class FullfillmentCriteria(Orderable):
    """ VBV Term Leistungskriterium"""
    name = models.CharField(max_length=2048)
    competence = models.ForeignKey(Competence, on_delete=models.CASCADE, null=True)

    def get_short_info(self):
        return f"{self.competence.get_short_info()}.{self.sort_order}"

    def __str__(self):
        return f"{self.get_short_info()}: {self.name}"

    class Meta:
        verbose_name = "Fullfillment Criteria"
