package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/google/uuid"
	"gopkg.in/yaml.v3"
)

func main() {
	// Load config.yaml
	data, err := os.ReadFile("config.yaml")
	if err != nil {
		log.Fatalf("Failed to read config.yaml: %v", err)
	}
	cfg := &Config{}
	if err := yaml.Unmarshal(data, cfg); err != nil {
		log.Fatalf("Failed to parse config.yaml: %v", err)
	}
	config = cfg

	// Generate session ID (persist across requests)
	sessionID := uuid.New().String()

	// Register routes
	http.HandleFunc("/v1/chat/completions", func(w http.ResponseWriter, r *http.Request) {
		r = r.WithContext(contextWithSessionIDInContext(r.Context(), sessionID))
		handleChatCompletions(w, r)
	})
	http.HandleFunc("/v1/models", handleModels)
	http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
		fmt.Fprint(w, "OK")
	})

	addr := fmt.Sprintf(":%d", config.Port)
	log.Printf("Starting proxx on %s, upstream: %s", addr, config.UpstreamURL)
	if err := http.ListenAndServe(addr, nil); err != nil {
		log.Fatalf("Server failed: %v", err)
	}
}

// contextKey is a custom type for context keys
type contextKey string

const sessionIDKey contextKey = "sessionID"

// contextWithSessionID creates a context with the session ID
func contextWithSessionID(sessionID string) context.Context {
	return context.WithValue(nil, sessionIDKey, sessionID)
}

// contextWithSessionIDInContext creates a new context with session ID in existing context
func contextWithSessionIDInContext(parent context.Context, sessionID string) context.Context {
	return context.WithValue(parent, sessionIDKey, sessionID)
}
