from django.db import models
from django.db.models.constraints import UniqueConstraint
import ulid
from XCMS_PIM.models import WorkspaceUser
from app.models.product_classification_model import ProductClassificationModel
from app.models.attributes_model import AttributesModel
from app.models.channel_model import ChannelModel


class ProductClassificationChannelAttributeModel(models.Model):
    id = models.BigIntegerField(primary_key=True)
    product_classification = models.ForeignKey(
        ProductClassificationModel, on_delete=models.DO_NOTHING, to_field="cd", db_column="product_classification_cd"
    )
    channel = models.ForeignKey(ChannelModel, on_delete=models.DO_NOTHING, to_field="cd", db_column="channel_cd")
    attribute = models.ForeignKey(AttributesModel, on_delete=models.DO_NOTHING, to_field="cd", db_column="attribute_cd")
    transformation = models.CharField(max_length=1)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    created_by = models.ForeignKey(
        WorkspaceUser,
        related_name="created_product_classification_channel_attributes",
        on_delete=models.DO_NOTHING,
        to_field="user_uid",
        db_column="created_by",
    )
    updated_by = models.ForeignKey(
        WorkspaceUser,
        related_name="updated_product_classification_channel_attributes",
        on_delete=models.DO_NOTHING,
        to_field="user_uid",
        db_column="updated_by",
    )

    class Meta:
        managed = False
        db_table = "product_classification_channel_attributes"
        constraints = [
            models.UniqueConstraint(
                fields=["product_classification_id", "channel_id", "attribute_id"],
                name="unique_product_classification_channel_attribute",
            )
        ]
