package cp_test

import (
	"log/slog"
	"testing"

	"github.com/aquaproj/aqua/v2/pkg/config"
	"github.com/aquaproj/aqua/v2/pkg/config/aqua"
	"github.com/aquaproj/aqua/v2/pkg/controller/cp"
	"github.com/aquaproj/aqua/v2/pkg/controller/which"
	"github.com/aquaproj/aqua/v2/pkg/policy"
	"github.com/aquaproj/aqua/v2/pkg/runtime"
	"github.com/spf13/afero"
)

func TestController_Copy(t *testing.T) { //nolint:funlen
	t.Parallel()
	data := []struct {
		name         string
		param        *config.Param
		pkgInstaller cp.PackageInstaller
		fs           afero.Fs
		rt           *runtime.Runtime
		whichCtrl    cp.WhichController
		installer    cp.Installer
		isErr        bool
	}{
		{
			name:      "no arg",
			param:     &config.Param{},
			fs:        afero.NewMemMapFs(),
			installer: &cp.MockInstaller{},
		},
		{
			name: "gh",
			param: &config.Param{
				MaxParallelism: 5,
				Args: []string{
					"gh",
				},
			},
			rt: &runtime.Runtime{
				GOOS:   "darwin",
				GOARCH: "amd64",
			},
			fs:        afero.NewMemMapFs(),
			installer: &cp.MockInstaller{},
			whichCtrl: &which.MockController{
				FindResult: &which.FindResult{
					ExePath: "/home/foo/.local/share/aquaproj-aqua/pkgs/github_release/github.com/cli/cli/v2.17.0/gh_2.17.0_macOS_amd64.tar.gz/gh_2.17.0_macOS_amd64/bin/gh",
					Package: &config.Package{
						Package: &aqua.Package{
							Name: "cli/cli",
						},
					},
					Config: &aqua.Config{
						Checksum: &aqua.Checksum{
							Enabled:         new(true),
							RequireChecksum: new(true),
						},
					},
					ConfigFilePath: "aqua.yaml",
				},
			},
			pkgInstaller: &cp.MockPackageInstaller{},
		},
	}
	logger := slog.New(slog.DiscardHandler)
	for _, d := range data {
		t.Run(d.name, func(t *testing.T) {
			t.Parallel()
			ctx := t.Context()
			ctrl := cp.New(d.param, d.pkgInstaller, d.fs, d.rt, d.whichCtrl, d.installer, &policy.MockReader{})
			if err := ctrl.Copy(ctx, logger, d.param); err != nil {
				if d.isErr {
					return
				}
				t.Fatal(err)
			}
			if d.isErr {
				t.Fatal("error should be returned")
			}
		})
	}
}
