package http

import (
	"context"
	"log/slog"
	"testing"

	"git.eeqj.de/sneak/ipapi/internal/config"
	"github.com/go-chi/chi/v5"
)

func TestNewServer(t *testing.T) {
	cfg := &config.Config{
		Port: 8080,
	}
	logger := slog.Default()
	router := chi.NewRouter()

	server, err := NewServer(cfg, logger, router)
	if err != nil {
		t.Fatalf("failed to create server: %v", err)
	}

	if server == nil {
		t.Fatal("expected server, got nil")
	}

	if server.config != cfg {
		t.Error("config not set correctly")
	}

	if server.logger != logger {
		t.Error("logger not set correctly")
	}

	if server.router != router {
		t.Error("router not set correctly")
	}
}

func TestServerStartStop(t *testing.T) {
	cfg := &config.Config{
		Port: 0, // Use random port
	}
	logger := slog.Default()
	router := chi.NewRouter()

	server, err := NewServer(cfg, logger, router)
	if err != nil {
		t.Fatalf("failed to create server: %v", err)
	}

	ctx := context.Background()

	// Start server
	if err := server.Start(ctx); err != nil {
		t.Fatalf("failed to start server: %v", err)
	}

	// Stop server
	if err := server.Stop(ctx); err != nil {
		t.Fatalf("failed to stop server: %v", err)
	}
}
