package validator

import (
	"baron-sso-backend/internal/domain"
	"net/http"
	"testing"
)

// MockProvider는 IdentityProvider 인터페이스를 구현하는 테스트용 구조체입니다.
type MockProvider struct {
	Supported []string
}

func (m *MockProvider) Name() string {
	return "MockIDP"
}

func (m *MockProvider) GetMetadata() (*domain.IDPMetadata, error) {
	return &domain.IDPMetadata{
		SupportedFields: m.Supported,
	}, nil
}

func (m *MockProvider) CreateUser(user *domain.BrokerUser, password string) (string, error) {
	return "", nil
}

func (m *MockProvider) SignIn(loginID, password string) (*domain.AuthInfo, error) {
	return &domain.AuthInfo{}, nil
}

func (m *MockProvider) UserExists(loginID string) (bool, error) {
	return false, nil
}

func (m *MockProvider) IssueSession(loginID string) (*domain.AuthInfo, error) {
	return nil, domain.ErrNotSupported
}

func (m *MockProvider) InitiateLinkLogin(loginID, returnTo string) (*domain.LinkLoginInit, error) {
	return nil, domain.ErrNotSupported
}

func (m *MockProvider) VerifyLoginCode(loginID, flowID, code string) (*domain.AuthInfo, error) {
	return nil, domain.ErrNotSupported
}

func (m *MockProvider) GetPasswordPolicy() (*domain.PasswordPolicy, error) {
	return nil, domain.ErrNotSupported
}

// Stub implementations to satisfy the IdentityProvider interface for this unit test.
func (m *MockProvider) InitiatePasswordReset(loginID, redirectUrl string) error {
	return nil
}

func (m *MockProvider) VerifyPasswordResetToken(token string) (*domain.AuthInfo, error) {
	return &domain.AuthInfo{}, nil
}

func (m *MockProvider) UpdateUserPassword(loginID, newPassword string, r *http.Request) error {
	return nil
}

func TestValidateIDPCompatibility(t *testing.T) {
	// BrokerUser 모델은 다음과 같이 정의되어 있다고 가정합니다 (idp_models.go 참조):
	// ID (required), Email (required), Name, PhoneNumber
	// Attributes (required_keys: "grade", "department")

	tests := []struct {
		name      string
		supported []string
		wantErr   bool
	}{
		{
			name:      "성공: 모든 필수 필드 지원",
			supported: []string{"id", "email", "name", "grade", "department"},
			wantErr:   false,
		},
		{
			name:      "성공: 선택 필드(name) 누락이어도 성공",
			supported: []string{"id", "email", "grade", "department"},
			wantErr:   false,
		},
		{
			name:      "실패: 필수 필드(id) 누락",
			supported: []string{"email", "grade", "department"},
			wantErr:   true,
		},
		{
			name:      "실패: 필수 필드(email) 누락",
			supported: []string{"id", "grade", "department"},
			wantErr:   true,
		},
		{
			name:      "실패: 커스텀 속성(grade) 누락",
			supported: []string{"id", "email", "department"},
			wantErr:   true,
		},
		{
			name:      "실패: 커스텀 속성(department) 누락",
			supported: []string{"id", "email", "grade"},
			wantErr:   true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			// 테스트용 IDP Provider 생성
			mockIDP := &MockProvider{Supported: tt.supported}

			// 검증 수행
			err := ValidateIDPCompatibility(domain.BrokerUser{}, mockIDP)

			if (err != nil) != tt.wantErr {
				t.Errorf("ValidateIDPCompatibility() error = %v, wantErr %v", err, tt.wantErr)
			}
		})
	}
}
