/*
이 테스트 파일은 RelyingPartyService의 기능을 검증하기 위한 유닛 테스트입니다.
RelyingPartyService는 HydraAdminService, KetoService와 협력하므로
각 의존성을 모킹(Mocking)하여 통합 로직을 검증합니다.

주요 테스트 항목:
1. Create: Hydra 클라이언트 생성 -> Keto 권한 설정
2. Get: Hydra에서 정보 조회
3. Update: Hydra 업데이트
4. Delete: Hydra 삭제 + Keto 권한 정리
*/

package service

import (
	"baron-sso-backend/internal/domain"
	"context"
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/mock"
)

// --- Test Helpers ---

type hydraRoundTripperFunc func(*http.Request) (*http.Response, error)

func (f hydraRoundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
	return f(req)
}

func mockHydraClient(handler http.Handler) *http.Client {
	return &http.Client{
		Transport: hydraRoundTripperFunc(func(req *http.Request) (*http.Response, error) {
			rec := httptest.NewRecorder()
			handler.ServeHTTP(rec, req)
			return rec.Result(), nil
		}),
	}
}

// --- Tests ---

func TestRelyingPartyService_Create_Success(t *testing.T) {
	mockKeto := new(MockKetoServiceShared)
	mockOutbox := new(MockKetoOutboxRepositoryShared)

	tenantID := "tenant-1"
	inputClient := domain.HydraClient{
		ClientName: "Test App",
		Metadata: map[string]interface{}{
			"user_id": "creator-1",
		},
	}

	// Hydra Mock
	hydraHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method == http.MethodPost && strings.Contains(r.URL.Path, "/clients") {
			var req domain.HydraClient
			_ = json.NewDecoder(r.Body).Decode(&req)

			// 메타데이터 tenant_id 주입 확인
			if req.Metadata["tenant_id"] != tenantID {
				t.Errorf("expected tenant_id in metadata")
			}

			req.ClientID = "generated-client-id"
			w.WriteHeader(http.StatusCreated)
			_ = json.NewEncoder(w).Encode(req)
			return
		}
		http.NotFound(w, r)
	})
	hydraSvc := &HydraAdminService{
		AdminURL:   "http://hydra:4445",
		HTTPClient: mockHydraClient(hydraHandler),
	}

	// Keto sync via Outbox using 'parents' relation
	mockOutbox.On("Create", mock.Anything, mock.MatchedBy(func(e *domain.KetoOutbox) bool {
		return e.Namespace == "RelyingParty" && e.Object == "generated-client-id" && e.Relation == "parents" && e.Subject == "Tenant:"+tenantID
	})).Return(nil)
	for _, relation := range defaultRelyingPartyOperatorRelations {
		rel := relation
		mockOutbox.On("Create", mock.Anything, mock.MatchedBy(func(e *domain.KetoOutbox) bool {
			return e.Namespace == "RelyingParty" && e.Object == "generated-client-id" && e.Relation == rel && e.Subject == "User:creator-1"
		})).Return(nil)
	}

	svc := NewRelyingPartyService(hydraSvc, mockKeto, mockOutbox)
	rp, err := svc.Create(context.Background(), tenantID, inputClient)
	assert.NoError(t, err)
	assert.Equal(t, "generated-client-id", rp.ClientID)
	assert.Equal(t, tenantID, rp.TenantID)

	mockOutbox.AssertExpectations(t)
}

func TestRelyingPartyService_Create_HydraFail(t *testing.T) {
	mockKeto := new(MockKetoServiceShared)
	mockOutbox := new(MockKetoOutboxRepositoryShared)

	hydraHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusInternalServerError)
	})
	hydraSvc := &HydraAdminService{
		AdminURL:   "http://hydra:4445",
		HTTPClient: mockHydraClient(hydraHandler),
	}

	svc := NewRelyingPartyService(hydraSvc, mockKeto, mockOutbox)
	_, err := svc.Create(context.Background(), "tenant-1", domain.HydraClient{})

	assert.Error(t, err)
}

