package biz

import (
	"ai_scheduler/internal/data/mongo_model"
	"ai_scheduler/internal/entitys"
	"ai_scheduler/internal/pkg"
	"errors"
	"fmt"
	"time"

	"context"

	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
)

type AdviceClientBiz struct {
	AdvicerClientMongo *mongo_model.AdvicerClientMongo
	mongo              *pkg.Mongo
}

func NewAdviceClientBiz(
	advicerClientMongo *mongo_model.AdvicerClientMongo,
	mongo *pkg.Mongo,
) *AdviceClientBiz {
	return &AdviceClientBiz{
		AdvicerClientMongo: advicerClientMongo,
		mongo:              mongo,
	}
}

func (a *AdviceClientBiz) Add(ctx context.Context, param *entitys.AdvicerClientAddReq) (id interface{}, err error) {

	res, err := a.mongo.Co(a.AdvicerClientMongo).InsertOne(ctx, &mongo_model.AdvicerClientMongo{
		ProjectId:       param.ProjectId,
		AdvicerId:       param.AdvicerId,
		PersonalInfo:    param.PersonalInfo,
		PurchasePurpose: param.PurchasePurpose,
		CoreDemands:     param.CoreDemands,
		Concerns:        param.Concerns,
		DecisionProfile: param.DecisionProfile,
		LastUpdateTime:  time.Now(),
	})
	if err != nil {
		return nil, err
	}
	return res.InsertedID, err
}

func (a *AdviceClientBiz) Update(ctx context.Context, param *entitys.AdvicerrClientUpdateReq) (err error) {
	filter := bson.M{}
	if len(param.Id) == 0 {
		return errors.New("ID不能为空")
	}
	objectID, err := primitive.ObjectIDFromHex(param.Id)
	if err != nil {
		return fmt.Errorf("ID转换失败: %w", err)
	}
	filter["_id"] = objectID
	update := bson.M{
		"$set": &mongo_model.AdvicerClientMongo{
			ProjectId:       param.ProjectId,
			AdvicerId:       param.AdvicerId,
			PersonalInfo:    param.PersonalInfo,
			PurchasePurpose: param.PurchasePurpose,
			CoreDemands:     param.CoreDemands,
			Concerns:        param.Concerns,
			DecisionProfile: param.DecisionProfile,
			LastUpdateTime:  time.Now(),
		},
	}
	res := a.mongo.Co(a.AdvicerClientMongo).FindOneAndUpdate(ctx, filter, update)
	return res.Err()
}

func (a *AdviceClientBiz) List(ctx context.Context, param *entitys.AdvicerClientListReq) (list []mongo_model.AdvicerClientMongo, err error) {
	filter := bson.M{}
	// 1. advicer_id 条件
	if param.AdvicerId != 0 {
		filter["AdvicerId"] = param.AdvicerId
	}

	if param.ProjectId != 0 {
		filter["projectId"] = param.ProjectId
	}

	// 2. _id 条件
	if len(param.Id) != 0 {
		objectID, err := primitive.ObjectIDFromHex(param.Id)
		if err != nil {
			return nil, fmt.Errorf("ID转换失败: %w", err)
		}
		filter["_id"] = objectID
	}

	cursor, err := a.mongo.Co(a.AdvicerClientMongo).Find(ctx, filter)
	if err != nil {
		return nil, err
	}
	// 遍历结果
	for cursor.Next(ctx) {
		var advicerVersion mongo_model.AdvicerClientMongo
		if err := cursor.Decode(&advicerVersion); err != nil {
			return nil, err
		}
		list = append(list, advicerVersion)
	}

	if err := cursor.Err(); err != nil {
		return nil, err
	}
	return list, err
}

func (a *AdviceClientBiz) Del(ctx context.Context, param *entitys.AdvicerClientDelReq) (err error) {
	filter := bson.M{}

	if len(param.Id) != 0 {
		objectID, err := primitive.ObjectIDFromHex(param.Id)
		if err != nil {
			return fmt.Errorf("ID转换失败: %w", err)
		}
		filter["_id"] = objectID
	}

	_, err = a.mongo.Co(a.AdvicerClientMongo).DeleteOne(ctx, filter)

	return err
}

func (a *AdviceClientBiz) Info(ctx context.Context, param *entitys.AdvicerClientInfoReq) (info mongo_model.AdvicerClientMongo, err error) {
	filter := bson.M{}

	if len(param.Id) != 0 {
		objectID, err := primitive.ObjectIDFromHex(param.Id)
		if err != nil {
			return info, fmt.Errorf("ID转换失败: %w", err)

		}
		filter["_id"] = objectID
	}

	res := a.mongo.Co(a.AdvicerClientMongo).FindOne(ctx, filter)
	if res.Err() != nil {
		return info, res.Err()
	}

	if err = res.Decode(&info); err != nil {
		return info, err
	}
	return
}
