package bootstrapimpl

import (
	"reflect"
	"testing"

	"github.com/nexuscrm/shared/pkg/constants"
)

// TestSystemTableNotificationColumnsMatchConstants asserts set-equality between
// the Notification columns declared in system_tables.json and the generated
// constants.ColsNotification slice. A mismatch in either direction means the
// two declarations of the same fact have drifted, which is the bug class that
// caused issue #452 (wrong literal "created_date" vs schema "CreatedDate").
func TestSystemTableNotificationColumnsMatchConstants(t *testing.T) {
	defs := GetSystemTableDefinitions()

	var notifCols []string
	found := false
	for _, def := range defs {
		if def.TableName == "Notification" {
			found = true
			for _, col := range def.Columns {
				notifCols = append(notifCols, col.Name)
			}
			break
		}
	}
	if !found {
		t.Fatal("Notification table not found in system_tables.json")
	}

	gotJSON := make(map[string]struct{}, len(notifCols))
	for _, c := range notifCols {
		gotJSON[c] = struct{}{}
	}

	gotConsts := make(map[string]struct{}, len(constants.ColsNotification))
	for _, c := range constants.ColsNotification {
		gotConsts[c] = struct{}{}
	}

	if !reflect.DeepEqual(gotJSON, gotConsts) {
		// Report drift direction so the maintainer sees which side changed.
		var extraInJSON, missingFromJSON []string
		for c := range gotJSON {
			if _, ok := gotConsts[c]; !ok {
				extraInJSON = append(extraInJSON, c)
			}
		}
		for c := range gotConsts {
			if _, ok := gotJSON[c]; !ok {
				missingFromJSON = append(missingFromJSON, c)
			}
		}
		t.Fatalf("Notification column-set drift: JSON has extra %v, missing %v (vs constants.ColsNotification)",
			extraInJSON, missingFromJSON)
	}
}
