from django.db import models
from django.db.models.constraints import UniqueConstraint
import ulid
from XCMS_PIM.models import WorkspaceUser
from app.models.attributes_model import AttributesModel
from app.models.prd_sku_model import PrdSKUModel
from django.db.models import Q


class ULIDField(models.CharField):
    def __init__(self, *args, **kwargs):
        kwargs["max_length"] = 26
        super(ULIDField, self).__init__(*args, **kwargs)

    def db_type(self, connection):
        return "char(26)"


class ProductClassificationModel(models.Model):
    cd = ULIDField(default=ulid.new(), primary_key=True)
    name = models.CharField(max_length=256)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    is_deleted = models.CharField(max_length=1)
    created_by = models.ForeignKey(
        WorkspaceUser,
        related_name="created_product_classification",
        on_delete=models.DO_NOTHING,
        to_field="user_uid",
        db_column="created_by",
    )
    updated_by = models.ForeignKey(
        WorkspaceUser,
        related_name="updated_product_classification",
        on_delete=models.DO_NOTHING,
        to_field="user_uid",
        db_column="updated_by",
    )
    attributes = models.ManyToManyField(
        AttributesModel, related_name="attributes", through="ProductClassificationAttributeModel"
    )
    products = models.ManyToManyField(
        PrdSKUModel, related_name="product_classifications", through="ProductClassificationSkuModel"
    )

    def get_attributes(self, exclude_private=False):
        filter = Q(product_classification=self)
        if exclude_private:
            filter = filter & ~Q(is_private="1")
        return ProductClassificationAttributeModel.objects.filter(filter).order_by("order", "-attribute_id")

    class Meta:
        managed = False
        db_table = "product_classfications"
        UniqueConstraint(fields=["cd"], name="product_classfication_unique")
        UniqueConstraint(fields=["name"], name="product_classfication_unique_name")


class ProductClassificationAttributeModel(models.Model):
    product_classification = models.ForeignKey(
        ProductClassificationModel,
        related_name="product_classification_code",
        on_delete=models.DO_NOTHING,
        to_field="cd",
        db_column="product_classification_cd",
    )
    attribute = models.ForeignKey(
        AttributesModel,
        related_name="attribute_code",
        on_delete=models.DO_NOTHING,
        to_field="cd",
        db_column="attribute_cd",
    )
    is_deleted = models.CharField(max_length=1)
    is_fixed = models.CharField(max_length=1)
    is_common = models.CharField(max_length=1)
    is_with_unit = models.CharField(max_length=1)
    is_private = models.CharField(max_length=1)
    order = models.IntegerField()
    name = models.CharField(max_length=256)
    alternative_name = models.CharField(max_length=256)
    control_type = models.CharField(max_length=1)
    not_null = models.CharField(max_length=1)
    max_length = models.IntegerField()
    select_list = models.CharField(max_length=1024)
    default_value = models.CharField(max_length=256)
    unit = models.CharField(max_length=100)
    option1 = models.CharField(max_length=45)
    option2 = models.CharField(max_length=45)
    option3 = models.CharField(max_length=45)

    class Meta:
        managed = False
        db_table = "product_classification_attributes"


class ProductClassificationSkuModel(models.Model):
    product_classification_cd = models.ForeignKey(
        ProductClassificationModel,
        related_name="product_classifications_code",
        on_delete=models.DO_NOTHING,
        to_field="cd",
        db_column="product_classification_cd",
    )
    sku_cd = models.ForeignKey(
        PrdSKUModel, related_name="sku_code", on_delete=models.DO_NOTHING, to_field="prd_mng_id", db_column="sku_cd"
    )

    class Meta:
        managed = False
        db_table = "product_classifications_sku"
