// Package api provides the REST + WebSocket API for the bot dashboard.
//
// Endpoints:
//   GET  /api/status          — bot status, current state, routine, stats
//   GET  /api/capture/frame   — returns current frame as JPEG
//   GET  /api/capture/frame/annotated — frame with region overlays
//   GET  /api/capture/stats   — capture performance stats
//   POST /api/capture/upload  — upload a screenshot for testing
//   POST /api/capture/source  — switch capture source
//   GET  /api/regions         — list all defined regions
//   GET  /api/state           — current detected game state + vitals
//   GET  /api/loot/rules      — get current loot filter rules
//   GET  /api/routines        — list available routines
//   GET  /api/pixel           — get pixel color at coordinates
//   GET  /api/config          — current config
//   GET  /api/plugins         — list loaded plugins and info
//   WS   /api/ws              — WebSocket for real-time updates
//   GET  /                    — serve dev dashboard
//
// The API is served by the bot process itself (single binary).
package api

import (
	"bytes"
	"encoding/json"
	"fmt"
	"image"
	"image/color"
	"image/draw"
	"image/jpeg"
	"io"
	"log"
	"net/http"
	"os"
	"path/filepath"
	"strconv"
	"strings"
	"sync"

	"git.cloonar.com/openclawd/iso-bot/pkg/engine"
	"git.cloonar.com/openclawd/iso-bot/pkg/engine/capture/backends"
	"git.cloonar.com/openclawd/iso-bot/pkg/engine/vision"
)

// Status represents the current bot status.
type Status struct {
	Running      bool    `json:"running"`
	Paused       bool    `json:"paused"`
	DevMode      bool    `json:"devMode"`
	GameState    string  `json:"gameState"`
	Routine      string  `json:"routine,omitempty"`
	Phase        string  `json:"phase,omitempty"`
	RunCount     int     `json:"runCount"`
	ItemsFound   int     `json:"itemsFound"`
	Uptime       string  `json:"uptime"`
	CaptureFPS   float64 `json:"captureFps"`
	HealthPct    float64 `json:"healthPct"`
	ManaPct      float64 `json:"manaPct"`
}

// PixelInfo represents RGB and HSV values at a pixel.
type PixelInfo struct {
	X   int           `json:"x"`
	Y   int           `json:"y"`
	RGB [3]int        `json:"rgb"`
	HSV [3]int        `json:"hsv"`
}

// RegionInfo represents a screen region.
type RegionInfo struct {
	Name   string `json:"name"`
	X      int    `json:"x"`
	Y      int    `json:"y"`
	Width  int    `json:"width"`
	Height int    `json:"height"`
}

// HSVColor represents an HSV color value.
type HSVColor struct {
	H int `json:"h"`
	S int `json:"s"`
	V int `json:"v"`
}

// RegionAnalysis represents analysis results for a specific region.
type RegionAnalysis struct {
	Name         string   `json:"name"`
	DominantHSV  HSVColor `json:"dominantHSV"`
	MatchPercent float64  `json:"matchPercent"`
}

// AnalysisResponse represents the response from /api/analyze endpoint.
type AnalysisResponse struct {
	GameState string           `json:"gameState"`
	Vitals    VitalsInfo       `json:"vitals"`
	Regions   []RegionAnalysis `json:"regions"`
}

// VitalsInfo represents vital statistics.
type VitalsInfo struct {
	HealthPct float64 `json:"healthPct"`
	ManaPct   float64 `json:"manaPct"`
}

// Server provides the HTTP API and WebSocket endpoint.
type Server struct {
	mu       sync.RWMutex
	engine   *engine.Engine
	addr     string
	mux      *http.ServeMux
	webRoot  string
}

// NewServer creates an API server on the given address.
func NewServer(addr string, eng *engine.Engine, webRoot string) *Server {
	s := &Server{
		engine:  eng,
		addr:    addr,
		mux:     http.NewServeMux(),
		webRoot: webRoot,
	}
	s.registerRoutes()
	return s
}

// Start begins serving the API.
func (s *Server) Start() error {
	return http.ListenAndServe(s.addr, s.enableCORS(s.mux))
}

