package logic

import (
	plantModel "sundynix-micro-go/app/plant/model"
	"sundynix-micro-go/app/plant/rpc/plant"

	"gorm.io/gorm"
)

func wikiRelationIDs(db *gorm.DB, wikiID string) (classIDs, ossIDs, relatedWikiIDs []string) {
	var classes []plantModel.WikiClassRelation
	_ = db.Where("wiki_id = ?", wikiID).Find(&classes).Error
	for _, item := range classes {
		classIDs = append(classIDs, item.ClassID)
	}

	var ossList []plantModel.WikiOss
	_ = db.Where("wiki_id = ?", wikiID).Find(&ossList).Error
	for _, item := range ossList {
		ossIDs = append(ossIDs, item.OssID)
	}

	var related []plantModel.WikiRelated
	_ = db.Where("wiki_id = ?", wikiID).Find(&related).Error
	for _, item := range related {
		relatedWikiIDs = append(relatedWikiIDs, item.RelatedWikiID)
	}
	return
}

func wikiInfoFromModel(db *gorm.DB, item plantModel.Wiki) *plant.WikiInfo {
	classIDs, ossIDs, relatedWikiIDs := wikiRelationIDs(db, item.ID)
	return &plant.WikiInfo{
		Id:                 item.ID,
		Name:               item.Name,
		LatinName:          item.LatinName,
		Aliases:            item.Aliases,
		Genus:              item.Genus,
		Difficulty:         int32(item.Difficulty),
		IsHot:              int32(item.IsHot),
		GrowthHabit:        item.GrowthHabit,
		LightIntensity:     item.LightIntensity,
		OptimalTempPeriod:  item.OptimalTempPeriod,
		CreatedAt:          item.CreatedAt.Unix(),
		ClassId:            item.ClassID,
		DistributionArea:   item.DistributionArea,
		LifeCycle:          item.LifeCycle,
		ClassIds:           classIDs,
		OssIds:             ossIDs,
		RelatedWikiIds:     relatedWikiIDs,
		IsVectorSynced:     item.IsVectorSynced == 1,
		ReproductionMethod: item.ReproductionMethod,
		PestsDiseases:      item.PestsDiseases,
		LightType:          item.LightType,
		Stem:               item.Stem,
		Fruit:              item.Fruit,
		FoliageType:        item.FoliageType,
		FoliageColor:       item.FoliageColor,
		FoliageShape:       item.FoliageShape,
		Height:             int32(item.Height),
		FloweringPeriod:    item.FloweringPeriod,
		FloweringColor:     item.FloweringColor,
		FloweringShape:     item.FloweringShape,
		FloweringDiameter:  int32(item.FlowerDiameter),
	}
}

func replaceWikiRelations(tx *gorm.DB, wikiID string, classIDs, ossIDs, relatedWikiIDs []string) error {
	if err := tx.Where("wiki_id = ?", wikiID).Delete(&plantModel.WikiClassRelation{}).Error; err != nil {
		return err
	}
	if err := tx.Where("wiki_id = ?", wikiID).Delete(&plantModel.WikiOss{}).Error; err != nil {
		return err
	}
	if err := tx.Where("wiki_id = ?", wikiID).Delete(&plantModel.WikiRelated{}).Error; err != nil {
		return err
	}

	for _, classID := range classIDs {
		if classID == "" {
			continue
		}
		if err := tx.Create(&plantModel.WikiClassRelation{WikiID: wikiID, ClassID: classID}).Error; err != nil {
			return err
		}
	}
	for _, ossID := range ossIDs {
		if ossID == "" {
			continue
		}
		if err := tx.Create(&plantModel.WikiOss{WikiID: wikiID, OssID: ossID}).Error; err != nil {
			return err
		}
	}
	for _, relatedID := range relatedWikiIDs {
		if relatedID == "" || relatedID == wikiID {
			continue
		}
		if err := tx.Create(&plantModel.WikiRelated{WikiID: wikiID, RelatedWikiID: relatedID}).Error; err != nil {
			return err
		}
	}
	return nil
}
