from django.db import models
from admin_ordering.models import OrderableModel
from filebrowser.fields import FileBrowseField
from core.utils import SingletonModel
from tinymce.models import HTMLField

# Create your models here.


class AttachmentModel(OrderableModel):
    title = models.CharField('Tytuł', max_length=250)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

    def _upload_to():
        return None

    def upload_to():
        return 'zalaczniki'

    attachment = FileBrowseField(
        'Plik pdf', directory=upload_to, max_length=200, extensions=['.pdf', '.doc'])

    class Meta(OrderableModel.Meta):
        abstract = True


class Announcement(AttachmentModel):
    def upload_to():
        return 'zarzad_ogloszenia'

    class Meta(OrderableModel.Meta):
        verbose_name = 'Ogłoszenie'
        verbose_name_plural = 'Ogłoszenia'


class Data(SingletonModel):
    content = HTMLField('Dane zarządu', default='', blank=True)

    def __str__(self):
        return 'Dane zarządu'

    class Meta:
        verbose_name = 'Dane zarządu'
        verbose_name_plural = 'Dane zarządu'


class Member(OrderableModel):
    name = models.CharField('Imię i nazwisko', max_length=512)
    contact = models.CharField('Dane kontaktowe', max_length=512)
    function = models.CharField('Funkcja', max_length=512)

    def __str__(self):
        return f'{self.function} - {self.name}'

    class Meta(OrderableModel.Meta):
        verbose_name = 'Członek'
        verbose_name_plural = 'Członkowie'


class Protocol(AttachmentModel):
    def upload_to():
        return 'zarzad_protokolyuchwaly'

    extra = models.TextField('Dodatkowy opis', default='', blank=True)
    year = models.IntegerField('Rok', blank=True, null=True)

    class Meta(OrderableModel.Meta):
        verbose_name = 'Protokół / Uchwała'
        verbose_name_plural = 'Protokoły / Uchwały'
        ordering = ['-year', 'ordering']


class Regulation(AttachmentModel):
    def upload_to():
        return 'zarzad_regulaminy'

    class Meta(OrderableModel.Meta):
        verbose_name = 'Regulamin'
        verbose_name_plural = 'Regulaminy'


class RODO(models.Model):
    who = models.CharField(
        'Przez kogo przetwarzane dane osobowe?', max_length=512)
    attachment = FileBrowseField(
        'Plik pdf', directory='zarzad_rodo', max_length=200, extensions=['.pdf', '.doc'])

    def __str__(self):
        return f'RODO - {self.who}'

    class Meta:
        verbose_name = 'Dokument RODO'
        verbose_name_plural = 'Dokumenty RODO'


class Statute(SingletonModel):
    title = models.CharField('Tytuł', max_length=250)
    attachment = FileBrowseField(
        'Plik pdf', directory='zarzad_statuty', max_length=200, extensions=['.pdf', '.doc'])

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = 'Statut'
        verbose_name_plural = 'Statut'
