package bootstrapimpl

import "testing"

func TestConnectorInboundHandlerConfigTableExists(t *testing.T) {
	const tableName = "_System_ConnectorInboundHandlerConfig"
	defs := GetManagedTableDefinitions()

	for _, def := range defs {
		if def.TableName != tableName {
			continue
		}
		wantCols := []string{
			"Id", "InstalledPackId", "Namespace", "CapabilityKind",
			"ConfigJSON", "IsDeleted", "CreatedDate", "LastModifiedDate",
		}
		gotCols := make(map[string]bool, len(def.Columns))
		for _, col := range def.Columns {
			gotCols[col.Name] = true
		}
		for _, name := range wantCols {
			if !gotCols[name] {
				t.Fatalf("%s: missing expected column %q", tableName, name)
			}
		}
		return
	}
	t.Fatalf("%s not found in system_tables.json", tableName)
}

func TestConnectorInboundHandlerConfigPermissionsAreBlocked(t *testing.T) {
	const tableName = "_System_ConnectorInboundHandlerConfig"
	defs := GetManagedTableDefinitions()

	for _, def := range defs {
		if def.TableName != tableName {
			continue
		}
		if def.RequiredReadPermission != "BLOCKED" {
			t.Fatalf("%s.requiredReadPermission = %q, want BLOCKED", tableName, def.RequiredReadPermission)
		}
		if def.RequiredWritePermission != "BLOCKED" {
			t.Fatalf("%s.requiredWritePermission = %q, want BLOCKED", tableName, def.RequiredWritePermission)
		}
		return
	}
	t.Fatalf("%s not found in system_tables.json", tableName)
}

func TestConnectorInboundHandlerConfigUniqueIndexNameAndColumns(t *testing.T) {
	const tableName = "_System_ConnectorInboundHandlerConfig"
	defs := GetManagedTableDefinitions()

	for _, def := range defs {
		if def.TableName != tableName {
			continue
		}
		for _, idx := range def.Indices {
			if !idx.Unique || idx.Name != "uniq_cihc_pack_kind" {
				continue
			}
			if len(idx.Columns) == 2 &&
				idx.Columns[0] == "InstalledPackId" &&
				idx.Columns[1] == "CapabilityKind" {
				return
			}
		}
		t.Fatalf("%s: unique index uniq_cihc_pack_kind on (InstalledPackId, CapabilityKind) not found", tableName)
	}
	t.Fatalf("%s not found in system_tables.json", tableName)
}
