//
// Copyright 2024 The Chainloop Authors.
//
// 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.

package cmd

import (
	"errors"
	"fmt"

	"github.com/chainloop-dev/chainloop/app/cli/pkg/action"
	"github.com/spf13/cobra"
)

func newAttestationResetCmd() *cobra.Command {
	var trigger, reason string
	triggerFailed, triggerCanceled := action.AttestationResetTriggerFailed, action.AttestationResetTriggerCancelled

	cmd := &cobra.Command{
		Use:   "reset",
		Short: "mark current attestation process as canceled or failed",
		Annotations: map[string]string{
			useAPIToken: "true",
		},
		PreRunE: func(cmd *cobra.Command, args []string) error {
			if trigger != triggerFailed && trigger != triggerCanceled {
				return errors.New("--trigger value is invalid")
			}

			return nil
		},
		RunE: func(cmd *cobra.Command, args []string) error {
			a, err := action.NewAttestationReset(&action.AttestationResetOpts{ActionsOpts: ActionOpts, LocalStatePath: attestationLocalStatePath})
			if err != nil {
				return fmt.Errorf("failed to load action: %w", err)
			}

			if err := a.Run(cmd.Context(), attestationID, trigger, reason); err != nil {
				return newGracefulError(err)
			}

			msg := "Attestation canceled"
			if trigger == triggerFailed {
				msg = "Attestation marked as failed"
			}

			logger.Info().Msg(msg)

			return nil
		},
	}

	cmd.Flags().StringVar(&trigger, "trigger", triggerFailed, fmt.Sprintf("trigger for the reset, valid options are %q and %q", triggerFailed, triggerCanceled))
	cmd.Flags().StringVar(&reason, "reason", "", "reset reason")
	flagAttestationID(cmd)

	return cmd
}
