// Copyright 2025 Emiliano Spinella (eminwux)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

package stack

import (
	"errors"
	"fmt"
	"strings"
	"time"

	"github.com/eminwux/kukeon/cmd/config"
	"github.com/eminwux/kukeon/cmd/kuke/get/shared"
	kukeshared "github.com/eminwux/kukeon/cmd/kuke/shared"
	"github.com/eminwux/kukeon/internal/errdefs"
	"github.com/eminwux/kukeon/pkg/api/kukeonv1"
	v1beta1 "github.com/eminwux/kukeon/pkg/api/model/v1beta1"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

// MockControllerKey is used to inject a mock kukeonv1.Client via context in tests.
type MockControllerKey struct{}

func NewStackCmd() *cobra.Command {
	cmd := &cobra.Command{
		Use:           "stack [name]",
		Aliases:       []string{"stacks", "st"},
		Short:         "Get or list stack information",
		Args:          cobra.MaximumNArgs(1),
		SilenceUsage:  true,
		SilenceErrors: false,
		RunE: func(cmd *cobra.Command, args []string) error {
			outputFormat, err := shared.ParseOutputFormat(cmd)
			if err != nil {
				return err
			}

			selector, err := shared.ParseLabelSelectorFlag(cmd)
			if err != nil {
				return err
			}

			realm := shared.ExplicitFlag(cmd, "realm", config.KUKE_GET_STACK_REALM.ViperKey)
			space := shared.ExplicitFlag(cmd, "space", config.KUKE_GET_STACK_SPACE.ViperKey)

			var name string
			if len(args) > 0 {
				name = strings.TrimSpace(args[0])
			} else {
				name = strings.TrimSpace(viper.GetString(config.KUKE_GET_STACK_NAME.ViperKey))
			}

			if name != "" && !selector.Empty() {
				return errdefs.ErrSelectorWithName
			}

			client, err := resolveClient(cmd)
			if err != nil {
				return err
			}
			defer func() { _ = client.Close() }()

			if name != "" {
				if realm == "" {
					realm = strings.TrimSpace(config.KUKE_GET_STACK_REALM.ValueOrDefault())
				}
				if space == "" {
					space = strings.TrimSpace(config.KUKE_GET_STACK_SPACE.ValueOrDefault())
				}
				if realm == "" {
					return fmt.Errorf("%w (--realm)", errdefs.ErrRealmNameRequired)
				}
				if space == "" {
					return fmt.Errorf("%w (--space)", errdefs.ErrSpaceNameRequired)
				}
				doc := v1beta1.StackDoc{
					Metadata: v1beta1.StackMetadata{Name: name},
					Spec: v1beta1.StackSpec{
						RealmID: realm,
						SpaceID: space,
					},
				}
				result, err := client.GetStack(cmd.Context(), doc)
				if err != nil {
					if errors.Is(err, errdefs.ErrStackNotFound) {
						return fmt.Errorf("stack %q not found in realm %q, space %q", name, realm, space)
					}
					return err
				}
				if !result.MetadataExists {
					return fmt.Errorf("stack %q not found in realm %q, space %q", name, realm, space)
				}
				return printStack(cmd, &result.Stack, outputFormat)
			}

			stacks, err := client.ListStacks(cmd.Context(), realm, space)
			if err != nil {
				return err
			}
			stacks = filterStacksBySelector(stacks, selector)
			return printStacks(cmd, stacks, outputFormat)
		},
	}

	cmd.Flags().String("realm", "", "Filter stacks by realm name")
	_ = viper.BindPFlag(config.KUKE_GET_STACK_REALM.ViperKey, cmd.Flags().Lookup("realm"))
	cmd.Flags().String("space", "", "Filter stacks by space name")
	_ = viper.BindPFlag(config.KUKE_GET_STACK_SPACE.ViperKey, cmd.Flags().Lookup("space"))
	cmd.Flags().
		StringP("output", "o", "", "Output format (yaml, json, table, wide). Default: table for list, yaml for single resource")
	_ = viper.BindPFlag(config.KUKE_GET_OUTPUT.ViperKey, cmd.Flags().Lookup("output"))
	_ = viper.BindPFlag(config.KUKE_GET_OUTPUT.ViperKey, cmd.Flags().Lookup("o"))

	shared.RegisterLabelSelectorFlag(cmd)

	cmd.ValidArgsFunction = config.CompleteStackNames
	_ = cmd.RegisterFlagCompletionFunc("realm", config.CompleteRealmNames)
	_ = cmd.RegisterFlagCompletionFunc("space", config.CompleteSpaceNames)
	_ = cmd.RegisterFlagCompletionFunc("output", config.CompleteOutputFormat)
	_ = cmd.RegisterFlagCompletionFunc("o", config.CompleteOutputFormat)

	return cmd
}

func resolveClient(cmd *cobra.Command) (kukeonv1.Client, error) {
	if mockClient, ok := cmd.Context().Value(MockControllerKey{}).(kukeonv1.Client); ok {
		return mockClient, nil
	}
	return kukeshared.ClientFromCmd(cmd)
}

// filterStacksBySelector returns the subset of stacks whose
// Metadata.Labels satisfy selector. A nil or empty selector returns the
// input slice unmodified so the common no-flag path skips the allocation.
func filterStacksBySelector(stacks []v1beta1.StackDoc, selector *shared.LabelSelector) []v1beta1.StackDoc {
	if selector.Empty() {
		return stacks
	}
	out := make([]v1beta1.StackDoc, 0, len(stacks))
	for i := range stacks {
		if selector.Matches(stacks[i].Metadata.Labels) {
			out = append(out, stacks[i])
		}
	}
	return out
}

func printStack(cmd *cobra.Command, stack *v1beta1.StackDoc, format shared.OutputFormat) error {
	switch format {
	case shared.OutputFormatJSON:
		return shared.PrintJSON(cmd, stack)
	default:
		return shared.PrintYAML(cmd, stack)
	}
}

func printStacks(
	cmd *cobra.Command,
	stacks []v1beta1.StackDoc,
	format shared.OutputFormat,
) error {
	switch format {
	case shared.OutputFormatYAML:
		return shared.PrintYAML(cmd, stacks)
	case shared.OutputFormatJSON:
		return shared.PrintJSON(cmd, stacks)
	case shared.OutputFormatTable, shared.OutputFormatWide:
		if len(stacks) == 0 {
			cmd.Println("No stacks found.")
			return nil
		}
		// Stack has no per-entity wide columns — `-o wide` renders the
		// same shape as default (#603). Investigation drops to -o yaml.
		headers := []string{"NAME", "REALM", "SPACE", "STATE", "AGE"}
		now := time.Now()
		rows := make([][]string, 0, len(stacks))
		for i := range stacks {
			s := &stacks[i]
			state := (&s.Status.State).String()
			rows = append(rows, []string{
				s.Metadata.Name,
				s.Spec.RealmID,
				s.Spec.SpaceID,
				state,
				shared.RenderAge(s.Status.CreatedAt, now),
			})
		}
		shared.PrintTable(cmd, headers, rows)
		return nil
	default:
		return shared.PrintYAML(cmd, stacks)
	}
}
