package persistence

import (
	"testing"

	"github.com/nexuscrm/shared/pkg/constants"
	"github.com/stretchr/testify/assert"
)

// TestScannerParityUI verifies that the UI scanner functions accept the
// correct number of Scan arguments matching their column constant slices.
func TestScannerParityUI(t *testing.T) {
	repo := &MetadataRepository{}

	tests := []struct {
		Name     string
		Cols     []string
		ScanFunc func(*MockScannable)
	}{
		{
			Name: "App",
			Cols: constants.ColsSystemApp,
			ScanFunc: func(m *MockScannable) {
				repo.scanApp(m)
			},
		},
		{
			Name: "PageLayout",
			Cols: constants.ColsSystemPageLayout,
			ScanFunc: func(m *MockScannable) {
				repo.scanLayout(m)
			},
		},
		{
			Name: "Dashboard",
			Cols: constants.ColsDashboard,
			ScanFunc: func(m *MockScannable) {
				repo.scanDashboard(m)
			},
		},
		{
			Name: "ListView",
			Cols: constants.ColsSystemListView,
			ScanFunc: func(m *MockScannable) {
				repo.scanListView(m)
			},
		},
		{
			Name: "Theme",
			Cols: constants.ColsSystemTheme,
			ScanFunc: func(m *MockScannable) {
				repo.scanTheme(m)
			},
		},
		{
			Name: "ValidationRule",
			Cols: constants.ColsSystemValidationRule,
			ScanFunc: func(m *MockScannable) {
				repo.scanValidationRule(m)
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.Name, func(t *testing.T) {
			mock := &MockScannable{}
			defer func() {
				if r := recover(); r != nil {
					// Even on panic, verify scan argument count
					if len(mock.CapturedArgs) != len(tt.Cols) {
						t.Errorf("Panic occurred, and argument mismatch: expected %d, got %d", len(tt.Cols), len(mock.CapturedArgs))
					}
				}
			}()

			tt.ScanFunc(mock)
			assert.Equal(t, len(tt.Cols), len(mock.CapturedArgs), "Scanner argument count must match columns constant count")
		})
	}
}