func TestRelyingPartyService_Get_Success(t *testing.T) {
	mockKeto := new(MockKetoServiceShared)
	mockOutbox := new(MockKetoOutboxRepositoryShared)
	clientID := "client-123"

	hydraHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		_ = json.NewEncoder(w).Encode(domain.HydraClient{
			ClientID:   clientID,
			ClientName: "Hydra Name",
			Metadata: map[string]interface{}{
				"tenant_id": "tenant-1",
			},
		})
	})
	hydraSvc := &HydraAdminService{
		AdminURL:   "http://hydra:4445",
		HTTPClient: mockHydraClient(hydraHandler),
	}

	svc := NewRelyingPartyService(hydraSvc, mockKeto, mockOutbox)
	rp, hc, err := svc.Get(context.Background(), clientID)
	assert.NoError(t, err)
	assert.Equal(t, "Hydra Name", rp.Name)
	assert.Equal(t, "Hydra Name", hc.ClientName)
}

func TestRelyingPartyService_Update_Success(t *testing.T) {
	mockKeto := new(MockKetoServiceShared)
	mockOutbox := new(MockKetoOutboxRepositoryShared)
	clientID := "client-123"

	hydraHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method == http.MethodPut {
			var req domain.HydraClient
			_ = json.NewDecoder(r.Body).Decode(&req)
			_ = json.NewEncoder(w).Encode(req)
			return
		}
	})
	hydraSvc := &HydraAdminService{
		AdminURL:   "http://hydra:4445",
		HTTPClient: mockHydraClient(hydraHandler),
	}

	svc := NewRelyingPartyService(hydraSvc, mockKeto, mockOutbox)

	updateReq := domain.HydraClient{ClientName: "New Name"}
	rp, err := svc.Update(context.Background(), clientID, updateReq)
	assert.NoError(t, err)
	assert.Equal(t, "New Name", rp.Name)
}

func TestRelyingPartyService_Delete_Success(t *testing.T) {
	mockKeto := new(MockKetoServiceShared)
	mockOutbox := new(MockKetoOutboxRepositoryShared)
	clientID := "client-123"
	tenantID := "tenant-1"

	hydraHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method == http.MethodGet && strings.Contains(r.URL.Path, clientID) {
			_ = json.NewEncoder(w).Encode(domain.HydraClient{
				ClientID: clientID,
				Metadata: map[string]interface{}{
					"tenant_id": tenantID,
					"user_id":   "creator-1",
				},
			})
			return
		}
		if r.Method == http.MethodDelete && strings.Contains(r.URL.Path, clientID) {
			w.WriteHeader(http.StatusNoContent)
			return
		}
		http.NotFound(w, r)
	})
	hydraSvc := &HydraAdminService{
		AdminURL:   "http://hydra:4445",
		HTTPClient: mockHydraClient(hydraHandler),
	}

	// Delete relation via Outbox using 'parents'
	mockOutbox.On("Create", mock.Anything, mock.MatchedBy(func(e *domain.KetoOutbox) bool {
		return e.Namespace == "RelyingParty" && e.Object == clientID && e.Relation == "parents" && e.Subject == "Tenant:"+tenantID
	})).Return(nil)
	for _, relation := range defaultRelyingPartyOperatorRelations {
		rel := relation
		mockOutbox.On("Create", mock.Anything, mock.MatchedBy(func(e *domain.KetoOutbox) bool {
			return e.Namespace == "RelyingParty" && e.Object == clientID && e.Relation == rel && e.Subject == "User:creator-1"
		})).Return(nil)
	}

	svc := NewRelyingPartyService(hydraSvc, mockKeto, mockOutbox)
	err := svc.Delete(context.Background(), clientID)
	assert.NoError(t, err)

	mockOutbox.AssertExpectations(t)
}