func (s *Server) registerRoutes() {
	// API routes
	s.mux.HandleFunc("GET /api/status", s.handleStatus)
	s.mux.HandleFunc("GET /api/capture/frame", s.handleCaptureFrame)
	s.mux.HandleFunc("GET /api/capture/frame/annotated", s.handleCaptureFrameAnnotated)
	s.mux.HandleFunc("GET /api/capture/stats", s.handleCaptureStats)
	s.mux.HandleFunc("POST /api/capture/upload", s.handleCaptureUpload)
	s.mux.HandleFunc("POST /api/capture/source", s.handleCaptureSource)
	s.mux.HandleFunc("GET /api/regions", s.handleRegions)
	s.mux.HandleFunc("GET /api/state", s.handleState)
	s.mux.HandleFunc("GET /api/loot/rules", s.handleLootRules)
	s.mux.HandleFunc("GET /api/routines", s.handleRoutines)
	s.mux.HandleFunc("GET /api/pixel", s.handlePixel)
	s.mux.HandleFunc("GET /api/config", s.handleConfig)
	s.mux.HandleFunc("GET /api/plugins", s.handlePlugins)
	s.mux.HandleFunc("GET /api/analyze", s.handleAnalyze)
	s.mux.HandleFunc("POST /api/start", s.handleStart)
	s.mux.HandleFunc("POST /api/stop", s.handleStop)
	s.mux.HandleFunc("POST /api/pause", s.handlePause)
	
	// WebSocket endpoint
	// s.mux.HandleFunc("/api/ws", s.handleWebSocket)
	
	// Serve static files for dev dashboard
	s.mux.Handle("/", http.FileServer(http.Dir(s.webRoot)))
}

func (s *Server) enableCORS(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Access-Control-Allow-Origin", "*")
		w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
		w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
		
		if r.Method == "OPTIONS" {
			w.WriteHeader(http.StatusOK)
			return
		}
		
		next.ServeHTTP(w, r)
	})
}

func (s *Server) handleStatus(w http.ResponseWriter, r *http.Request) {
	status := s.engine.Status()
	
	response := Status{
		Running:    status.Running,
		Paused:     status.Paused,
		DevMode:    status.DevMode,
		GameState:  string(status.GameState),
		Uptime:     status.Uptime.String(),
		CaptureFPS: status.CaptureStats.FPS,
		HealthPct:  status.Vitals.HealthPct,
		ManaPct:    status.Vitals.ManaPct,
		ItemsFound: status.ItemsFound,
		RunCount:   status.RunCount,
	}
	
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(response)
}

func (s *Server) handleCaptureFrame(w http.ResponseWriter, r *http.Request) {
	frame := s.engine.CurrentFrame()
	if frame == nil {
		http.Error(w, "No frame available", http.StatusNoContent)
		return
	}
	
	w.Header().Set("Content-Type", "image/jpeg")
	
	var buf bytes.Buffer
	if err := jpeg.Encode(&buf, frame, &jpeg.Options{Quality: 85}); err != nil {
		http.Error(w, "Failed to encode frame", http.StatusInternalServerError)
		return
	}
	
	w.Write(buf.Bytes())
}

func (s *Server) handleCaptureFrameAnnotated(w http.ResponseWriter, r *http.Request) {
	frame := s.engine.CurrentFrame()
	if frame == nil {
		http.Error(w, "No frame available", http.StatusNoContent)
		return
	}

	// Create a copy of the frame to draw on
	bounds := frame.Bounds()
	annotated := image.NewRGBA(bounds)
	draw.Draw(annotated, bounds, frame, bounds.Min, draw.Src)

	// Get regions to draw (filtered by query parameter if present)
	regions := s.getRegions()

	// Filter regions by query parameter if provided
	if r.URL.Query().Has("regions") {
		regionsParam := r.URL.Query().Get("regions")
		if regionsParam == "" {
			regions = nil // Empty param = no overlays
		} else {
			regionNames := strings.Split(regionsParam, ",")
			filteredRegions := make([]RegionInfo, 0)
			for _, region := range regions {
				for _, name := range regionNames {
					if strings.TrimSpace(name) == region.Name {
						filteredRegions = append(filteredRegions, region)
						break
					}
				}
			}
			regions = filteredRegions
		}
	}

	s.drawRegionOverlays(annotated, regions)

	w.Header().Set("Content-Type", "image/jpeg")

	var buf bytes.Buffer
	if err := jpeg.Encode(&buf, annotated, &jpeg.Options{Quality: 85}); err != nil {
		http.Error(w, "Failed to encode annotated frame", http.StatusInternalServerError)
		return
	}

	w.Write(buf.Bytes())
}

