package model

import (
	"gorm.io/gorm"
)

// Tag 标签模型
// 用于设备、IoT卡、号卡的分类标记，支持自定义颜色
// 支持企业、店铺、平台三级隔离
type Tag struct {
	gorm.Model
	BaseModel    `gorm:"embedded"`
	Name         string  `gorm:"column:name;type:varchar(100);not null;uniqueIndex:idx_tag_enterprise_name,priority:2;uniqueIndex:idx_tag_shop_name,priority:2;uniqueIndex:idx_tag_global_name;comment:标签名称" json:"name"`
	EnterpriseID *uint   `gorm:"column:enterprise_id;index:idx_tag_enterprise;uniqueIndex:idx_tag_enterprise_name,priority:1;comment:归属企业ID（NULL表示非企业标签）" json:"enterprise_id,omitempty"`
	ShopID       *uint   `gorm:"column:shop_id;index:idx_tag_shop;uniqueIndex:idx_tag_shop_name,priority:1;comment:归属店铺ID（NULL表示非店铺标签）" json:"shop_id,omitempty"`
	Color        *string `gorm:"column:color;type:varchar(20);comment:标签颜色（十六进制）" json:"color,omitempty"`
	UsageCount   int     `gorm:"column:usage_count;type:int;not null;default:0;index:idx_tag_usage;comment:使用次数" json:"usage_count"`
}

// TableName 指定表名
func (Tag) TableName() string {
	return "tb_tag"
}

// ResourceTag 资源-标签关联模型
// 统一管理设备、IoT卡、号卡与标签的多对多关系
// 添加 enterprise_id 和 shop_id 用于权限控制，从资源所有者推断
type ResourceTag struct {
	gorm.Model
	BaseModel    `gorm:"embedded"`
	ResourceType string `gorm:"column:resource_type;type:varchar(20);not null;uniqueIndex:idx_resource_tag_unique,priority:1,where:deleted_at IS NULL;index:idx_resource_tag_resource,priority:1;index:idx_resource_tag_composite,priority:1;comment:资源类型 device-设备 iot_card-IoT卡 number_card-号卡" json:"resource_type"`
	ResourceID   uint   `gorm:"column:resource_id;not null;uniqueIndex:idx_resource_tag_unique,priority:2,where:deleted_at IS NULL;index:idx_resource_tag_resource,priority:2;comment:资源ID" json:"resource_id"`
	TagID        uint   `gorm:"column:tag_id;not null;uniqueIndex:idx_resource_tag_unique,priority:3,where:deleted_at IS NULL;index:idx_resource_tag_tag;index:idx_resource_tag_composite,priority:2;comment:标签ID" json:"tag_id"`
	EnterpriseID *uint  `gorm:"column:enterprise_id;index:idx_resource_tag_enterprise;comment:归属企业ID（从资源推断）" json:"enterprise_id,omitempty"`
	ShopID       *uint  `gorm:"column:shop_id;index:idx_resource_tag_shop;comment:归属店铺ID（从资源推断）" json:"shop_id,omitempty"`
}

// TableName 指定表名
func (ResourceTag) TableName() string {
	return "tb_resource_tag"
}
