// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package task

import (
	stdctx "context"
	"fmt"
	"time"

	gitea "gitea.dev/sdk"

	"gitea.dev/tea/modules/config"
	"gitea.dev/tea/modules/print"
)

// CreateMilestone creates a milestone in the given repo and prints the result
func CreateMilestone(ctx stdctx.Context, login *config.Login, repoOwner, repoName, title, description string, deadline *time.Time, state gitea.StateType) error {
	// title is required
	if len(title) == 0 {
		return fmt.Errorf("title is required")
	}

	mile, _, err := login.Client().Repositories.CreateMilestone(ctx, repoOwner, repoName, gitea.CreateMilestoneOption{
		Title:       title,
		Description: description,
		Deadline:    deadline,
		State:       state,
	})
	if err != nil {
		return err
	}

	print.MilestoneDetails(mile)
	return nil
}