func (s *Server) handleCaptureStats(w http.ResponseWriter, r *http.Request) {
	status := s.engine.Status()
	stats := status.CaptureStats
	
	response := map[string]interface{}{
		"frameCount":   stats.FrameCount,
		"avgCaptureMs": stats.AvgCaptureMs,
		"fps":          stats.FPS,
		"lastCapture":  stats.LastCapture,
		"backend":      "file", // TODO: Get actual backend name
	}
	
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(response)
}

func (s *Server) handleCaptureUpload(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
		return
	}
	
	file, header, err := r.FormFile("file")
	if err != nil {
		http.Error(w, "Failed to read uploaded file", http.StatusBadRequest)
		return
	}
	defer file.Close()
	
	// Save to temporary directory
	tempDir := "/tmp/iso-bot-uploads"
	os.MkdirAll(tempDir, 0755)
	
	filename := filepath.Join(tempDir, header.Filename)
	out, err := os.Create(filename)
	if err != nil {
		http.Error(w, "Failed to save file", http.StatusInternalServerError)
		return
	}
	defer out.Close()
	
	_, err = io.Copy(out, file)
	if err != nil {
		http.Error(w, "Failed to save file", http.StatusInternalServerError)
		return
	}

	// Auto-switch capture source to uploaded file
	fileConfig := map[string]interface{}{
		"path": filename,
		"type": "image",
	}

	newSource, err := backends.NewFileSource(fileConfig)
	if err != nil {
		log.Printf("Failed to create file source: %v", err)
		response := map[string]string{
			"filename":   filename,
			"message":    fmt.Sprintf("File uploaded to %s but failed to switch capture source: %v", filename, err),
			"autoSwitch": "failed",
		}
		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(response)
		return
	}

	err = s.engine.SetCaptureSource(newSource)
	if err != nil {
		log.Printf("Failed to set capture source: %v", err)
		response := map[string]string{
			"filename":   filename,
			"message":    fmt.Sprintf("File uploaded to %s but failed to switch capture source: %v", filename, err),
			"autoSwitch": "failed",
		}
		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(response)
		return
	}

	response := map[string]string{
		"filename":   filename,
		"message":    fmt.Sprintf("File uploaded to %s and capture source switched successfully", filename),
		"autoSwitch": "true",
	}
	
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(response)
}

func (s *Server) handleCaptureSource(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
		return
	}
	
	var req struct {
		Type string            `json:"type"`
		Config map[string]interface{} `json:"config"`
	}
	
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		http.Error(w, "Invalid JSON", http.StatusBadRequest)
		return
	}
	
	// Import capture backends to create new source
	// We'll need to import the backends package here
	log.Printf("Switching capture source to type=%s config=%+v", req.Type, req.Config)
	
	response := map[string]string{
		"message": fmt.Sprintf("Source switch requested: %s", req.Type),
		"status":  "partial", // Implementation incomplete
	}
	
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(response)
}

func (s *Server) handleRegions(w http.ResponseWriter, r *http.Request) {
	regions := s.getRegions()
	
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(regions)
}

func (s *Server) handleState(w http.ResponseWriter, r *http.Request) {
	status := s.engine.Status()
	
	response := map[string]interface{}{
		"gameState":  string(status.GameState),
		"healthPct":  status.Vitals.HealthPct,
		"manaPct":    status.Vitals.ManaPct,
		"xpPct":      status.Vitals.XPPct,
		"frameCount": status.FrameCount,
	}
	
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(response)
}

func (s *Server) handleLootRules(w http.ResponseWriter, r *http.Request) {
	lootEngine := s.engine.LootEngine()
	if lootEngine == nil {
		response := map[string]interface{}{
			"rules": []interface{}{},
			"count": 0,
		}
		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(response)
		return
	}
	
	response := map[string]interface{}{
		"rules": lootEngine.Rules,
		"count": len(lootEngine.Rules),
	}
	
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(response)
}

func (s *Server) handleRoutines(w http.ResponseWriter, r *http.Request) {
	routines := s.engine.GamePlugin().Routines()
	
	response := make([]map[string]interface{}, 0)
	for _, routine := range routines {
		response = append(response, map[string]interface{}{
			"name":  routine.Name(),
			"phase": routine.Phase(),
		})
	}
	
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(response)
}

