package containers

import (
	"context"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestReplaceImageReference(t *testing.T) {
	t.Parallel()

	ctx := context.Background()

	testCases := []struct {
		name           string
		input          string
		expectedOutput string
		modified       bool
	}{
		{
			name: "Replace image reference",
			input: `
version: v1
services:
  - name: web
    image: nginx:1.25.3
  - name: localstack
    image: localstack/localstack:3.0.2
`,
			expectedOutput: `
version: v1
services:
  - name: web
    image: index.docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee  # 1.25.3
  - name: localstack
    image: index.docker.io/localstack/localstack@sha256:e606c4421419030b12d63a59f1211f57f5b0fbf7e9ce769e6250ee62ff4f9293  # 3.0.2
`,
			modified: true,
		},
		// Add more test cases as needed
	}

	// Define a regular expression to match YAML tags containing "image"
	for _, tc := range testCases {
		tc := tc

		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()

			var output strings.Builder
			m, err := ReplaceImageReferenceFromYAML(ctx, strings.NewReader(tc.input), &output)
			assert.NoError(t, err)

			assert.Equal(t, tc.expectedOutput, output.String())
			assert.Equal(t, tc.modified, m, "modified")
		})
	}
}
