package persistence

import (
	"errors"
	"testing"

	apperrors "github.com/nexuscrm/kernel/pkg/errors"
	"github.com/nexuscrm/shared/pkg/models"
)

func TestNormalizeAdminPageLayoutPreservesCloneProvenanceOnly(t *testing.T) {
	for _, test := range []struct {
		name       string
		source     string
		wantSource string
	}{
		{name: "clone", source: " cloned ", wantSource: "cloned"},
		{name: "direct create", source: "", wantSource: "admin_created"},
		{name: "managed takeover", source: "platform_generated", wantSource: "admin_created"},
	} {
		t.Run(test.name, func(t *testing.T) {
			layout := &models.PageLayout{
				ObjectAPIName: " Account ", LayoutName: " Main ", Source: test.source,
				GenerationVersion: 7,
			}
			normalizeAdminPageLayoutForPersistence(layout)
			if layout.Source != test.wantSource || !layout.IsCustomized || layout.GenerationVersion != 0 ||
				layout.ObjectAPIName != "Account" || layout.LayoutName != "Main" {
				t.Fatalf("normalized Layout = %#v", layout)
			}
		})
	}
}

func assertLayoutAdministrationHTTPStatus(t *testing.T, err error, want int) {
	t.Helper()
	var appError apperrors.AppError
	if !errors.As(err, &appError) || appError.HTTPStatus() != want {
		t.Fatalf("error = %T %v, want HTTP status %d", err, err, want)
	}
}
