package bootstrapimpl

import (
	"testing"
)

// TestConnectorEventCompanionColumns (gate 1 §7): asserts that
// _System_ConnectorEvent declares the two new nullable VARCHAR columns
// CompanionPackInstallId and CompanionFlowVersionId.
func TestConnectorEventCompanionColumns(t *testing.T) {
	const tableName = "_System_ConnectorEvent"
	defs := GetManagedTableDefinitions()

	for _, def := range defs {
		if def.TableName != tableName {
			continue
		}
		colIdx := make(map[string]bool, len(def.Columns))
		colNullable := make(map[string]bool, len(def.Columns))
		for _, col := range def.Columns {
			colIdx[col.Name] = true
			colNullable[col.Name] = col.Nullable
		}

		for _, wantName := range []string{"CompanionPackInstallId", "CompanionFlowVersionId"} {
			if !colIdx[wantName] {
				t.Errorf("%s: missing expected column %q (required by issue-1777 companion snapshot)", tableName, wantName)
				continue
			}
			if !colNullable[wantName] {
				t.Errorf("%s.%s.nullable = false, want true (NULL = same-pack/legacy event)", tableName, wantName)
			}
		}
		return
	}
	t.Fatalf("%s not found in system_tables.json", tableName)
}

// TestInstalledPackDependencyTableExists (gate 1 §7): asserts that
// _System_InstalledPackDependency is declared in system_tables.json with the
// required columns and indices.
func TestInstalledPackDependencyTableExists(t *testing.T) {
	const tableName = "_System_InstalledPackDependency"
	defs := GetManagedTableDefinitions()

	var found bool
	for _, def := range defs {
		if def.TableName != tableName {
			continue
		}
		found = true
		if def.Label != "Installed Pack Dependency" {
			t.Errorf("%s.label = %q, want %q", tableName, def.Label, "Installed Pack Dependency")
		}
		if len(def.Label) == 0 || len(def.Label) > 255 {
			t.Errorf("%s.label length = %d, want 1..255 so startup metadata registration does not fall back to the long description", tableName, len(def.Label))
		}

		wantCols := []string{
			"Id",
			"DependentPackName",
			"DependentPackInstallId",
			"DependsOnPackName",
			"VersionConstraint",
			"IsDeleted",
			"CreatedDate",
			"LastModifiedDate",
		}
		colIdx := make(map[string]bool, len(def.Columns))
		colNullable := make(map[string]bool, len(def.Columns))
		colPK := make(map[string]bool, len(def.Columns))
		for _, col := range def.Columns {
			colIdx[col.Name] = true
			colNullable[col.Name] = col.Nullable
			colPK[col.Name] = col.PrimaryKey
		}
		for _, name := range wantCols {
			if !colIdx[name] {
				t.Errorf("%s: missing expected column %q", tableName, name)
			}
		}

		// Id must be a primary key.
		if !colPK["Id"] {
			t.Errorf("%s.Id.primaryKey = false, want true", tableName)
		}

		// VersionConstraint must be nullable (empty/NULL = any version accepted).
		if !colNullable["VersionConstraint"] {
			t.Errorf("%s.VersionConstraint.nullable = false, want true", tableName)
		}

		// Verify unique index on (DependentPackName, DependsOnPackName).
		var hasUniq bool
		for _, idx := range def.Indices {
			if idx.Unique &&
				len(idx.Columns) == 2 &&
				idx.Columns[0] == "DependentPackName" &&
				idx.Columns[1] == "DependsOnPackName" {
				hasUniq = true
				break
			}
		}
		if !hasUniq {
			t.Errorf("%s: missing unique index on (DependentPackName, DependsOnPackName)", tableName)
		}

		// Verify index on (DependsOnPackName, IsDeleted) for consent query.
		var hasDepOnIdx bool
		for _, idx := range def.Indices {
			if !idx.Unique &&
				len(idx.Columns) == 2 &&
				idx.Columns[0] == "DependsOnPackName" &&
				idx.Columns[1] == "IsDeleted" {
				hasDepOnIdx = true
				break
			}
		}
		if !hasDepOnIdx {
			t.Errorf("%s: missing index on (DependsOnPackName, IsDeleted)", tableName)
		}

		// requiredWritePermission must be BLOCKED.
		if def.RequiredWritePermission != "BLOCKED" {
			t.Errorf("%s.requiredWritePermission = %q, want BLOCKED; "+
				"direct writes bypass the pack-install lifecycle write path",
				tableName, def.RequiredWritePermission)
		}

		break
	}
	if !found {
		t.Fatalf("%s not found in system_tables.json", tableName)
	}
}
