// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

package server

import (
	"context"
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/apache/skywalking-banyandb/fodc/agent/internal/exporter"
	"github.com/apache/skywalking-banyandb/fodc/agent/internal/flightrecorder"
)

type stubCollectionProvider struct {
	err     error
	payload []map[string]any
}

func (s stubCollectionProvider) MarshalCollections() ([]byte, error) {
	if s.err != nil {
		return nil, s.err
	}
	return json.Marshal(s.payload)
}

func TestNewServer_ValidConfig(t *testing.T) {
	config := Config{
		ListenAddr:          ":9090",
		DiagnosisListenAddr: ":9091",
		ReadHeaderTimeout:   5 * time.Second,
		ShutdownTimeout:     10 * time.Second,
	}

	server, err := NewServer(config)

	require.NoError(t, err)
	assert.NotNil(t, server)
	assert.Equal(t, ":9090", server.GetListenAddr())
	assert.Equal(t, ":9091", server.GetDiagnosisListenAddr())
	assert.False(t, server.IsStarted())
	assert.Equal(t, config.ReadHeaderTimeout, server.config.ReadHeaderTimeout)
	assert.Equal(t, config.ShutdownTimeout, server.config.ShutdownTimeout)
}

func TestNewServer_DefaultTimeouts(t *testing.T) {
	config := Config{
		ListenAddr: ":9090",
	}

	server, err := NewServer(config)

	require.NoError(t, err)
	assert.NotNil(t, server)
	assert.Equal(t, ":9090", server.GetDiagnosisListenAddr())
	assert.Equal(t, defaultReadHeaderTimeout, server.config.ReadHeaderTimeout)
	assert.Equal(t, defaultShutdownTimeout, server.config.ShutdownTimeout)
}

func TestNewServer_EmptyListenAddr(t *testing.T) {
	config := Config{
		ListenAddr: "",
	}

	server, err := NewServer(config)

	assert.Error(t, err)
	assert.Nil(t, server)
	assert.Contains(t, err.Error(), "listen address cannot be empty")
}

func TestServer_StartStop(t *testing.T) {
	config := Config{
		ListenAddr:        "localhost:0", // Use port 0 for automatic assignment
		ReadHeaderTimeout: 1 * time.Second,
		ShutdownTimeout:   1 * time.Second,
	}

	server, err := NewServer(config)
	require.NoError(t, err)
	assert.NotNil(t, server)

	// Create a registry and collector for testing
	registry := prometheus.NewRegistry()
	fr := flightrecorder.NewFlightRecorder(1000)
	datasourceCollector := exporter.NewDatasourceCollector(fr)

	// Start the server
	errCh, startErr := server.Start(registry, datasourceCollector, nil)
	require.NoError(t, startErr)
	assert.True(t, server.IsStarted())

	// Give the server a moment to start
	time.Sleep(100 * time.Millisecond)

	// Stop the server
	stopErr := server.Stop(context.Background())
	assert.NoError(t, stopErr)
	assert.False(t, server.IsStarted())

	// Check that no errors were sent on the error channel
	select {
	case err := <-errCh:
		t.Errorf("Unexpected error from server: %v", err)
	default:
		// No error, which is expected
	}
}

func TestServer_Start_AlreadyStarted(t *testing.T) {
	config := Config{
		ListenAddr: "localhost:0",
	}

	server, err := NewServer(config)
	require.NoError(t, err)

	// Create a registry and collector for testing
	registry := prometheus.NewRegistry()
	fr := flightrecorder.NewFlightRecorder(1000)
	datasourceCollector := exporter.NewDatasourceCollector(fr)

	// Start the server first time
	_, startErr := server.Start(registry, datasourceCollector, nil)
	require.NoError(t, startErr)
	assert.True(t, server.IsStarted())

	// Try to start again - should fail
	_, startErr2 := server.Start(registry, datasourceCollector, nil)
	assert.Error(t, startErr2)
	assert.Contains(t, startErr2.Error(), "server is already started")

	// Clean up
	server.Stop(context.Background())
}

func TestServer_Stop_NotStarted(t *testing.T) {
	config := Config{
		ListenAddr: ":9090",
	}

	server, err := NewServer(config)
	require.NoError(t, err)
	assert.False(t, server.IsStarted())

	// Try to stop without starting - should fail
	stopErr := server.Stop(context.Background())
	assert.Error(t, stopErr)
	assert.Contains(t, stopErr.Error(), "server is not started")
}

