# Copyright (C) 2018-2025 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later

from django.db import models
from django.utils.translation import gettext_lazy as _
from note.models import Note


class Bde(models.Model):
    """
    describe a BDE
    """

    name = models.CharField(
        max_length=255,
        verbose_name=_('name'),
    )

    date_start = models.DateTimeField(
        verbose_name=_('date start'),
    )

    date_end = models.DateTimeField(
        verbose_name=_('date end'),
    )

    class Meta:
        verbose_name = _('BDE')
        verbose_name_plural = _('BDE')

    def __str__(self):
        return self.name


class Wrapped(models.Model):
    """
    A Wrapped is associated to a note, a BDE year,
    """
    generated = models.BooleanField(
        verbose_name=_('generated'),
        default=False,
    )

    public = models.BooleanField(
        verbose_name=_('public'),
        default=False,
    )

    bde = models.ForeignKey(
        Bde,
        on_delete=models.PROTECT,
        related_name='+',
        verbose_name=_('bde'),
    )

    note = models.ForeignKey(
        Note,
        on_delete=models.PROTECT,
        related_name='+',
        verbose_name=_('note'),
    )

    data_json = models.TextField(
        default='{}',
        verbose_name=_('data json'),
        help_text=_('data in the wrapped and generated by the script generate_wrapped'),
    )

    class Meta:
        verbose_name = _('Wrapped')
        verbose_name_plural = _('Wrappeds')
        unique_together = ('note', 'bde')

    def __str__(self):
        return 'NoteKfet Wrapped of {note} sponsored by {bde}'.format(bde=str(self.bde), note=str(self.note))

    def makepublic(self):
        self.public = not self.public
        self.save()
        return
