package job

import (
	"context"

	domusecase "haixun-backend/internal/model/job/domain/usecase"
	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"
)

type UpdateJobScheduleLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateJobScheduleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateJobScheduleLogic {
	return &UpdateJobScheduleLogic{ctx: ctx, svcCtx: svcCtx}
}

func (l *UpdateJobScheduleLogic) UpdateJobSchedule(req *types.UpdateJobScheduleReq) (*types.JobScheduleData, error) {
	schedule, err := l.svcCtx.Job.UpdateSchedule(l.ctx, domusecase.UpdateScheduleRequest{
		ID:              req.ID,
		Cron:            req.Cron,
		Timezone:        req.Timezone,
		PayloadTemplate: req.PayloadTemplate,
		Enabled:         req.Enabled,
	})
	if err != nil {
		return nil, err
	}
	data := toJobScheduleData(schedule)
	return &data, nil
}
