package entity

import (
	"context"
	"gorm.io/gorm"
	"yx-dataset-server/app/schema"
)

// GetDatasetRelationDB 获取 DatasetRelation 存储
func GetDatasetRelationDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
	return getDBWithModel(ctx, defDB, DatasetRelation{})
}

// SchemaDatasetRelation DatasetRelation 对象
type SchemaDatasetRelation schema.DatasetRelation

// ToDatasetRelation 转换为 DatasetRelation 实体
func (a SchemaDatasetRelation) ToDatasetRelation() *DatasetRelation {
	return &DatasetRelation{
		RecordID:  a.RecordID,
		DatasetId: a.DatasetId,
		BizId:     a.BizId,
		Type:      a.Type,
		CreatorId: a.CreatorId,
	}
}

// ToSchemaDatasetRelation 转换为 DatasetRelation 对象
func (a DatasetRelation) ToSchemaDatasetRelation() *schema.DatasetRelation {
	return &schema.DatasetRelation{
		RecordID:  a.RecordID,
		DatasetId: a.DatasetId,
		BizId:     a.BizId,
		Type:      a.Type,
		CreatorId: a.CreatorId,
	}
}

// DatasetRelation 知识库关系映射（统一替代 org_dataset / user_dataset）
type DatasetRelation struct {
	gorm.Model
	RecordID  string `gorm:"column:record_id;size:32;index;"`
	DatasetId string `gorm:"column:dataset_id;size:32;index;"` // 知识库id
	BizId     string `gorm:"column:biz_id;size:32;index;"`     // 业务id：企业id 或 用户id
	Type      int    `gorm:"column:type;index;"`               // 1 公共/共享  2 企业  3 个人
	CreatorId string `gorm:"column:creator_id;size:32;"`       // 关联创建人
}

// DatasetRelations 列表
type DatasetRelations []*DatasetRelation

// ToSchemaDatasetRelations 批量转换
func (a DatasetRelations) ToSchemaDatasetRelations() []*schema.DatasetRelation {
	list := make([]*schema.DatasetRelation, len(a))
	for i, item := range a {
		list[i] = item.ToSchemaDatasetRelation()
	}
	return list
}
