import functools

from django.db import models

from ichec_django_core.models import Member, TimesStampMixin, content_file_name

from marinerg_data_access.models import Dataset
from marinerg_facility.models import Facility

from .application import AccessApplication


class FacilityTestReport(TimesStampMixin):

    creator = models.ForeignKey(Member, on_delete=models.CASCADE)
    report = models.FileField(
        null=True, upload_to=functools.partial(content_file_name, "report")
    )

    datasets = models.ManyToManyField(
        Dataset,
        verbose_name="Datasets",
        related_name="test_reports",
        blank=True,
    )

    confirmed_complies_data_mgmt = models.BooleanField(default=False)
    application = models.ForeignKey(AccessApplication, on_delete=models.CASCADE)
    facility = models.ForeignKey(Facility, on_delete=models.CASCADE, null=True)

    @property
    def facility_name(self):
        return self.application.chosen_facility.name

    @property
    def facility_acronym(self):
        return self.application.chosen_facility.acronym

    @property
    def creator_contact(self):
        return self.creator.email

    @property
    def applicant_contact(self):
        return self.application.applicant.email

    @property
    def call_title(self):
        return self.application.call.title
