package api

import (
	"3588AdminBackend/internal/config"
	"3588AdminBackend/internal/models"
	"3588AdminBackend/internal/service"
	"bytes"
	"encoding/json"
	"net"
	"net/http"
	"net/http/httptest"
	"strconv"
	"strings"
	"testing"
	"time"

	"github.com/go-chi/chi/v5"
)

func TestHandler_ListDevices(t *testing.T) {
	cfg := &config.Config{}
	agent := service.NewAgentClient(cfg)
	reg := service.NewRegistryService(cfg, agent)
	reg.UpdateDevice(&models.Device{DeviceID: "dev1", IP: "127.0.0.1"})

	h := NewHandler(nil, reg, agent, nil, nil)

	req, _ := http.NewRequest("GET", "/api/devices", nil)
	rr := httptest.NewRecorder()

	h.ListDevices(rr, req)

	if rr.Code != http.StatusOK {
		t.Errorf("expected status 200, got %d", rr.Code)
	}

	var resp map[string][]models.Device
	json.Unmarshal(rr.Body.Bytes(), &resp)

	if len(resp["items"]) != 1 {
		t.Errorf("expected 1 item, got %d", len(resp["items"]))
	}
}

func TestHandler_CreateTask(t *testing.T) {
	cfg := &config.Config{Concurrency: 1}
	agent := service.NewAgentClient(cfg)
	reg := service.NewRegistryService(cfg, agent)
	tasks := service.NewTaskService(cfg, agent, reg)

	h := NewHandler(nil, reg, agent, tasks, nil)

	body := map[string]interface{}{
		"type":       "config_apply",
		"device_ids": []string{"dev1"},
		"payload":    map[string]string{"foo": "bar"},
	}
	b, _ := json.Marshal(body)

	req, _ := http.NewRequest("POST", "/api/tasks", bytes.NewBuffer(b))
	rr := httptest.NewRecorder()

	h.CreateTask(rr, req)

	if rr.Code != http.StatusOK {
		t.Errorf("expected status 200, got %d", rr.Code)
	}

	var resp map[string]string
	json.Unmarshal(rr.Body.Bytes(), &resp)

	if resp["task_id"] == "" {
		t.Error("expected task_id in response")
	}
}

func TestHandler_ListTasks(t *testing.T) {
	cfg := &config.Config{Concurrency: 1}
	agent := service.NewAgentClient(cfg)
	reg := service.NewRegistryService(cfg, agent)
	tasks := service.NewTaskService(cfg, agent, reg)

	// Create a task so list is non-empty
	if _, err := tasks.CreateTask("config_apply", []string{"dev1"}, map[string]string{"foo": "bar"}); err != nil {
		t.Fatalf("failed to create task: %v", err)
	}

	h := NewHandler(nil, reg, agent, tasks, nil)

	req, _ := http.NewRequest("GET", "/api/tasks", nil)
	rr := httptest.NewRecorder()

	h.ListTasks(rr, req)

	if rr.Code != http.StatusOK {
		t.Errorf("expected status 200, got %d", rr.Code)
	}

	var resp map[string][]models.Task
	json.Unmarshal(rr.Body.Bytes(), &resp)

	if len(resp["items"]) < 1 {
		t.Errorf("expected at least 1 item, got %d", len(resp["items"]))
	}
}

