from uuid import uuid4

from clinicedc_constants import YES
from django.core.validators import RegexValidator
from django.db import models
from django.db.models import PROTECT

from edc_constants.choices import YES_NO


class StudyMedicationRefillModelMixin(models.Model):
    refill = models.CharField(
        verbose_name="Will the subject receive study medication for this visit",
        max_length=15,
        default=YES,
        choices=YES_NO,
        help_text="If NO, set refill_start_datetime equal to the refill_end_datetime",
    )

    refill_start_datetime = models.DateTimeField()

    refill_end_datetime = models.DateTimeField(
        null=True,
        blank=True,
        help_text="Leave blank to auto-calculate if refilling to the next scheduled visit",
    )

    refill_identifier = models.CharField(max_length=36, default=uuid4, editable=False)

    dosage_guideline = models.ForeignKey(
        "edc_pharmacy.DosageGuideline", on_delete=PROTECT, null=True, blank=False
    )

    formulation = models.ForeignKey(
        "edc_pharmacy.Formulation", on_delete=PROTECT, null=True, blank=False
    )

    roundup_divisible_by = models.IntegerField(default=1)

    refill_to_next_visit = models.CharField(
        verbose_name="Refill to the next scheduled visit",
        max_length=25,
        choices=YES_NO,
        default=YES,
        help_text="If YES, leave refill end date blank to auto-calculate",
    )

    number_of_days = models.IntegerField(
        null=True,
        blank=True,
        help_text="Leave blank to auto-calculate relative to the next scheduled appointment",
    )

    stock_codes = models.TextField(
        max_length=30,
        default="",
        blank=True,
        validators=[
            RegexValidator(
                # regex="^([A-Z0-9]{6})(,[A-Z0-9]{6})*$",
                regex="^([A-Z0-9]{6})(\r\n[A-Z0-9]{6})*$",
                message="Enter one or more valid codes, one code per line",
            )
        ],
        help_text="Enter the medication bottle barcode or barcodes. Type one code per line",
    )

    special_instructions = models.TextField(blank=True, default="")

    class Meta:
        verbose_name = "Study Medication"
        verbose_name_plural = "Study Medication"
        abstract = True
