package update

import (
	"context"
	"testing"
	"time"

	"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
	"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
	"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
	"github.com/google/uuid"
	"github.com/stackitcloud/stackit-sdk-go/services/serviceaccount"
)

var projectIdFlag = globalflags.ProjectIdFlag

type testCtxKey struct{}

var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
var testClient = &serviceaccount.APIClient{}
var testProjectId = uuid.NewString()
var testServiceAccountEmail = "my-service-account-1234567@sa.stackit.cloud"
var testKeyId = uuid.NewString()
var testNow = time.Now()
var test10DaysFromNow = daysFromNow(testNow, 10)

func fixtureArgValues(mods ...func(argValues []string)) []string {
	argValues := []string{
		testKeyId,
	}
	for _, mod := range mods {
		mod(argValues)
	}
	return argValues
}

func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
	flagValues := map[string]string{
		projectIdFlag:           testProjectId,
		serviceAccountEmailFlag: testServiceAccountEmail,
	}
	for _, mod := range mods {
		mod(flagValues)
	}
	return flagValues
}

func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
	model := &inputModel{
		GlobalFlagModel: &globalflags.GlobalFlagModel{
			ProjectId: testProjectId,
			Verbosity: globalflags.VerbosityDefault,
		},
		ServiceAccountEmail: testServiceAccountEmail,
		KeyId:               testKeyId,
	}
	for _, mod := range mods {
		mod(model)
	}
	return model
}

func fixtureRequest(mods ...func(request *serviceaccount.ApiPartialUpdateServiceAccountKeyRequest)) serviceaccount.ApiPartialUpdateServiceAccountKeyRequest {
	request := testClient.PartialUpdateServiceAccountKey(testCtx, testProjectId, testServiceAccountEmail, testKeyId)
	request = request.PartialUpdateServiceAccountKeyPayload(serviceaccount.PartialUpdateServiceAccountKeyPayload{})
	for _, mod := range mods {
		mod(&request)
	}
	return request
}

func TestParseInput(t *testing.T) {
	tests := []struct {
		description   string
		argValues     []string
		flagValues    map[string]string
		isValid       bool
		expectedModel *inputModel
	}{
		{
			description:   "base",
			argValues:     fixtureArgValues(),
			flagValues:    fixtureFlagValues(),
			isValid:       true,
			expectedModel: fixtureInputModel(),
		},
		{
			description: "no values",
			argValues:   []string{},
			flagValues:  map[string]string{},
			isValid:     false,
		},
		{
			description: "no arg values",
			argValues:   []string{},
			flagValues:  fixtureFlagValues(),
			isValid:     false,
		},
		{
			description: "no flag values",
			argValues:   fixtureArgValues(),
			flagValues:  map[string]string{},
			isValid:     false,
		},
		{
			description: "with expiring date",
			argValues:   fixtureArgValues(),
			flagValues: fixtureFlagValues(func(flagValues map[string]string) {
				flagValues[expiredInDaysFlag] = "10"
			}),
			isValid: true,
			expectedModel: fixtureInputModel(func(model *inputModel) {
				model.ExpiresInDays = utils.Ptr(int64(10))
			}),
		},
		{
			description: "with activate flag",
			argValues:   fixtureArgValues(),
			flagValues: fixtureFlagValues(func(flagValues map[string]string) {
				flagValues[activateFlag] = "true"
			}),
			isValid: true,
			expectedModel: fixtureInputModel(func(model *inputModel) {
				model.Activate = true
			}),
		},
		{
			description: "with deactivate flag",
			argValues:   fixtureArgValues(),
			flagValues: fixtureFlagValues(func(flagValues map[string]string) {
				flagValues[deactivateFlag] = "true"
			}),
			isValid: true,
			expectedModel: fixtureInputModel(func(model *inputModel) {
				model.Deactivate = true
			}),
		},
		{
			description: "with activate and deactivate flags",
			argValues:   fixtureArgValues(),
			flagValues: fixtureFlagValues(func(flagValues map[string]string) {
				flagValues[activateFlag] = "true"
				flagValues[deactivateFlag] = "true"
			}),
			isValid: false,
		},
		{
			description: "project id missing",
			argValues:   fixtureArgValues(),
			flagValues: fixtureFlagValues(func(flagValues map[string]string) {
				delete(flagValues, projectIdFlag)
			}),
			isValid: false,
		},
		{
			description: "project id invalid 1",
			argValues:   fixtureArgValues(),
			flagValues: fixtureFlagValues(func(flagValues map[string]string) {
				flagValues[projectIdFlag] = ""
			}),
			isValid: false,
		},
		{
			description: "project id invalid 2",
			argValues:   fixtureArgValues(),
			flagValues: fixtureFlagValues(func(flagValues map[string]string) {
				flagValues[projectIdFlag] = "invalid-uuid"
			}),
			isValid: false,
		},
		{
			description: "service account email missing",
			argValues:   fixtureArgValues(),
			flagValues: fixtureFlagValues(func(flagValues map[string]string) {
				delete(flagValues, serviceAccountEmailFlag)
			}),
			isValid: false,
		},
		{
			description: "key id invalid 1",
			argValues:   []string{""},
			flagValues:  fixtureFlagValues(),
			isValid:     false,
		},
		{
			description: "key id invalid 2",
			argValues:   []string{"invalid-uuid"},
			flagValues:  fixtureFlagValues(),
			isValid:     false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.description, func(t *testing.T) {
			testutils.TestParseInput(t, NewCmd, parseInput, tt.expectedModel, tt.argValues, tt.flagValues, tt.isValid)
		})
	}
}

func TestBuildRequest(t *testing.T) {
	tests := []struct {
		description     string
		model           *inputModel
		isValid         bool
		expectedRequest serviceaccount.ApiPartialUpdateServiceAccountKeyRequest
	}{
		{
			description:     "base",
			model:           fixtureInputModel(),
			isValid:         true,
			expectedRequest: fixtureRequest(),
		},
		{
			description: "with expiring date",
			model: fixtureInputModel(func(model *inputModel) {
				model.ExpiresInDays = utils.Ptr(int64(10))
			}),
			isValid: true,
			expectedRequest: fixtureRequest().PartialUpdateServiceAccountKeyPayload(
				serviceaccount.PartialUpdateServiceAccountKeyPayload{
					ValidUntil: utils.Ptr(test10DaysFromNow),
				}),
		},
		{
			description: "with activate flag",
			model: fixtureInputModel(func(model *inputModel) {
				model.Activate = true
			}),
			isValid: true,
			expectedRequest: fixtureRequest().PartialUpdateServiceAccountKeyPayload(
				serviceaccount.PartialUpdateServiceAccountKeyPayload{
					Active: utils.Ptr(true),
				}),
		},
		{
			description: "with deactivate flag",
			model: fixtureInputModel(func(model *inputModel) {
				model.Deactivate = true
			}),
			isValid: true,
			expectedRequest: fixtureRequest().PartialUpdateServiceAccountKeyPayload(
				serviceaccount.PartialUpdateServiceAccountKeyPayload{
					Active: utils.Ptr(false),
				}),
		},
	}

	for _, tt := range tests {
		t.Run(tt.description, func(t *testing.T) {
			request := buildRequest(testCtx, tt.model, testClient, testNow)

			diff := cmp.Diff(request, tt.expectedRequest,
				cmp.AllowUnexported(tt.expectedRequest),
				cmpopts.EquateComparable(testCtx),
			)
			if diff != "" {
				t.Fatalf("Data does not match: %s", diff)
			}
		})
	}
}