func TestHandler_ProxyAgentMapsConfigStatus(t *testing.T) {
	agentServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != http.MethodGet {
			t.Fatalf("expected GET, got %s", r.Method)
		}
		if r.URL.Path != "/v1/config/status" {
			t.Fatalf("expected /v1/config/status, got %s", r.URL.Path)
		}
		w.Header().Set("Content-Type", "application/json")
		_, _ = w.Write([]byte(`{"ok":true,"metadata":{"config_id":"local_3588_face_debug"}}`))
	}))
	defer agentServer.Close()

	host, portText, err := net.SplitHostPort(strings.TrimPrefix(agentServer.URL, "http://"))
	if err != nil {
		t.Fatalf("parse test server address: %v", err)
	}
	port, err := strconv.Atoi(portText)
	if err != nil {
		t.Fatalf("parse test server port: %v", err)
	}

	cfg := &config.Config{}
	agent := service.NewAgentClient(cfg)
	reg := service.NewRegistryService(cfg, agent)
	reg.UpdateDevice(&models.Device{DeviceID: "edge-01", IP: host, AgentPort: port})
	h := NewHandler(nil, reg, agent, nil, nil)

	r := chi.NewRouter()
	r.Get("/api/devices/{id}/config/status", h.ProxyAgent)
	rr := httptest.NewRecorder()
	r.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/api/devices/edge-01/config/status", nil))

	if rr.Code != http.StatusOK {
		t.Fatalf("expected status 200, got %d: %s", rr.Code, rr.Body.String())
	}
	if !strings.Contains(rr.Body.String(), "local_3588_face_debug") {
		t.Fatalf("expected config status response, got %s", rr.Body.String())
	}
}

func TestHandler_ProxyAgentMapsConfigCandidate(t *testing.T) {
	agentServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != http.MethodPut {
			t.Fatalf("expected PUT, got %s", r.Method)
		}
		if r.URL.Path != "/v1/config/candidate" {
			t.Fatalf("expected /v1/config/candidate, got %s", r.URL.Path)
		}
		w.Header().Set("Content-Type", "application/json")
		_, _ = w.Write([]byte(`{"ok":true,"path":"/tmp/media-server.json.candidate.json"}`))
	}))
	defer agentServer.Close()

	host, portText, err := net.SplitHostPort(strings.TrimPrefix(agentServer.URL, "http://"))
	if err != nil {
		t.Fatalf("parse test server address: %v", err)
	}
	port, err := strconv.Atoi(portText)
	if err != nil {
		t.Fatalf("parse test server port: %v", err)
	}

	cfg := &config.Config{}
	agent := service.NewAgentClient(cfg)
	reg := service.NewRegistryService(cfg, agent)
	reg.UpdateDevice(&models.Device{DeviceID: "edge-01", IP: host, AgentPort: port})
	h := NewHandler(nil, reg, agent, nil, nil)

	r := chi.NewRouter()
	r.Put("/api/devices/{id}/config/candidate", h.ProxyAgent)
	rr := httptest.NewRecorder()
	r.ServeHTTP(rr, httptest.NewRequest(http.MethodPut, "/api/devices/edge-01/config/candidate", strings.NewReader(`{"metadata":{"config_id":"preview"},"templates":{"t":{}},"instances":[]}`)))

	if rr.Code != http.StatusOK {
		t.Fatalf("expected status 200, got %d: %s", rr.Code, rr.Body.String())
	}
	if !strings.Contains(rr.Body.String(), "candidate") {
		t.Fatalf("expected candidate response, got %s", rr.Body.String())
	}
}

func TestHandler_ProxyAgentMapsConfigCandidateApply(t *testing.T) {
	agentServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != http.MethodPost {
			t.Fatalf("expected POST, got %s", r.Method)
		}
		if r.URL.Path != "/v1/config/candidate/apply" {
			t.Fatalf("expected /v1/config/candidate/apply, got %s", r.URL.Path)
		}
		w.Header().Set("Content-Type", "application/json")
		_, _ = w.Write([]byte(`{"ok":true,"status":{"metadata":{"config_id":"candidate"}}}`))
	}))
	defer agentServer.Close()

	host, portText, err := net.SplitHostPort(strings.TrimPrefix(agentServer.URL, "http://"))
	if err != nil {
		t.Fatalf("parse test server address: %v", err)
	}
	port, err := strconv.Atoi(portText)
	if err != nil {
		t.Fatalf("parse test server port: %v", err)
	}

	cfg := &config.Config{}
	agent := service.NewAgentClient(cfg)
	reg := service.NewRegistryService(cfg, agent)
	reg.UpdateDevice(&models.Device{DeviceID: "edge-01", IP: host, AgentPort: port})
	h := NewHandler(nil, reg, agent, nil, nil)

	r := chi.NewRouter()
	r.Post("/api/devices/{id}/config/candidate/apply", h.ProxyAgent)
	rr := httptest.NewRecorder()
	r.ServeHTTP(rr, httptest.NewRequest(http.MethodPost, "/api/devices/edge-01/config/candidate/apply", nil))

	if rr.Code != http.StatusOK {
		t.Fatalf("expected status 200, got %d: %s", rr.Code, rr.Body.String())
	}
	if !strings.Contains(rr.Body.String(), "candidate") {
		t.Fatalf("expected candidate apply response, got %s", rr.Body.String())
	}
}

