package work_experience

import (
	"context"

	"wechat-api/ent/predicate"
	"wechat-api/ent/workexperience"
	"wechat-api/internal/svc"
	"wechat-api/internal/types"
	"wechat-api/internal/utils/dberrorhandler"

	"github.com/suyuan32/simple-admin-common/msg/errormsg"

	"github.com/suyuan32/simple-admin-common/utils/pointy"
	"github.com/zeromicro/go-zero/core/logx"
)

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

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

func (l *GetWorkExperienceListLogic) GetWorkExperienceList(req *types.WorkExperienceListReq) (*types.WorkExperienceListResp, error) {
	var predicates []predicate.WorkExperience
	if req.Company != nil {
		predicates = append(predicates, workexperience.CompanyContains(*req.Company))
	}
	data, err := l.svcCtx.DB.WorkExperience.Query().Where(predicates...).Page(l.ctx, req.Page, req.PageSize)

	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}

	resp := &types.WorkExperienceListResp{}
	resp.Msg = errormsg.Success
	resp.Data.Total = data.PageDetails.Total

	for _, v := range data.List {
		resp.Data.Data = append(resp.Data.Data,
			types.WorkExperienceInfo{
				BaseIDInfo: types.BaseIDInfo{
					Id:        &v.ID,
					CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
					UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
				},
				EmployeeId: &v.EmployeeID,
				StartDate:  pointy.GetPointer(v.StartDate.UnixMilli()),
				EndDate:    pointy.GetPointer(v.EndDate.UnixMilli()),
				Company:    &v.Company,
				Experience: &v.Experience,
			})
	}

	return resp, nil
}
