package impl

import (
	"context"
	"testing"

	"github.com/stretchr/testify/mock"
	"github.com/stretchr/testify/require"
	crrf_repo_mock "github.com/tidbcloud/dataflow-service/app/repo/crrf/mock"
	"github.com/tidbcloud/dataflow-service/app/repo/entity"
	"github.com/tidbcloud/pkgv2/bizerr"
)

func TestQueryCRRFStatusForAPI(t *testing.T) {
	clusterID := uint64(1001)
	tests := map[string]struct {
		mockSetting *entity.CRRFSetting
		mockErr     bizerr.BizError
		wantStatus  entity.CRRFSettingStatus
		wantErr     bool
	}{
		"found enabled": {
			mockSetting: &entity.CRRFSetting{Status: entity.CRRFSettingStatusEnabled},
			wantStatus:  entity.CRRFSettingStatusEnabled,
		},
		"record not found": {
			mockErr:    bizerr.New(bizerr.ErrCodeRecordNotFound, "not found"),
			wantStatus: entity.CRRFSettingStatusDisabled,
		},
		"unexpected error": {
			mockErr: bizerr.New(bizerr.ErrCodeUnknown, "db error"),
			wantErr: true,
		},
	}

	for name, tc := range tests {
		t.Run(name, func(t *testing.T) {
			mockRepo := crrf_repo_mock.NewCRRFSettingRepo(t)
			mockRepo.On("GetByClusterID", mock.Anything, clusterID).
				Return(tc.mockSetting, tc.mockErr).
				Once()

			m := &backupSettingModel{crrfSettingRepo: mockRepo}
			got, err := m.queryCRRFStatusForAPI(context.Background(), clusterID)
			if tc.wantErr {
				require.Error(t, err)
				return
			}
			require.NoError(t, err)
			require.Equal(t, tc.wantStatus, got)
		})
	}
}