func TestHandler_ProxyAgentMapsLongRunningConfigCandidateApply(t *testing.T) {
	agentServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != http.MethodPost {
			t.Fatalf("expected POST, got %s", r.Method)
		}
		if r.URL.Path != "/v1/config/candidate/apply" {
			t.Fatalf("expected /v1/config/candidate/apply, got %s", r.URL.Path)
		}
		time.Sleep(3500 * time.Millisecond)
		w.Header().Set("Content-Type", "application/json")
		_, _ = w.Write([]byte(`{"ok":true,"status":{"metadata":{"config_id":"candidate"}}}`))
	}))
	defer agentServer.Close()

	host, portText, err := net.SplitHostPort(strings.TrimPrefix(agentServer.URL, "http://"))
	if err != nil {
		t.Fatalf("parse test server address: %v", err)
	}
	port, err := strconv.Atoi(portText)
	if err != nil {
		t.Fatalf("parse test server port: %v", err)
	}

	cfg := &config.Config{}
	agent := service.NewAgentClient(cfg)
	reg := service.NewRegistryService(cfg, agent)
	reg.UpdateDevice(&models.Device{DeviceID: "edge-01", IP: host, AgentPort: port})
	h := NewHandler(nil, reg, agent, nil, nil)

	r := chi.NewRouter()
	r.Post("/api/devices/{id}/config/candidate/apply", h.ProxyAgent)
	rr := httptest.NewRecorder()
	r.ServeHTTP(rr, httptest.NewRequest(http.MethodPost, "/api/devices/edge-01/config/candidate/apply", nil))

	if rr.Code != http.StatusOK {
		t.Fatalf("expected status 200, got %d: %s", rr.Code, rr.Body.String())
	}
	if !strings.Contains(rr.Body.String(), "candidate") {
		t.Fatalf("expected candidate apply response, got %s", rr.Body.String())
	}
}

func TestHandler_ProxyAgentMapsLongRunningRollback(t *testing.T) {
	agentServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != http.MethodPost {
			t.Fatalf("expected POST, got %s", r.Method)
		}
		if r.URL.Path != "/v1/media-server/rollback" {
			t.Fatalf("expected /v1/media-server/rollback, got %s", r.URL.Path)
		}
		time.Sleep(3500 * time.Millisecond)
		w.Header().Set("Content-Type", "application/json")
		_, _ = w.Write([]byte(`{"ok":true}`))
	}))
	defer agentServer.Close()

	host, portText, err := net.SplitHostPort(strings.TrimPrefix(agentServer.URL, "http://"))
	if err != nil {
		t.Fatalf("parse test server address: %v", err)
	}
	port, err := strconv.Atoi(portText)
	if err != nil {
		t.Fatalf("parse test server port: %v", err)
	}

	cfg := &config.Config{}
	agent := service.NewAgentClient(cfg)
	reg := service.NewRegistryService(cfg, agent)
	reg.UpdateDevice(&models.Device{DeviceID: "edge-01", IP: host, AgentPort: port})
	h := NewHandler(nil, reg, agent, nil, nil)

	r := chi.NewRouter()
	r.Post("/api/devices/{id}/rollback", h.ProxyAgent)
	rr := httptest.NewRecorder()
	r.ServeHTTP(rr, httptest.NewRequest(http.MethodPost, "/api/devices/edge-01/rollback", nil))

	if rr.Code != http.StatusOK {
		t.Fatalf("expected status 200, got %d: %s", rr.Code, rr.Body.String())
	}
}
