package inference

import (
	"context"
	"errors"
	"github.com/zeromicro/go-zero/core/logx"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/common"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
)

type DeployInstanceListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDeployInstanceListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeployInstanceListLogic {
	return &DeployInstanceListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DeployInstanceListLogic) DeployInstanceList(req *types.DeployInstanceListReq) (resp *types.DeployInstanceListResp, err error) {
	limit := req.PageSize
	offset := req.PageSize * (req.PageNum - 1)
	resp = &types.DeployInstanceListResp{}

	var tasklist []*models.AiDeployInstanceTask
	tx := l.svcCtx.DbEngin.Raw("select * from ai_deploy_instance_task").Scan(&tasklist)
	if req.UserName != "" && req.UserName != "admin" {
		tx = tx.Where("user_id = ?", req.UserId)
	}
	if tx.Error != nil {
		logx.Errorf(tx.Error.Error())
		return nil, tx.Error
	}

	if len(tasklist) == 0 {
		resp.List = nil
		resp.PageSize = req.PageSize
		resp.PageNum = req.PageNum
		resp.Total = 0
		return
	}

	//count total
	var total int64
	err = tx.Count(&total).Error
	tx.Limit(limit).Offset(offset)

	if err != nil {
		return resp, err
	}

	err = tx.Order("create_time desc").Find(&tasklist).Error
	if err != nil {
		return nil, errors.New(err.Error())
	}

	deployTasks, err := l.GenerateDeployTasks(tasklist)
	if err != nil {
		return nil, errors.New(err.Error())
	}
	slices := make([][]*models.AiInferDeployInstance, len(deployTasks))
	for i := 0; i < len(deployTasks); i++ {
		slices[i] = deployTasks[i].Instances
	}
	list := common.ConcatMultipleSlices(slices)

	if len(list) != 0 {
		go l.svcCtx.Scheduler.AiService.Si.UpdateDeployInstanceStatusBatch(list, true)
	}

	resp.List = &deployTasks
	resp.PageSize = req.PageSize
	resp.PageNum = req.PageNum
	resp.Total = total

	return
}

func (l *DeployInstanceListLogic) GenerateDeployTasks(tasklist []*models.AiDeployInstanceTask) ([]*DeployTask, error) {
	var tasks []*DeployTask
	for _, t := range tasklist {
		list, err := l.svcCtx.Scheduler.AiStorages.GetInstanceListByDeployTaskId(t.Id)
		if err != nil {
			logx.Errorf("db GetInstanceListByDeployTaskId error")
			return nil, errors.New(err.Error())
		}
		if len(list) == 0 {
			//err := l.svcCtx.Scheduler.AiStorages.DeleteDeployTaskById(t.Id)
			//if err != nil {
			//	logx.Errorf("db DeleteByDeployTaskId error")
			//	return nil, errors.New(err.Error())
			//}
			continue
		}
		deployTask := &DeployTask{
			Id:        t.Id,
			Name:      t.Name,
			Desc:      t.Desc,
			Instances: list,
		}
		tasks = append(tasks, deployTask)
	}
	return tasks, nil
}

type DeployTask struct {
	Id        int64                           `json:"id,string"`
	Name      string                          `json:"name"`
	Desc      string                          `json:"desc"`
	Instances []*models.AiInferDeployInstance `json:"instances,string"`
}
