from django.db import models

from . import managers


class VTM(models.Model):
    class Meta:
        verbose_name = "Virtual Therapeutic Moiety"
        verbose_name_plural = "Virtual Therapeutic Moieties"
        ordering = ["nm"]

    objects = managers.VTMManager()

    obj_type = "vtm"
    name_field = "nm"

    def __str__(self):
        return str(self.id)

    id = models.BigIntegerField(
        primary_key=True,
        db_column="vtmid",
        help_text="Identifier",
    )
    invalid = models.BooleanField(
        help_text="Invalid",
    )
    nm = models.CharField(
        max_length=255,
        help_text="Name",
    )
    abbrevnm = models.CharField(
        max_length=60,
        null=True,
        help_text="Abbreviated name",
    )
    vtmidprev = models.BigIntegerField(
        null=True,
        help_text="Previous identifier",
    )
    vtmiddt = models.DateField(
        null=True,
        help_text="VTM identifier date",
    )

    def title(self):
        return self.nm


class VMP(models.Model):
    class Meta:
        verbose_name = "Virtual Medicinal Product"
        ordering = ["nm"]

    objects = managers.VMPManager()

    obj_type = "vmp"
    name_field = "nm"

    def __str__(self):
        return str(self.id)

    id = models.BigIntegerField(
        primary_key=True,
        db_column="vpid",
        help_text="Identifier",
    )
    vpiddt = models.DateField(
        null=True,
        help_text="Date identifier became valid",
    )
    vpidprev = models.BigIntegerField(
        null=True,
        help_text="Previous product identifier",
    )
    vtm = models.ForeignKey(
        db_column="vtmid",
        to="VTM",
        on_delete=models.CASCADE,
        null=True,
        help_text="VTM",
    )
    invalid = models.BooleanField(
        help_text="Invalid",
    )
    nm = models.CharField(
        max_length=255,
        help_text="Name",
    )
    abbrevnm = models.CharField(
        max_length=60,
        null=True,
        help_text="Abbreviated name",
    )
    basis = models.ForeignKey(
        db_column="basiscd",
        to="BasisOfName",
        on_delete=models.CASCADE,
        help_text="Basis of preferred name",
    )
    nmdt = models.DateField(
        null=True,
        help_text="Date of name applicability",
    )
    nmprev = models.CharField(
        max_length=255,
        null=True,
        help_text="Previous name",
    )
    basis_prev = models.ForeignKey(
        db_column="basis_prevcd",
        to="BasisOfName",
        on_delete=models.CASCADE,
        related_name="+",
        null=True,
        help_text="Basis of previous name",
    )
    nmchange = models.ForeignKey(
        db_column="nmchangecd",
        to="NamechangeReason",
        on_delete=models.CASCADE,
        null=True,
        help_text="Reason for name change",
    )
    combprod = models.ForeignKey(
        db_column="combprodcd",
        to="CombinationProdInd",
        on_delete=models.CASCADE,
        null=True,
        help_text="Combination product",
    )
    pres_stat = models.ForeignKey(
        db_column="pres_statcd",
        to="VirtualProductPresStatus",
        on_delete=models.CASCADE,
        help_text="Prescribing status",
    )
    sug_f = models.BooleanField(
        help_text="Sugar free",
    )
    glu_f = models.BooleanField(
        help_text="Gluten free",
    )
    pres_f = models.BooleanField(
        help_text="Preservative free",
    )
    cfc_f = models.BooleanField(
        help_text="CFC free",
    )
    non_avail = models.ForeignKey(
        db_column="non_availcd",
        to="VirtualProductNonAvail",
        on_delete=models.CASCADE,
        null=True,
        help_text="Non-availability",
    )
    non_availdt = models.DateField(
        null=True,
        help_text="Non-availability status date",
    )
    df_ind = models.ForeignKey(
        db_column="df_indcd",
        to="DfIndicator",
        on_delete=models.CASCADE,
        null=True,
        help_text="Dose form",
    )
    udfs = models.DecimalField(
        max_digits=10,
        decimal_places=3,
        null=True,
        help_text="Unit dose form size",
    )
    udfs_uom = models.ForeignKey(
        db_column="udfs_uomcd",
        to="UnitOfMeasure",
        on_delete=models.CASCADE,
        related_name="+",
        null=True,
        help_text="Unit dose form units",
    )
    unit_dose_uom = models.ForeignKey(
        db_column="unit_dose_uomcd",
        to="UnitOfMeasure",
        on_delete=models.CASCADE,
        related_name="+",
        null=True,
        help_text="Unit dose unit of measure",
    )
    bnf_code = models.CharField(max_length=15, null=True)

    def title(self):
        return self.nm


