package team

import (
	"fmt"
	"net/url"

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

func Shortcuts() []*common.Shortcut {
	return []*common.Shortcut{
		{
			Name:        "list",
			Description: "List teams in an organization",
			Flags: []common.Flag{
				{Name: "org", Short: "o", Usage: "Organization ID or login", Required: true},
				{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 {
				org, _ := ctx.RequireArg("org", "--org my-org")
				q := url.Values{}
				q.Set("page", ctx.Arg("page"))
				q.Set("limit", ctx.Arg("limit"))
				env, err := ctx.CallAPIWithQuery("GET", fmt.Sprintf("/organizations/%s/teams", org), q)
				if err != nil {
					return err
				}
				return ctx.Output(env)
			},
		},
		{
			Name:        "create",
			Description: "Create a team in an organization",
			Flags: []common.Flag{
				{Name: "org", Short: "o", Usage: "Organization ID or login", Required: true},
				{Name: "name", Short: "n", Usage: "Team name", Required: true},
				{Name: "description", Short: "d", Usage: "Team description"},
			},
			Run: func(ctx *common.RuntimeContext) error {
				org, _ := ctx.RequireArg("org", "--org my-org")
				name, _ := ctx.RequireArg("name", `--name "My Team"`)
				body := map[string]interface{}{
					"name": name,
				}
				if desc := ctx.Arg("description"); desc != "" {
					body["description"] = desc
				}
				env, err := ctx.CallAPI("POST", fmt.Sprintf("/organizations/%s/teams", org), body)
				if err != nil {
					return err
				}
				return ctx.Output(env)
			},
		},
		{
			Name:        "delete",
			Description: "Delete a team",
			Flags: []common.Flag{
				{Name: "org", Short: "o", Usage: "Organization ID or login", Required: true},
				{Name: "id", Short: "i", Usage: "Team ID", Required: true},
			},
			Run: func(ctx *common.RuntimeContext) error {
				org, _ := ctx.RequireArg("org", "--org my-org")
				id, _ := ctx.RequireArg("id", "--id 1")
				env, err := ctx.CallAPI("DELETE", fmt.Sprintf("/organizations/%s/teams/%s", org, id), nil)
				if err != nil {
					return err
				}
				return ctx.Output(env)
			},
		},
		{
			Name:        "members",
			Description: "List members of a team",
			Flags: []common.Flag{
				{Name: "org", Short: "o", Usage: "Organization ID or login", Required: true},
				{Name: "id", Short: "i", Usage: "Team ID", Required: true},
				{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 {
				org, _ := ctx.RequireArg("org", "--org my-org")
				id, _ := ctx.RequireArg("id", "--id 1")
				q := url.Values{}
				q.Set("page", ctx.Arg("page"))
				q.Set("limit", ctx.Arg("limit"))
				env, err := ctx.CallAPIWithQuery("GET", fmt.Sprintf("/organizations/%s/teams/%s/members", org, id), q)
				if err != nil {
					return err
				}
				return ctx.Output(env)
			},
		},
		{
			Name:        "member-add",
			Description: "Add a user to a team",
			Flags: []common.Flag{
				{Name: "org", Short: "o", Usage: "Organization ID or login", Required: true},
				{Name: "team", Short: "t", Usage: "Team ID", Required: true},
				{Name: "user", Short: "u", Usage: "User login or ID to add", Required: true},
			},
			Run: func(ctx *common.RuntimeContext) error {
				org, _ := ctx.RequireArg("org", "--org my-org")
				team, _ := ctx.RequireArg("team", "--team dev-team")
				user, _ := ctx.RequireArg("user", "--user alice")
				body := map[string]interface{}{
					"user_id": user,
				}
				env, err := ctx.CallAPI("POST", fmt.Sprintf("/organizations/%s/teams/%s/members", org, team), body)
				if err != nil {
					return err
				}
				return ctx.Output(env)
			},
		},
		{
			Name:        "member-remove",
			Description: "Remove a user from a team",
			Flags: []common.Flag{
				{Name: "org", Short: "o", Usage: "Organization ID or login", Required: true},
				{Name: "team", Short: "t", Usage: "Team ID", Required: true},
				{Name: "user", Short: "u", Usage: "User login or ID to remove", Required: true},
			},
			Run: func(ctx *common.RuntimeContext) error {
				org, _ := ctx.RequireArg("org", "--org my-org")
				team, _ := ctx.RequireArg("team", "--team dev-team")
				user, _ := ctx.RequireArg("user", "--user alice")
				env, err := ctx.CallAPI("DELETE", fmt.Sprintf("/organizations/%s/teams/%s/members/%s", org, team, user), nil)
				if err != nil {
					return err
				}
				return ctx.Output(env)
			},
		},
	}
}
