package api

import (
	"fmt"
	"scnict/db/model"
	"scnict/db/repo"
	"strings"

	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson"
)

func Move(r *GinRouter) {
	r.GET("/move", DbMove)
}

var old_url = "https://scnict.oss-cn-chengdu.aliyuncs.com/"
var new_url = "https://oss.scnict.com/"

func DbMove(c *gin.Context, apictx *ApiSession) (interface{}, error) {
	// banner
	banners := []*model.NictBanner{}
	repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
		CollectName: repo.CollectionBanners,
		Project:     []string{"image"},
	}, &banners)
	if len(banners) > 0 {
		for _, banner := range banners {
			image := strings.ReplaceAll(banner.Image, old_url, new_url)
			repo.RepoUpdateSetDocProps(apictx.CreateRepoCtx(), repo.CollectionBanners, banner.Id.Hex(), bson.M{"$set": bson.M{"image": image}})
		}

	}

	// news
	news := []*model.NictNews{}
	repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
		CollectName: repo.CollectionNews,
		Project:     []string{"thumbnail", "detail"},
	}, &news)
	if len(news) > 0 {
		for _, new := range news {
			thumbnail := strings.ReplaceAll(new.Thumbnail, old_url, new_url)
			detail := strings.ReplaceAll(new.Detail, old_url, new_url)
			repo.RepoUpdateSetDocProps(apictx.CreateRepoCtx(), repo.CollectionNews, new.Id.Hex(), bson.M{"$set": bson.M{"thumbnail": thumbnail, "detail": detail}})
		}

	}

	// partners
	partners := []*model.NictPartner{}
	repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
		CollectName: repo.CollectionPartners,
		Project:     []string{"image"},
	}, &partners)
	if len(partners) > 0 {
		for _, partner := range partners {
			image := strings.ReplaceAll(partner.Image, old_url, new_url)
			repo.RepoUpdateSetDocProps(apictx.CreateRepoCtx(), repo.CollectionPartners, partner.Id.Hex(), bson.M{"$set": bson.M{"image": image}})
		}

	}

	// parts
	parts := []*model.NictParts{}
	repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
		CollectName: repo.CollectionParts,
		Project:     []string{"thumbnail", "detail"},
	}, &parts)
	if len(parts) > 0 {
		for _, part := range parts {
			thumbnail := strings.ReplaceAll(part.Thumbnail, old_url, new_url)
			detail := strings.ReplaceAll(part.Detail, old_url, new_url)
			repo.RepoUpdateSetDocProps(apictx.CreateRepoCtx(), repo.CollectionParts, part.Id.Hex(), bson.M{"$set": bson.M{"thumbnail": thumbnail, "detail": detail}})
		}

	}

	// products
	products := []*model.NictProduct{}
	repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
		CollectName: repo.CollectionProducts,
		Project:     []string{"detail"},
	}, &products)
	if len(products) > 0 {
		for _, product := range products {
			detail := strings.ReplaceAll(product.Detail, old_url, new_url)
			repo.RepoUpdateSetDocProps(apictx.CreateRepoCtx(), repo.CollectionProducts, product.Id.Hex(), bson.M{"$set": bson.M{"detail": detail}})
		}

	}

	// pumps
	pumps := []*model.NictPump{}
	repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
		CollectName: repo.CollectionPumps,
		Project:     []string{"thumbnail", "detail"},
	}, &pumps)
	if len(pumps) > 0 {
		for _, pump := range pumps {
			thumbnail := strings.ReplaceAll(pump.Thumbnail, old_url, new_url)
			detail := strings.ReplaceAll(pump.Detail, old_url, new_url)
			repo.RepoUpdateSetDocProps(apictx.CreateRepoCtx(), repo.CollectionPumps, pump.Id.Hex(), bson.M{"$set": bson.M{"thumbnail": thumbnail, "detail": detail}})
		}

	}

	fmt.Println("end")
	return nil, nil
}