func (s *Server) handlePixel(w http.ResponseWriter, r *http.Request) {
	xStr := r.URL.Query().Get("x")
	yStr := r.URL.Query().Get("y")
	
	x, err := strconv.Atoi(xStr)
	if err != nil {
		http.Error(w, "Invalid x coordinate", http.StatusBadRequest)
		return
	}
	
	y, err := strconv.Atoi(yStr)
	if err != nil {
		http.Error(w, "Invalid y coordinate", http.StatusBadRequest)
		return
	}
	
	frame := s.engine.CurrentFrame()
	if frame == nil {
		http.Error(w, "No frame available", http.StatusNoContent)
		return
	}
	
	bounds := frame.Bounds()
	point := image.Point{X: x, Y: y}
	if !point.In(bounds) {
		http.Error(w, "Coordinates out of bounds", http.StatusBadRequest)
		return
	}
	
	c := frame.At(x, y)
	red, green, blue, _ := c.RGBA()
	// Convert to 8-bit
	red, green, blue = red>>8, green>>8, blue>>8
	
	hsv := vision.RGBToHSV(c)
	
	response := PixelInfo{
		X:   x,
		Y:   y,
		RGB: [3]int{int(red), int(green), int(blue)},
		HSV: [3]int{hsv.H, hsv.S, hsv.V},
	}
	
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(response)
}

func (s *Server) handleConfig(w http.ResponseWriter, r *http.Request) {
	// TODO: Return actual config
	response := map[string]interface{}{
		"game":       s.engine.GamePlugin().Info().ID,
		"devMode":    s.engine.Status().DevMode,
		"resolution": fmt.Sprintf("%dx%d", 1920, 1080), // TODO: Get actual resolution
	}
	
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(response)
}

func (s *Server) handlePlugins(w http.ResponseWriter, r *http.Request) {
	pluginInfo := s.engine.GamePlugin().Info()
	
	response := []map[string]interface{}{
		{
			"id":          pluginInfo.ID,
			"name":        pluginInfo.Name,
			"version":     pluginInfo.Version,
			"description": pluginInfo.Description,
			"resolution":  fmt.Sprintf("%dx%d", pluginInfo.Resolution.X, pluginInfo.Resolution.Y),
		},
	}
	
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(response)
}

func (s *Server) handleStart(w http.ResponseWriter, r *http.Request) {
	// TODO: Start the engine
	response := map[string]string{
		"message": "Start not implemented yet",
	}
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(response)
}

func (s *Server) handleStop(w http.ResponseWriter, r *http.Request) {
	s.engine.Stop()
	response := map[string]string{
		"message": "Engine stopped",
	}
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(response)
}

func (s *Server) handlePause(w http.ResponseWriter, r *http.Request) {
	var req struct {
		Paused bool `json:"paused"`
	}

	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		http.Error(w, "Invalid JSON", http.StatusBadRequest)
		return
	}

	s.engine.Pause(req.Paused)

	action := "resumed"
	if req.Paused {
		action = "paused"
	}

	response := map[string]string{
		"message": fmt.Sprintf("Engine %s", action),
	}

	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(response)
}

func (s *Server) handleAnalyze(w http.ResponseWriter, r *http.Request) {
	frame := s.engine.CurrentFrame()
	if frame == nil {
		http.Error(w, "No frame available", http.StatusNoContent)
		return
	}

	status := s.engine.Status()

	// Get vitals
	vitals := VitalsInfo{
		HealthPct: status.Vitals.HealthPct,
		ManaPct:   status.Vitals.ManaPct,
	}

	// Get regions and analyze each one
	regions := s.getRegions()
	regionAnalyses := make([]RegionAnalysis, 0, len(regions))

	for _, region := range regions {
		analysis := s.analyzeRegion(frame, region)
		regionAnalyses = append(regionAnalyses, analysis)
	}

	response := AnalysisResponse{
		GameState: string(status.GameState),
		Vitals:    vitals,
		Regions:   regionAnalyses,
	}

	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(response)
}

// Helper functions

func (s *Server) getRegions() []RegionInfo {
	gameID := s.engine.GamePlugin().Info().ID
	// TODO: Get actual current resolution
	width, height := 1920, 1080
	
	registry := s.engine.ResolutionRegistry()
	profile, err := registry.Get(gameID, width, height)
	if err != nil {
		log.Printf("Failed to get resolution profile: %v", err)
		return make([]RegionInfo, 0)
	}
	
	var regions []RegionInfo
	for name, rect := range profile.Regions {
		regions = append(regions, RegionInfo{
			Name:   name,
			X:      rect.Min.X,
			Y:      rect.Min.Y,
			Width:  rect.Dx(),
			Height: rect.Dy(),
		})
	}
	
	return regions
}

