package imagetypes_test

import (
	"errors"
	"testing"

	"github.com/stretchr/testify/assert"

	"debian-bootc-image-builder/internal/imagetypes"
)

type testCase struct {
	imageTypes      []string
	expectedExports []string
	expectISO       bool
	expectedErr     error
}

func TestImageTypes(t *testing.T) {
	testCases := map[string]testCase{
		"qcow-disk": {
			imageTypes:      []string{"qcow2"},
			expectedExports: []string{"qcow2"},
			expectISO:       false,
		},
		"ami-disk": {
			imageTypes:      []string{"ami"},
			expectedExports: []string{"image"},
			expectISO:       false,
		},
		"vmdk-disk": {
			imageTypes:      []string{"vmdk"},
			expectedExports: []string{"vmdk"},
			expectISO:       false,
		},
		"qcow-ami-disk": {
			imageTypes:      []string{"qcow2", "ami"},
			expectedExports: []string{"qcow2", "image"},
			expectISO:       false,
		},
		"ami-raw": {
			imageTypes:      []string{"ami", "raw"},
			expectedExports: []string{"image"},
			expectISO:       false,
		},
		"all-disk": {
			imageTypes:      []string{"ami", "raw", "vmdk", "qcow2"},
			expectedExports: []string{"image", "vmdk", "qcow2"},
			expectISO:       false,
		},
		"debian-installer": {
			imageTypes:      []string{"debian-installer"},
			expectedExports: []string{"bootiso"},
			expectISO:       true,
		},
		"calamares": {
			imageTypes:      []string{"calamares"},
			expectedExports: []string{"bootiso"},
			expectISO:       true,
		},
		"bad-mix": {
			imageTypes:  []string{"vmdk", "debian-installer"},
			expectedErr: errors.New("cannot mix ISO/disk images in request [vmdk debian-installer]"),
		},
		"bad-mix-part-2": {
			imageTypes:  []string{"ami", "calamares"},
			expectedErr: errors.New("cannot mix ISO/disk images in request [ami calamares]"),
		},
		"bad-image-type": {
			imageTypes:  []string{"bad"},
			expectedErr: errors.New(`unsupported image type "bad", valid types are ami, calamares, debian-installer, qcow2, raw, vmdk`),
		},
		"bad-in-good": {
			imageTypes:  []string{"ami", "raw", "vmdk", "qcow2", "something-else-what-is-this"},
			expectedErr: errors.New(`unsupported image type "something-else-what-is-this", valid types are ami, calamares, debian-installer, qcow2, raw, vmdk`),
		},
		"all-bad": {
			imageTypes:  []string{"bad1", "bad2", "bad3", "bad4", "bad5", "bad42"},
			expectedErr: errors.New(`unsupported image type "bad1", valid types are ami, calamares, debian-installer, qcow2, raw, vmdk`),
		},
	}

	for name, tc := range testCases {
		t.Run(name, func(t *testing.T) {
			it, err := imagetypes.New(tc.imageTypes...)
			if tc.expectedErr != nil {
				assert.Equal(t, err, tc.expectedErr)
			} else {
				assert.Equal(t, it.Exports(), tc.expectedExports)
				assert.Equal(t, it.BuildsISO(), tc.expectISO)
				assert.NoError(t, err)
			}
		})
	}
}

func TestImageTypesValidation(t *testing.T) {
	// Test empty image types
	_, err := imagetypes.New()
	assert.Error(t, err)
	assert.Contains(t, err.Error(), "cannot use an empty array as a build request")

	// Test valid single image type
	it, err := imagetypes.New("qcow2")
	assert.NoError(t, err)
	assert.Equal(t, []string{"qcow2"}, it.Exports())
	assert.False(t, it.BuildsISO())

	// Test valid multiple image types
	it, err = imagetypes.New("qcow2", "ami", "vmdk")
	assert.NoError(t, err)
	assert.Equal(t, []string{"qcow2", "image", "vmdk"}, it.Exports())
	assert.False(t, it.BuildsISO())

	// Test ISO image type
	it, err = imagetypes.New("debian-installer")
	assert.NoError(t, err)
	assert.Equal(t, []string{"bootiso"}, it.Exports())
	assert.True(t, it.BuildsISO())
}

func TestImageTypesSupportedTypes(t *testing.T) {
	// Test all supported image types
	supportedTypes := []string{"ami", "calamares", "debian-installer", "qcow2", "raw", "vmdk"}
	
	for _, imageType := range supportedTypes {
		t.Run(imageType, func(t *testing.T) {
			it, err := imagetypes.New(imageType)
			assert.NoError(t, err)
			assert.NotNil(t, it)
		})
	}
}
