package ape

import (
	"testing"

	"git.frostfs.info/TrueCloudLab/policy-engine/schema/native"
	"github.com/stretchr/testify/require"
)

func TestValidationOfChainResources(t *testing.T) {
	tests := [...]struct {
		testName     string
		resourceName string
		expectErr    error
	}{
		{
			testName:     "native object: all objects",
			resourceName: native.ObjectPrefix + "/*",
		},
		{
			testName:     "native object: all objects in namespace",
			resourceName: native.ObjectPrefix + "/ns/*",
		},
		{
			testName:     "native object: all objects in root namespace",
			resourceName: native.ObjectPrefix + "//*",
		},
		{
			testName:     "native object: all objects in namespace/container",
			resourceName: native.ObjectPrefix + "/ns/SeHNpifDH2Fc4scNBphrbmrKi96QXj2HzYJkhSGuytH/*",
		},
		{
			testName:     "native object: all objects in root namespace/container",
			resourceName: native.ObjectPrefix + "//SeHNpifDH2Fc4scNBphrbmrKi96QXj2HzYJkhSGuytH/*",
		},
		{
			testName:     "native object: object in namespace/container",
			resourceName: native.ObjectPrefix + "/ns/SeHNpifDH2Fc4scNBphrbmrKi96QXj2HzYJkhSGuytH/BCGsUu6o92oG1UALVox1sV6YbBUKUL2xSCtAFkrsuvWY",
		},
		{
			testName:     "native object: object in root namespace/container",
			resourceName: native.ObjectPrefix + "//SeHNpifDH2Fc4scNBphrbmrKi96QXj2HzYJkhSGuytH/BCGsUu6o92oG1UALVox1sV6YbBUKUL2xSCtAFkrsuvWY",
		},
		{
			testName:     "native object: invalid all objects",
			resourceName: native.ObjectPrefix + "/*12313",
			expectErr:    ErrInvalidResource,
		},
		{
			testName:     "native object: all objects in invalid namespace",
			resourceName: native.ObjectPrefix + "/qwe_123123/*",
			expectErr:    ErrInvalidNamespace,
		},
		{
			testName:     "native object: invalid all objects in root namespace",
			resourceName: native.ObjectPrefix + "//qwe",
			expectErr:    ErrInvalidResource,
		},
		{
			testName:     "native object: invalid cid in all objects in root namespace",
			resourceName: native.ObjectPrefix + "//SeHNpifDH2Fc4scNBphrbmrKi96QXj2HzYJkhSGuytHqwe/*",
			expectErr:    ErrInvalidContainerID,
		},
		{
			testName:     "native object: invalid cid in all objects in namespace",
			resourceName: native.ObjectPrefix + "/ns/SeHNpifDH2Fc4scNBphrbmrKi96QXj2HzYJkhSGuytHqwe/*",
			expectErr:    ErrInvalidContainerID,
		},
		{
			testName:     "native object: invalid object in namespace/container",
			resourceName: native.ObjectPrefix + "/ns/SeHNpifDH2Fc4scNBphrbmrKi96QXj2HzYJkhSGuytH/BCGsUu6o92oG1UALVox1sV6YbBUKUL2xSCtAFkrsuvWY111",
			expectErr:    ErrInvalidObjectID,
		},
		{
			testName:     "native object: invalid resource",
			resourceName: native.ObjectPrefix + "/ns/SeHNpifD/AFkrsuvWY111/AFkrsuvWY222",
			expectErr:    ErrInvalidResource,
		},
		{
			testName:     "native container: all containers",
			resourceName: native.ContainerPrefix + "/*",
		},
		{
			testName:     "native container: all containers in namespace",
			resourceName: native.ContainerPrefix + "/ns/*",
		},
		{
			testName:     "native container: all containers in root namespace",
			resourceName: native.ContainerPrefix + "//*",
		},
		{
			testName:     "native container: container in namespace",
			resourceName: native.ContainerPrefix + "/ns/SeHNpifDH2Fc4scNBphrbmrKi96QXj2HzYJkhSGuytH",
		},
		{
			testName:     "native container: container in root namespace",
			resourceName: native.ContainerPrefix + "//SeHNpifDH2Fc4scNBphrbmrKi96QXj2HzYJkhSGuytH",
		},
		{
			testName:     "native container: invalid all containers",
			resourceName: native.ContainerPrefix + "/*asd",
			expectErr:    ErrInvalidResource,
		},
		{
			testName:     "native container: invalid resource",
			resourceName: native.ContainerPrefix + "/ns/cid/cid",
			expectErr:    ErrInvalidResource,
		},
		{
			testName:     "native container: invalid container in root namespace",
			resourceName: native.ContainerPrefix + "//*asd",
			expectErr:    ErrInvalidContainerID,
		},
		{
			testName:     "native container: container in invalid namespace",
			resourceName: native.ContainerPrefix + "/ns_111/SeHNpifDH2Fc4scNBphrbmrKi96QXj2HzYJkhSGuytH",
			expectErr:    ErrInvalidNamespace,
		},
		{
			testName:     "unsupported prefix",
			resourceName: "native:test/ns_111/SeHNpifDH2Fc4scNBphrbmrKi96QXj2HzYJkhSGuytH",
			expectErr:    ErrUnsupportedPrefix,
		},
	}

	for _, test := range tests {
		t.Run(test.testName, func(t *testing.T) {
			err := ValidateResourceName(test.resourceName)
			require.ErrorIs(t, err, test.expectErr)
		})
	}
}