package devfile

import (
	"encoding/json"
	"reflect"
	"testing"

	"github.com/devfile/registry-support/index/generator/schema"
)

func TestGetRegistrySamples(t *testing.T) {

	nodejsBase64Image := "[encoded data, 14468 characters]"

	quarkusBase64Image := "[encoded data, 5308 characters]"

	springBootBase64Image := "[encoded data, 8896 characters]"

	pythonBase64Image := "[encoded data, 6888 characters]"

	tests := []struct {
		name        string
		registry    string
		wantSamples []schema.Schema
		wantErr     bool
	}{
		{
			name:     "Fetch the sample placeholder",
			registry: "sample-placeholder",
			wantSamples: []schema.Schema{
				{
					Name:        "nodejs-basic",
					DisplayName: "Basic NodeJS",
					Description: "A simple Hello world NodeJS application",
					Tags:        []string{"NodeJS", "Express"},
					Icon:        nodejsBase64Image,
					Type:        schema.SampleDevfileType,
					ProjectType: "nodejs",
					Language:    "nodejs",
					Git: &schema.Git{
						Remotes: map[string]string{
							"origin": "https://github.com/redhat-developer/devfile-sample.git",
						},
					},
				},
				{
					Name:        "code-with-quarkus",
					DisplayName: "Basic Quarkus",
					Description: "A simple Hello World Java application using Quarkus",
					Tags:        []string{"Java", "Quarkus"},
					Icon:        quarkusBase64Image,
					Type:        schema.SampleDevfileType,
					ProjectType: "quarkus",
					Language:    "java",
					Git: &schema.Git{
						Remotes: map[string]string{
							"origin": "https://github.com/elsony/devfile-sample-code-with-quarkus.git",
						},
					},
				},
				{
					Name:        "java-springboot-basic",
					DisplayName: "Basic Spring Boot",
					Description: "A simple Hello World Java Spring Boot application using Maven",
					Tags:        []string{"Java", "Spring"},
					Icon:        springBootBase64Image,
					Type:        schema.SampleDevfileType,
					ProjectType: "springboot",
					Language:    "java",
					Git: &schema.Git{
						Remotes: map[string]string{
							"origin": "https://github.com/elsony/devfile-sample-java-springboot-basic.git",
						},
					},
				},
				{
					Name:        "python-basic",
					DisplayName: "Basic Python",
					Description: "A simple Hello World application using Python",
					Tags:        []string{"Python"},
					Icon:        pythonBase64Image,
					Type:        schema.SampleDevfileType,
					ProjectType: "python",
					Language:    "python",
					Git: &schema.Git{
						Remotes: map[string]string{
							"origin": "https://github.com/elsony/devfile-sample-python-basic.git",
						},
					},
				},
			},
		},
		{
			name:     "Invalid registry",
			registry: "invalid",
			wantErr:  true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			bytes, err := GetRegistrySamples(tt.registry)
			if tt.wantErr && err == nil {
				t.Errorf("Expected error from test but got nil")
			} else if !tt.wantErr && err != nil {
				t.Errorf("Got unexpected error: %s", err)
			} else if !tt.wantErr {
				var registryIndex []schema.Schema
				err = json.Unmarshal(bytes, &registryIndex)
				if err != nil {
					t.Errorf("Got unexpected error: %s", err)
					return
				}

				if !reflect.DeepEqual(registryIndex, tt.wantSamples) {
					t.Errorf("expected %+v does not match actual %+v", registryIndex, tt.wantSamples)
				}
			}
		})
	}
}
