// Copyright 2025 Woodpecker 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 repo

import (
	"bytes"
	"context"
	"io"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/urfave/cli/v3"

	"go.woodpecker-ci.org/woodpecker/v3/cli/common"
	"go.woodpecker-ci.org/woodpecker/v3/woodpecker-go/woodpecker"
)

func TestRepoOutput(t *testing.T) {
	tests := []struct {
		name     string
		args     []string
		expected string
		wantErr  bool
	}{
		{
			name:     "table output with default columns",
			args:     []string{},
			expected: "FULL NAME  BRANCH  FORGE URL        VISIBILITY  SCM PRIVATE  ACTIVE  ALLOW PULL\norg/repo1  main    git.example.com  public      no           yes     yes\n",
		},
		{
			name:     "table output with custom columns",
			args:     []string{"output", "--output", "table=Name,Forge_URL,Trusted_Network"},
			expected: "NAME   FORGE URL        TRUSTED NETWORK\nrepo1  git.example.com  yes\n",
		},
		{
			name:     "table output with no header",
			args:     []string{"output", "--output-no-headers"},
			expected: "org/repo1  main  git.example.com  public  no  yes  yes\n",
		},
		{
			name:     "go-template output",
			args:     []string{"output", "--output", "go-template={{range . }}{{.Name}} {{.ForgeURL}} {{.Trusted.Network}}{{end}}"},
			expected: "repo1 git.example.com true\n",
		},
		{
			name:     "go-format output",
			args:     []string{"output", "--output", "go-format={{.Name}} {{.ForgeURL}} {{.Trusted.Network}}"},
			expected: "repo1 git.example.com true\n",
		},
		{
			name:    "invalid go-template",
			args:    []string{"output", "--output", "go-template={{.InvalidField}}"},
			wantErr: true,
		},
	}

	repos := []*woodpecker.Repo{
		{
			Name:       "repo1",
			FullName:   "org/repo1",
			ForgeURL:   "git.example.com",
			Branch:     "main",
			Visibility: "public",
			IsActive:   true,
			AllowPull:  true,
			Trusted: woodpecker.TrustedConfiguration{
				Network: true,
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			command := &cli.Command{
				Writer: io.Discard,
				Name:   "output",
				Flags:  common.OutputFlags("table"),
				Action: func(_ context.Context, c *cli.Command) error {
					var buf bytes.Buffer
					err := repoOutput(c, repos, &buf)

					if tt.wantErr {
						assert.Error(t, err)
						return nil
					}

					assert.NoError(t, err)
					assert.Equal(t, tt.expected, buf.String())

					return nil
				},
			}

			_ = command.Run(t.Context(), tt.args)
		})
	}
}
