package milestone

import (
	"fmt"
	"net/url"

	"github.com/gitlink-org/gitlink-cli/shortcuts/common"
)

func Shortcuts() []*common.Shortcut {
	return []*common.Shortcut{
		{
			Name:        "list",
			Description: "List milestones",
			Flags: []common.Flag{
				{Name: "page", Short: "p", Usage: "Page number", Default: "1"},
				{Name: "limit", Short: "l", Usage: "Items per page", Default: "20"},
			},
			Run: func(ctx *common.RuntimeContext) error {
				if err := ctx.ResolveOwnerRepo(); err != nil {
					return err
				}
				q := url.Values{}
				q.Set("page", ctx.Arg("page"))
				q.Set("limit", ctx.Arg("limit"))
				env, err := ctx.CallAPIWithQuery("GET", fmt.Sprintf("/v1%s/milestones", ctx.RepoPath()), q)
				if err != nil {
					return err
				}
				return ctx.Output(env)
			},
		},
		{
			Name:        "create",
			Description: "Create a milestone",
			Flags: []common.Flag{
				{Name: "name", Short: "n", Usage: "Milestone name", Required: true},
				{Name: "description", Short: "d", Usage: "Description"},
				{Name: "due", Usage: "Due date (YYYY-MM-DD)"},
			},
			Run: func(ctx *common.RuntimeContext) error {
				if err := ctx.ResolveOwnerRepo(); err != nil {
					return err
				}
				name, _ := ctx.RequireArg("name", `--name "My Name"`)
				body := map[string]interface{}{
					"title": name,
				}
				if d := ctx.Arg("description"); d != "" {
					body["description"] = d
				}
				if due := ctx.Arg("due"); due != "" {
					body["due_date"] = due
				}
				env, err := ctx.CallAPI("POST", fmt.Sprintf("/v1%s/milestones", ctx.RepoPath()), body)
				if err != nil {
					return err
				}
				return ctx.Output(env)
			},
		},
		{
			Name:        "update",
			Description: "Update a milestone",
			Flags: []common.Flag{
				{Name: "id", Short: "i", Usage: "Milestone ID", Required: true},
				{Name: "name", Short: "n", Usage: "New name"},
				{Name: "description", Short: "d", Usage: "New description"},
				{Name: "due", Usage: "New due date (YYYY-MM-DD)"},
			},
			Run: func(ctx *common.RuntimeContext) error {
				if err := ctx.ResolveOwnerRepo(); err != nil {
					return err
				}
				id, _ := ctx.RequireArg("id", "--id 1")
				body := map[string]interface{}{}
				if n := ctx.Arg("name"); n != "" {
					body["title"] = n
				}
				if d := ctx.Arg("description"); d != "" {
					body["description"] = d
				}
				if due := ctx.Arg("due"); due != "" {
					body["due_date"] = due
				}
				if len(body) == 0 {
					return fmt.Errorf("at least one of --name, --description, --due is required")
				}
				env, err := ctx.CallAPI("PATCH", fmt.Sprintf("/v1%s/milestones/%s", ctx.RepoPath(), id), body)
				if err != nil {
					return err
				}
				return ctx.Output(env)
			},
		},
		{
			Name:        "delete",
			Description: "Delete a milestone",
			Flags: []common.Flag{
				{Name: "id", Short: "i", Usage: "Milestone ID", Required: true},
			},
			Run: func(ctx *common.RuntimeContext) error {
				if err := ctx.ResolveOwnerRepo(); err != nil {
					return err
				}
				id, _ := ctx.RequireArg("id", "--id 1")
				env, err := ctx.CallAPI("DELETE", fmt.Sprintf("/v1%s/milestones/%s", ctx.RepoPath(), id), nil)
				if err != nil {
					return err
				}
				return ctx.Output(env)
			},
		},
	}
}
