//                           _       _
// __      _____  __ ___   ___  __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
//  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
//   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
//  Copyright © 2016 - 2026 Weaviate B.V. All rights reserved.
//
//  CONTACT: hello@weaviate.io
//

package oidc

import (
	"encoding/json"
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"

	jose "github.com/go-jose/go-jose/v4"
	"github.com/golang-jwt/jwt/v4"
)

func newOIDCServer(t *testing.T) *httptest.Server {
	// we need to start up with an empty handler
	s := httptest.NewServer(nil)

	// so that we can configure it once we now the url, this is used to match the
	// issue field
	s.Config.Handler = oidcHandler(t, s.URL)
	return s
}

type oidcDiscovery struct {
	Issuer  string `json:"issuer"`
	JWKSUri string `json:"jwks_uri"`
}

type jwksResponse struct {
	Keys []jose.JSONWebKey `json:"keys"`
}

func oidcHandler(t *testing.T, url string) http.Handler {
	mux := http.NewServeMux()

	publicKey, err := jwt.ParseRSAPublicKeyFromPEM([]byte(testingPublicKey))
	if err != nil {
		t.Fatalf("test server: couldn't parse public key: %v", err)
	}

	mux.HandleFunc("/.well-known/openid-configuration", func(w http.ResponseWriter, req *http.Request) {
		w.Header().Add("Content-Type", "application/json")
		d := oidcDiscovery{
			Issuer:  url,
			JWKSUri: fmt.Sprintf("%v/.well-known/jwks", url),
		}
		json.NewEncoder(w).Encode(d)
	})

	mux.HandleFunc("/.well-known/jwks", func(w http.ResponseWriter, req *http.Request) {
		w.Header().Add("Content-Type", "application/json")
		d := jwksResponse{
			Keys: []jose.JSONWebKey{
				{
					Key:       publicKey,
					Use:       "sig",
					Algorithm: string(jose.RS256),
					KeyID:     "my-key",
				},
			},
		}
		if err := json.NewEncoder(w).Encode(d); err != nil {
			t.Fatalf("encoding jwks in test server: %v", err)
		}
	})

	return mux
}

// those keys are intended to make it possible to sign our own tokens in tests.
// Never use these keys for anything outside a test scenario!

var testing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e2jFD8IgAVHtkAHbFUliQtP6a6OOLMRq9GMhIv2Z
Wf79KyXvh5DFuM7zbwIDAQAB
-----END PUBLIC KEY-----`

func signToken(claims jwt.Claims) (string, error) {
	token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
	key, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(testingPrivateKey))
	if err != nil {
		return "", err
	}

	return token.SignedString(key)
}