class VPI(models.Model):
    class Meta:
        verbose_name = "Virtual Product Ingredient"

    vmp = models.ForeignKey(
        db_column="vpid",
        to="VMP",
        on_delete=models.CASCADE,
        help_text="VMP",
    )
    ing = models.ForeignKey(
        db_column="isid",
        to="Ing",
        on_delete=models.CASCADE,
        help_text="Ingredient",
    )
    basis_strnt = models.ForeignKey(
        db_column="basis_strntcd",
        to="BasisOfStrnth",
        on_delete=models.CASCADE,
        null=True,
        help_text="Basis of pharmaceutical strength",
    )
    bs_subid = models.BigIntegerField(
        null=True,
        help_text="Basis of strength substance identifier",
    )
    strnt_nmrtr_val = models.DecimalField(
        max_digits=10,
        decimal_places=3,
        null=True,
        help_text="Strength value numerator",
    )
    strnt_nmrtr_uom = models.ForeignKey(
        db_col
umn="strnt_nmrtr_uomcd",
        to="UnitOfMeasure",
        on_delete=models.CASCADE,
        related_name="+",
        null=True,
        help_text="Strength value numerator unit",
    )
    strnt_dnmtr_val = models.DecimalField(
        max_digits=10,
        decimal_places=3,
        null=True,
        help_text="Strength value denominator",

    )
    strnt_dnmtr_uom = models.ForeignKey(
        db_column="strnt_dnmtr_uomcd",
        to="UnitOfMeasure",
        on_delete=models.CASCADE,
        related_name="+",
        null=True,
        help_text="Strength value denominator unit",
    )


class Ont(models.Model):
    class Meta:
        verbose_name = "Ontology Drug Form & Route"

    vmp = models.ForeignKey(
        db_column="vpid",
        to="VMP",
        on_delete=models.CASCADE,
        help_text="VMP",
    )
    form = models.ForeignKey(
        db_column="formcd",
        to="OntFormRoute",
        on_delete=models.CASCADE,
        help_text="Form & Route",
    )


class Dform(models.Model):
    class Meta:
        verbose_name = "Dose Form"

    vmp = models.OneToOneField(
        db_column="vpid",
        to="VMP",
        on_delete=models.CASCADE,
        help_text="VMP",
    )
    form = models.ForeignKey(
        db_column="formcd",
        to="Form",
        on_delete=models.CASCADE,
        help_text="Formulation",
    )


class Droute(models.Model):
    class Meta:
        verbose_name = "Drug Route"

    vmp = models.ForeignKey(
        db_column="vpid",
        to="VMP",
        on_delete=models.CASCADE,
        help_text="VMP",
    )
    route = models.ForeignKey(
        db_column="routecd",
        to="Route",
        on_delete=models.CASCADE,
        help_text="Route",
    )


class ControlInfo(models.Model):
    class Meta:
        verbose_name = "Controlled Drug Prescribing Information"

    vmp = models.OneToOneField(
        db_column="vpid",
        to="VMP",
        on_delete=models.CASCADE,
        help_text="VMP",
    )
    cat = models.ForeignKey(
        db_column="catcd",
        to="ControlDrugCategory",
        on_delete=models.CASCADE,
        help_text="Controlled Drug category",
    )
    catdt = models.DateField(
        null=True,
        help_text="Date of applicability",
    )
    cat_prev = models.ForeignKey(
        db_column="cat_prevcd",
        to="ControlDrugCategory",
        on_delete=models.CASCADE,
        related_name="+",
        null=True,
        help_text="Previous Controlled Drug information",
    )


class AM