// internal/server/server.go
package server

import (
	"fmt"
	"io/fs"
	"net/http"
	"os/exec"
)

func Start() {
	mux := http.NewServeMux()

	// LLM API
	mux.HandleFunc("/api/llm/console", llmConsoleHandler)
	mux.HandleFunc("/api/llm/models", llmModelsHandler)
	mux.HandleFunc("/api/llm/select", llmSelectHandler)
	mux.HandleFunc("/api/llm/query", llmQueryHandler)
	mux.HandleFunc("/api/llm/status", llmStatusHandler)
	mux.HandleFunc("/api/llm/history", llmHistoryHandler)

	// Заглушки
	mux.HandleFunc("/api/stats", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "<h2>Статистика</h2><p>Пока пусто.</p>")
	})
	mux.HandleFunc("/api/tasks", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "<h2>Задачи</h2><p>Пока пусто.</p>")
	})
	mux.HandleFunc("/api/notes", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "<h2>AI_NOTES.md</h2><pre>Пока пусто.</pre>")
	})

	// Статика — строго последняя
	sub, err := fs.Sub(WebFS, "web")
	if err != nil {
		panic(err)
	}
	mux.Handle("/", http.FileServer(http.FS(sub)))

	fmt.Println("Listening on :4568")
	exec.Command("rundll32", "url.dll,FileProtocolHandler", "http://localhost:4568").Start()
	http.ListenAndServe(":4568", mux)
}
