package model

import (
	"encoding/json"
	es_models "github.com/caos/zitadel/internal/eventstore/models"
	"github.com/caos/zitadel/internal/project/model"
	"testing"
)

func TestApplicationChanges(t *testing.T) {
	type args struct {
		existing *Application
		new      *Application
	}
	type res struct {
		changesLen int
	}
	tests := []struct {
		name string
		args args
		res  res
	}{
		{
			name: "application name changes",
			args: args{
				existing: &Application{AppID: "AppID", Name: "Name"},
				new:      &Application{AppID: "AppID", Name: "NameChanged"},
			},
			res: res{
				changesLen: 2,
			},
		},
		{
			name: "no changes",
			args: args{
				existing: &Application{AppID: "AppID", Name: "Name"},
				new:      &Application{AppID: "AppID", Name: "Name"},
			},
			res: res{
				changesLen: 1,
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			changes := tt.args.existing.Changes(tt.args.new)
			if len(changes) != tt.res.changesLen {
				t.Errorf("got wrong changes len: expected: %v, actual: %v ", tt.res.changesLen, len(changes))
			}
		})
	}
}

func TestAppendAddAppEvent(t *testing.T) {
	type args struct {
		project *Project
		app     *Application
		event   *es_models.Event
	}
	tests := []struct {
		name   string
		args   args
		result *Project
	}{
		{
			name: "append add application event",
			args: args{
				project: &Project{},
				app:     &Application{Name: "Application"},
				event:   &es_models.Event{},
			},
			result: &Project{Applications: []*Application{&Application{Name: "Application"}}},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if tt.args.app != nil {
				data, _ := json.Marshal(tt.args.app)
				tt.args.event.Data = data
			}
			tt.args.project.appendAddAppEvent(tt.args.event)
			if len(tt.args.project.Applications) != 1 {
				t.Errorf("got wrong result should have one app actual: %v ", len(tt.args.project.Applications))
			}
			if tt.args.project.Applications[0] == tt.result.Applications[0] {
				t.Errorf("got wrong result: expected: %v, actual: %v ", tt.result.Applications[0], tt.args.project.Applications[0])
			}
		})
	}
}

func TestAppendChangeAppEvent(t *testing.T) {
	type args struct {
		project *Project
		app     *Application
		event   *es_models.Event
	}
	tests := []struct {
		name   string
		args   args
		result *Project
	}{
		{
			name: "append change application event",
			args: args{
				project: &Project{Applications: []*Application{&Application{Name: "Application"}}},
				app:     &Application{Name: "Application Change"},
				event:   &es_models.Event{},
			},
			result: &Project{Applications: []*Application{&Application{Name: "Application Change"}}},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if tt.args.app != nil {
				data, _ := json.Marshal(tt.args.app)
				tt.args.event.Data = data
			}
			tt.args.project.appendChangeAppEvent(tt.args.event)
			if len(tt.args.project.Applications) != 1 {
				t.Errorf("got wrong result should have one app actual: %v ", len(tt.args.project.Applications))
			}
			if tt.args.project.Applications[0] == tt.result.Applications[0] {
				t.Errorf("got wrong result: expected: %v, actual: %v ", tt.result.Applications[0], tt.args.project.Applications[0])
			}
		})
	}
}

func TestAppendRemoveAppEvent(t *testing.T) {
	type args struct {
		project *Project
		app     *Application
		event   *es_models.Event
	}
	tests := []struct {
		name   string
		args   args
		result *Project
	}{
		{
			name: "append remove application event",
			args: args{
				project: &Project{Applications: []*Application{&Application{AppID: "AppID", Name: "Application"}}},
				app:     &Application{AppID: "AppID", Name: "Application"},
				event:   &es_models.Event{},
			},
			result: &Project{Applications: []*Application{}},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if tt.args.app != nil {
				data, _ := json.Marshal(tt.args.app)
				tt.args.event.Data = data
			}
			tt.args.project.appendRemoveAppEvent(tt.args.event)
			if len(tt.args.project.Applications) != 0 {
				t.Errorf("got wrong result should have no apps actual: %v ", len(tt.args.project.Applications))
			}
		})
	}
}

func TestAppendAppStateEvent(t *testing.T) {
	type args struct {
		project *Project
		app     *ApplicationID
		event   *es_models.Event
		state   model.AppState
	}
	tests := []struct {
		name   string
		args   args
		result *Project
	}{
		{
			name: "append deactivate application event",
			args: args{
				project: &Project{Applications: []*Application{&Application{AppID: "AppID", Name: "Application", State: int32(model.APPSTATE_ACTIVE)}}},
				app:     &ApplicationID{AppID: "AppID"},
				event:   &es_models.Event{},
				state:   model.APPSTATE_INACTIVE,
			},
			result: &Project{Applications: []*Application{&Application{AppID: "AppID", Name: "Application", State: int32(model.APPSTATE_INACTIVE)}}},
		},
		{
			name: "append reactivate application event",
			args: args{
				project: &Project{Applications: []*Application{&Application{AppID: "AppID", Name: "Application", State: int32(model.APPSTATE_INACTIVE)}}},
				app:     &ApplicationID{AppID: "AppID"},
				event:   &es_models.Event{},
				state:   model.APPSTATE_ACTIVE,
			},
			result: &Project{Applications: []*Application{&Application{AppID: "AppID", Name: "Application", State: int32(model.APPSTATE_ACTIVE)}}},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if tt.args.app != nil {
				data, _ := json.Marshal(tt.args.app)
				tt.args.event.Data = data
			}
			tt.args.project.appendAppStateEvent(tt.args.event, tt.args.state)
			if len(tt.args.project.Applications) != 1 {
				t.Errorf("got wrong result should have one app actual: %v ", len(tt.args.project.Applications))
			}
			if tt.args.project.Applications[0] == tt.result.Applications[0] {
				t.Errorf("got wrong result: expected: %v, actual: %v ", tt.result.Applications[0], tt.args.project.Applications[0])
			}
		})
	}
}