from clinicedc_constants import NO, NOT_APPLICABLE, OTHER
from django.db import models
from edc_constants.choices import YES_NO
from edc_glucose.model_mixin_factories import (
    fasting_model_mixin_factory,
    fbg_model_mixin_factory,
)
from edc_model.models import BaseUuidModel
from edc_utils import formatted_date

from meta_lists.constants import ACCUCHEK, HEMACUE
from meta_lists.models import DiagnosticDevices

from ..choices import ENDPOINT_CHOICES
from ..constants import AMENDMENT_DATE
from ..model_mixins import CrfModelMixin


class GlucoseFbg(
    CrfModelMixin,
    fasting_model_mixin_factory(
        None,
        fasting=models.CharField(
            verbose_name="Has the participant fasted?",
            max_length=15,
            choices=YES_NO,
            null=True,
            blank=False,
            help_text="As reported by patient",
        ),
    ),
    fbg_model_mixin_factory("fbg"),
    BaseUuidModel,
):
    fbg_performed = models.CharField(
        verbose_name="Was the FBG test performed?",
        max_length=15,
        choices=YES_NO,
    )

    fbg_not_performed_reason = models.CharField(
        verbose_name="If NO, provide reason",
        max_length=150,
        default="",
        blank=True,
    )

    fasting_duration_estimated = models.CharField(
        max_length=15,
        default=NO,
        editable=False,
        help_text=(
            "Set to YES for existing values before duration "
            "question was added to the form, otherwise NO"
        ),
    )

    endpoint_today = models.CharField(
        verbose_name="Has the participant reached a study endpoint today?",
        max_length=25,
        choices=ENDPOINT_CHOICES,
        default=NOT_APPLICABLE,
        help_text=(
            f"Response is applicable if reporting after {formatted_date(AMENDMENT_DATE)} "
            "and both the FBG and OGTT were performed"
        ),
    )

    repeat_fbg_date = models.DateField(
        null=True,
        blank=True,
        help_text="Date should be within 1 week of report date",
    )

    fbg_diagnostic_device = models.ForeignKey(
        DiagnosticDevices,
        on_delete=models.PROTECT,
        null=True,
        blank=False,
        limit_choices_to={"name__in": [ACCUCHEK, HEMACUE, OTHER, NOT_APPLICABLE]},
    )

    class Meta(CrfModelMixin.Meta, BaseUuidModel.Meta):
        verbose_name = "Glucose (FBG)"
        verbose_name_plural = "Glucose (FBG)"