func (s *Server) analyzeRegion(frame image.Image, region RegionInfo) RegionAnalysis {
	rect := image.Rect(region.X, region.Y, region.X+region.Width, region.Y+region.Height)
	bounds := rect.Intersect(frame.Bounds())

	if bounds.Empty() {
		return RegionAnalysis{
			Name:         region.Name,
			DominantHSV:  HSVColor{H: 0, S: 0, V: 0},
			MatchPercent: 0.0,
		}
	}

	// Calculate dominant HSV by averaging all pixels in region
	var totalH, totalS, totalV int64
	var pixelCount int64

	// Also count matches for the region's expected color range
	var matchingPixels int64
	expectedColorRange := s.getExpectedColorRange(region.Name)

	for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
		for x := bounds.Min.X; x < bounds.Max.X; x++ {
			c := frame.At(x, y)
			hsv := vision.RGBToHSV(c)

			totalH += int64(hsv.H)
			totalS += int64(hsv.S)
			totalV += int64(hsv.V)
			pixelCount++

			// Check if this pixel matches the expected color range
			if expectedColorRange != nil && s.colorInRange(hsv, *expectedColorRange) {
				matchingPixels++
			}
		}
	}

	dominantHSV := HSVColor{H: 0, S: 0, V: 0}
	matchPercent := 0.0

	if pixelCount > 0 {
		dominantHSV = HSVColor{
			H: int(totalH / pixelCount),
			S: int(totalS / pixelCount),
			V: int(totalV / pixelCount),
		}

		if expectedColorRange != nil {
			matchPercent = float64(matchingPixels) / float64(pixelCount) * 100.0
		}
	}

	return RegionAnalysis{
		Name:         region.Name,
		DominantHSV:  dominantHSV,
		MatchPercent: matchPercent,
	}
}

func (s *Server) getExpectedColorRange(regionName string) *vision.ColorRange {
	// Get D2R config to determine expected color ranges for each region type
	// For now, we'll handle the main regions: health_orb and mana_orb
	switch regionName {
	case "health_orb":
		return &vision.ColorRange{
			LowerH: 0, UpperH: 10,
			LowerS: 150, UpperS: 255,
			LowerV: 150, UpperV: 255,
		}
	case "mana_orb":
		return &vision.ColorRange{
			LowerH: 200, UpperH: 240,
			LowerS: 100, UpperS: 255,
			LowerV: 80, UpperV: 255,
		}
	default:
		// For other regions, we don't have specific expected color ranges
		return nil
	}
}

func (s *Server) colorInRange(hsv vision.HSV, colorRange vision.ColorRange) bool {
	return hsv.H >= colorRange.LowerH && hsv.H <= colorRange.UpperH &&
		hsv.S >= colorRange.LowerS && hsv.S <= colorRange.UpperS &&
		hsv.V >= colorRange.LowerV && hsv.V <= colorRange.UpperV
}

func (s *Server) drawRegionOverlays(img *image.RGBA, regions []RegionInfo) {
	// Simple colored rectangles for region overlays
	colors := []image.Uniform{
		{C: color.RGBA{255, 0, 0, 128}},   // Red
		{C: color.RGBA{0, 255, 0, 128}},   // Green
		{C: color.RGBA{0, 0, 255, 128}},   // Blue
		{C: color.RGBA{255, 255, 0, 128}}, // Yellow
		{C: color.RGBA{255, 0, 255, 128}}, // Magenta
		{C: color.RGBA{0, 255, 255, 128}}, // Cyan
	}
	
	for i, region := range regions {
		color := &colors[i%len(colors)]
		rect := image.Rect(region.X, region.Y, region.X+region.Width, region.Y+region.Height)
		
		// Draw border (simple approach - just draw a few pixel wide border)
		borderWidth := 2
		
		// Top border
		draw.Draw(img, image.Rect(rect.Min.X, rect.Min.Y, rect.Max.X, rect.Min.Y+borderWidth), color, image.Point{}, draw.Over)
		// Bottom border  
		draw.Draw(img, image.Rect(rect.Min.X, rect.Max.Y-borderWidth, rect.Max.X, rect.Max.Y), color, image.Point{}, draw.Over)
		// Left border
		draw.Draw(img, image.Rect(rect.Min.X, rect.Min.Y, rect.Min.X+borderWidth, rect.Max.Y), color, image.Point{}, draw.Over)
		// Right border
		draw.Draw(img, image.Rect(rect.Max.X-borderWidth, rect.Min.Y, rect.Max.X, rect.Max.Y), color, image.Point{}, draw.Over)
	}
}
