package cmd

import (
	"time"

	"github.com/spf13/cobra"
)

// Top-level aliases for "actions run" and "actions workflow" commands,
// matching gh CLI's command structure (e.g., "fj run list" instead of "fj actions run list").

func init() {
	// --- run alias ---
	runAliasCmd := &cobra.Command{
		Use:   "run",
		Short: "View and manage workflow runs (alias for 'actions run')",
		Long:  "List, view, and manage workflow runs.\n\nThis is a top-level alias for 'actions run'.",
	}

	runAliasListCmd := &cobra.Command{
		Use:   "list",
		Short: "List recent workflow runs",
		Long:  "List recent workflow runs for a repository.",
		RunE:  runRunList,
	}
	addRepoFlags(runAliasListCmd)
	runAliasListCmd.Flags().IntP("limit", "L", 20, "Maximum number of runs to list")
	runAliasListCmd.Flags().Bool("json", false, "Output workflow runs as JSON")

	runAliasViewCmd := &cobra.Command{
		Use:   "view <run-id>",
		Short: "View a workflow run",
		Long:  "View details about a specific workflow run.",
		Args:  cobra.ExactArgs(1),
		RunE:  runRunView,
	}
	addRepoFlags(runAliasViewCmd)
	runAliasViewCmd.Flags().BoolP("verbose", "v", false, "Show job steps")
	runAliasViewCmd.Flags().BoolP("log", "", false, "View full log for either a run or specific job")
	runAliasViewCmd.Flags().StringP("job", "j", "", "View a specific job ID from a run")
	runAliasViewCmd.Flags().BoolP("log-failed", "", false, "View the log for any failed steps in a run or specific job")
	runAliasViewCmd.Flags().Bool("json", false, "Output workflow run as JSON")

	runAliasWatchCmd := &cobra.Command{
		Use:   "watch <run-id>",
		Short: "Watch a workflow run",
		Long:  "Poll a workflow run until it completes.",
		Args:  cobra.ExactArgs(1),
		RunE:  runRunWatch,
	}
	addRepoFlags(runAliasWatchCmd)
	runAliasWatchCmd.Flags().DurationP("interval", "i", 5*time.Second, "Polling interval")

	runAliasRerunCmd := &cobra.Command{
		Use:   "rerun <run-id>",
		Short: "Rerun a workflow run",
		Long:  "Trigger a rerun for a specific workflow run.",
		Args:  cobra.ExactArgs(1),
		RunE:  runRunRerun,
	}
	addRepoFlags(runAliasRerunCmd)

	runAliasCancelCmd := &cobra.Command{
		Use:   "cancel <run-id>",
		Short: "Cancel a workflow run",
		Long:  "Cancel a running workflow run.",
		Args:  cobra.ExactArgs(1),
		RunE:  runRunCancel,
	}
	addRepoFlags(runAliasCancelCmd)

	runAliasCmd.AddCommand(runAliasListCmd)
	runAliasCmd.AddCommand(runAliasViewCmd)
	runAliasCmd.AddCommand(runAliasWatchCmd)
	runAliasCmd.AddCommand(runAliasRerunCmd)
	runAliasCmd.AddCommand(runAliasCancelCmd)
	rootCmd.AddCommand(runAliasCmd)

	// --- workflow alias ---
	workflowAliasCmd := &cobra.Command{
		Use:   "workflow",
		Short: "Manage workflows (alias for 'actions workflow')",
		Long:  "List, view, and run workflows.\n\nThis is a top-level alias for 'actions workflow'.",
	}

	workflowAliasListCmd := &cobra.Command{
		Use:   "list",
		Short: "List workflows",
		Long:  "List all workflows in a repository.",
		RunE:  runWorkflowList,
	}
	addRepoFlags(workflowAliasListCmd)
	workflowAliasListCmd.Flags().IntP("limit", "L", 20, "Maximum number of workflows to list")
	workflowAliasListCmd.Flags().Bool("json", false, "Output workflows as JSON")

	workflowAliasViewCmd := &cobra.Command{
		Use:   "view <workflow>",
		Short: "View a workflow",
		Long:  "View details about a specific workflow. You can specify the workflow by name or filename.",
		Args:  cobra.ExactArgs(1),
		RunE:  runWorkflowView,
	}
	addRepoFlags(workflowAliasViewCmd)
	workflowAliasViewCmd.Flags().Bool("json", false, "Output workflow as JSON")

	workflowAliasRunCmd := &cobra.Command{
		Use:   "run <workflow>",
		Short: "Run a workflow",
		Long:  "Trigger a workflow_dispatch event for a workflow. The workflow must support the workflow_dispatch trigger.",
		Args:  cobra.ExactArgs(1),
		RunE:  runWorkflowRun,
	}
	addRepoFlags(workflowAliasRunCmd)
	workflowAliasRunCmd.Flags().StringP("ref", "r", "", "Branch or tag name to run the workflow on (defaults to repository's default branch)")
	workflowAliasRunCmd.Flags().StringSliceP("field", "f", nil, "Add a string parameter in key=value format (can be used multiple times)")
	workflowAliasRunCmd.Flags().StringSliceP("raw-field", "F", nil, "Add a string parameter in key=value format, reading from file if value starts with @ (can be used multiple times)")

	workflowAliasEnableCmd := &cobra.Command{
		Use:   "enable <workflow>",
		Short: "Enable a workflow",
		Long:  "Enable a workflow so it can be triggered.\n\nNote: This feature requires Forgejo 15.0+ or Gitea 1.24+.\nFor older versions, use the web UI to enable workflows.",
		Args:  cobra.ExactArgs(1),
		RunE:  runWorkflowEnable,
	}
	addRepoFlags(workflowAliasEnableCmd)

	workflowAliasDisableCmd := &cobra.Command{
		Use:   "disable <workflow>",
		Short: "Disable a workflow",
		Long:  "Disable a workflow so it cannot be triggered.\n\nNote: This feature requires Forgejo 15.0+ or Gitea 1.24+.\nFor older versions, use the web UI to disable workflows.",
		Args:  cobra.ExactArgs(1),
		RunE:  runWorkflowDisable,
	}
	addRepoFlags(workflowAliasDisableCmd)

	workflowAliasCmd.AddCommand(workflowAliasListCmd)
	workflowAliasCmd.AddCommand(workflowAliasViewCmd)
	workflowAliasCmd.AddCommand(workflowAliasRunCmd)
	workflowAliasCmd.AddCommand(workflowAliasEnableCmd)
	workflowAliasCmd.AddCommand(workflowAliasDisableCmd)
	rootCmd.AddCommand(workflowAliasCmd)
}
