// Copyright 2016-2020, Pulumi Corporation.
//
// 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 (
	"context"
	"fmt"
	"strings"

	"github.com/pulumi/pulumi/pkg/apitype"
	"github.com/pulumi/pulumi/pkg/backend/display"
	"github.com/pulumi/pulumi/pkg/util/cmdutil"
	"github.com/spf13/cobra"
)

func newPolicyLsCmd() *cobra.Command {
	var jsonOut bool

	var cmd = &cobra.Command{
		Use:   "ls [org-name]",
		Args:  cmdutil.MaximumNArgs(1),
		Short: "List all Policy Packs for a Pulumi organization",
		Long:  "List all Policy Packs for a Pulumi organization",
		Run: cmdutil.RunFunc(func(cmd *cobra.Command, cliArgs []string) error {
			// Get backend.
			b, err := currentBackend(display.Options{Color: cmdutil.GetGlobalColorization()})
			if err != nil {
				return err
			}

			// Get organization.
			var orgName string
			if len(cliArgs) > 0 {
				orgName = cliArgs[0]
			} else {
				orgName, err = b.CurrentUser()
				if err != nil {
					return err
				}
			}

			// List the Policy Packs for the organization.
			ctx := context.Background()
			policyPacks, err := b.ListPolicyPacks(ctx, orgName)
			if err != nil {
				return err
			}

			if jsonOut {
				return formatPolicyPacksJSON(policyPacks)
			}
			return formatPolicyPacksConsole(policyPacks)
		}),
	}
	cmd.PersistentFlags().BoolVarP(
		&jsonOut, "json", "j", false, "Emit output as JSON")
	return cmd
}

func formatPolicyPacksConsole(policyPacks apitype.ListPolicyPacksResponse) error {
	// Header string and formatting options to align columns.
	headers := []string{"NAME", "VERSIONS"}

	rows := []cmdutil.TableRow{}

	for _, packs := range policyPacks.PolicyPacks {
		// Name column
		name := packs.Name

		// Versions column
		versions := strings.Trim(strings.Replace(fmt.Sprint(packs.Versions), " ", ", ", -1), "[]")

		// Render the columns.
		columns := []string{name, versions}
		rows = append(rows, cmdutil.TableRow{Columns: columns})
	}
	cmdutil.PrintTable(cmdutil.Table{
		Headers: headers,
		Rows:    rows,
	})
	return nil
}

// policyPacksJSON is the shape of the --json output of this command. When --json is passed, we print an array
// of policyPacksJSON objects.  While we can add fields to this structure in the future, we should not change
// existing fields.
type policyPacksJSON struct {
	Name     string `json:"name"`
	Versions []int  `json:"versions"`
}

func formatPolicyPacksJSON(policyPacks apitype.ListPolicyPacksResponse) error {
	output := make([]policyPacksJSON, len(policyPacks.PolicyPacks))
	for i, pack := range policyPacks.PolicyPacks {
		output[i] = policyPacksJSON{
			Name:     pack.Name,
			Versions: pack.Versions,
		}
	}
	return printJSON(output)
}
