package checker_test

import (
	"testing"

	"github.com/oasdiff/oasdiff/checker"
	"github.com/oasdiff/oasdiff/diff"
	"github.com/stretchr/testify/require"
)

// CL: adding prefixItems to request body
func TestRequestBodyPrefixItemsAdded(t *testing.T) {
	s1, err := open("../data/checker/prefix_items_added_base.yaml")
	require.NoError(t, err)
	s2, err := open("../data/checker/prefix_items_added_revision.yaml")
	require.NoError(t, err)

	d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
	require.NoError(t, err)
	errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.RequestPropertyPrefixItemsUpdatedCheck), d, osm, checker.INFO)
	require.NotEmpty(t, errs)

	found := false
	for _, e := range errs {
		if e.GetId() == checker.RequestBodyPrefixItemsAddedId {
			found = true
			break
		}
	}
	require.True(t, found, "expected request-body-prefix-items-added")
}

// CL: removing prefixItems from request body (reverse)
func TestRequestBodyPrefixItemsRemoved(t *testing.T) {
	s1, err := open("../data/checker/prefix_items_added_revision.yaml")
	require.NoError(t, err)
	s2, err := open("../data/checker/prefix_items_added_base.yaml")
	require.NoError(t, err)

	d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
	require.NoError(t, err)
	errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.RequestPropertyPrefixItemsUpdatedCheck), d, osm, checker.INFO)
	require.NotEmpty(t, errs)

	found := false
	for _, e := range errs {
		if e.GetId() == checker.RequestBodyPrefixItemsRemovedId {
			found = true
			break
		}
	}
	require.True(t, found, "expected request-body-prefix-items-removed")
}

// CL: adding prefixItems to request property
func TestRequestPropertyPrefixItemsAdded(t *testing.T) {
	s1, err := open("../data/checker/prefix_items_prop_base.yaml")
	require.NoError(t, err)
	s2, err := open("../data/checker/prefix_items_prop_revision.yaml")
	require.NoError(t, err)

	d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
	require.NoError(t, err)
	errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.RequestPropertyPrefixItemsUpdatedCheck), d, osm, checker.INFO)
	require.NotEmpty(t, errs)
	require.True(t, containsId(errs, checker.RequestPropertyPrefixItemsAddedId), "expected request-property-prefix-items-added")
}

// CL: removing prefixItems from request property
func TestRequestPropertyPrefixItemsRemoved(t *testing.T) {
	s1, err := open("../data/checker/prefix_items_prop_revision.yaml")
	require.NoError(t, err)
	s2, err := open("../data/checker/prefix_items_prop_base.yaml")
	require.NoError(t, err)

	d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
	require.NoError(t, err)
	errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.RequestPropertyPrefixItemsUpdatedCheck), d, osm, checker.INFO)
	require.NotEmpty(t, errs)
	require.True(t, containsId(errs, checker.RequestPropertyPrefixItemsRemovedId), "expected request-property-prefix-items-removed")
}
