package ci

import (
	"fmt"
	"net/url"

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

func Shortcuts() []*common.Shortcut {
	return []*common.Shortcut{
		{
			Name:        "builds",
			Description: "List CI builds",
			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", ctx.RepoPath()+"/builds", q)
				if err != nil {
					return fmt.Errorf("获取 CI 构建列表失败: %w", err)
				}
				return ctx.Output(env)
			},
		},
		{
			Name:        "logs",
			Description: "View build logs",
			Flags: []common.Flag{
				{Name: "build", Short: "b", Usage: "Build number", Required: true},
				{Name: "stage", Short: "s", Usage: "Stage number", Default: "1"},
				{Name: "step", Usage: "Step number", Default: "1"},
			},
			Run: func(ctx *common.RuntimeContext) error {
				if err := ctx.ResolveOwnerRepo(); err != nil {
					return err
				}
				build, err := ctx.RequireArg("build", "--build 42")
				if err != nil {
					return err
				}
				stage := ctx.Arg("stage")
				step := ctx.Arg("step")
				if stage == "" {
					stage = "1"
				}
				if step == "" {
					step = "1"
				}
				env, err := ctx.CallAPI("GET", fmt.Sprintf("%s/builds/%s/logs/%s/%s", ctx.RepoPath(), build, stage, step), nil)
				if err != nil {
					return fmt.Errorf("获取构建日志失败: %w", err)
				}
				return ctx.Output(env)
			},
		},
		{
			Name:        "restart",
			Description: "Restart a build",
			DryRun:      true,
			DryRunHint: func(ctx *common.RuntimeContext) (string, error) {
				build := ctx.Arg("build")
				return fmt.Sprintf("Restart build #%s", build), nil
			},
			Flags: []common.Flag{
				{Name: "build", Short: "b", Usage: "Build number", Required: true},
			},
			Run: func(ctx *common.RuntimeContext) error {
				if err := ctx.ResolveOwnerRepo(); err != nil {
					return err
				}
				build, err := ctx.RequireArg("build", "--build 42")
				if err != nil {
					return err
				}
				env, err := ctx.CallAPI("POST", fmt.Sprintf("%s/builds/%s/restart", ctx.RepoPath(), build), nil)
				if err != nil {
					return fmt.Errorf("重启构建失败: %w", err)
				}
				return ctx.Output(env)
			},
		},
		{
			Name:        "stop",
			Description: "Stop a build",
			DryRun:      true,
			DryRunHint: func(ctx *common.RuntimeContext) (string, error) {
				build := ctx.Arg("build")
				return fmt.Sprintf("Stop build #%s", build), nil
			},
			Flags: []common.Flag{
				{Name: "build", Short: "b", Usage: "Build number", Required: true},
			},
			Run: func(ctx *common.RuntimeContext) error {
				if err := ctx.ResolveOwnerRepo(); err != nil {
					return err
				}
				build, err := ctx.RequireArg("build", "--build 42")
				if err != nil {
					return err
				}
				env, err := ctx.CallAPI("DELETE", fmt.Sprintf("%s/builds/%s/stop", ctx.RepoPath(), build), nil)
				if err != nil {
					return fmt.Errorf("停止构建失败: %w", err)
				}
				return ctx.Output(env)
			},
		},
	}
}