func TestServer_Start_RegistryAlreadyHasCollector(t *testing.T) {
	config := Config{
		ListenAddr: "localhost:0",
	}

	server, err := NewServer(config)
	require.NoError(t, err)

	// Create a registry
	registry := prometheus.NewRegistry()
	fr := flightrecorder.NewFlightRecorder(1000)
	datasourceCollector := exporter.NewDatasourceCollector(fr)

	// First start should work
	errCh, startErr := server.Start(registry, datasourceCollector, nil)
	require.NoError(t, startErr)
	assert.True(t, server.IsStarted())

	// Stop the server
	require.NoError(t, server.Stop(context.Background()))
	assert.False(t, server.IsStarted())

	// Check that no errors were sent on the error channel
	select {
	case err := <-errCh:
		t.Errorf("Unexpected error from server: %v", err)
	default:
		// No error, which is expected
	}
}

func TestServer_GetListenAddr(t *testing.T) {
	config := Config{
		ListenAddr: ":8080",
	}

	server, err := NewServer(config)
	require.NoError(t, err)

	assert.Equal(t, ":8080", server.GetListenAddr())
}

func TestServer_IsStarted(t *testing.T) {
	config := Config{
		ListenAddr: "localhost:0",
	}

	server, err := NewServer(config)
	require.NoError(t, err)

	// Initially not started
	assert.False(t, server.IsStarted())

	// Create a registry and collector for testing
	registry := prometheus.NewRegistry()
	fr := flightrecorder.NewFlightRecorder(1000)
	datasourceCollector := exporter.NewDatasourceCollector(fr)

	// Start the server
	_, startErr := server.Start(registry, datasourceCollector, nil)
	require.NoError(t, startErr)
	assert.True(t, server.IsStarted())

	// Stop the server
	stopErr := server.Stop(context.Background())
	require.NoError(t, stopErr)
	assert.False(t, server.IsStarted())
}

func TestServer_DiagnosticsEndpoint(t *testing.T) {
	config := Config{
		ListenAddr:        "localhost:0",
		ReadHeaderTimeout: time.Second,
		ShutdownTimeout:   time.Second,
	}

	server, err := NewServer(config)
	require.NoError(t, err)

	registry := prometheus.NewRegistry()
	fr := flightrecorder.NewFlightRecorder(1000)
	datasourceCollector := exporter.NewDatasourceCollector(fr)
	provider := stubCollectionProvider{
		payload: []map[string]any{
			{
				"artifactDir": "/crash/one",
				"collector":   "fodc-agent",
			},
		},
	}

	errCh, startErr := server.Start(registry, datasourceCollector, provider)
	require.NoError(t, startErr)
	defer func() {
		require.NoError(t, server.Stop(context.Background()))
		select {
		case serverErr := <-errCh:
			t.Fatalf("unexpected server error: %v", serverErr)
		default:
		}
	}()

	request := httptest.NewRequest(http.MethodGet, "http://example/diagnostics", nil)
	recorder := httptest.NewRecorder()
	server.diagnosisServer.Handler.ServeHTTP(recorder, request)

	require.Equal(t, http.StatusOK, recorder.Code)
	var payload []map[string]any
	require.NoError(t, json.NewDecoder(recorder.Body).Decode(&payload))
	require.Len(t, payload, 1)
	assert.Equal(t, "/crash/one", payload[0]["artifactDir"])
}

func TestDiagnosticsHandlerMethodNotAllowed(t *testing.T) {
	server, err := NewServer(Config{ListenAddr: "localhost:0"})
	require.NoError(t, err)

	registry := prometheus.NewRegistry()
	fr := flightrecorder.NewFlightRecorder(1000)
	datasourceCollector := exporter.NewDatasourceCollector(fr)
	_, startErr := server.Start(registry, datasourceCollector, stubCollectionProvider{})
	require.NoError(t, startErr)
	defer func() {
		require.NoError(t, server.Stop(context.Background()))
	}()

	request := httptest.NewRequest(http.MethodPost, "http://"+server.GetListenAddr()+"/diagnostics", nil)
	recorder := httptest.NewRecorder()
	server.diagnosisServer.Handler.ServeHTTP(recorder, request)
	assert.Equal(t, http.StatusMethodNotAllowed, recorder.Code)
}
