package api

import (
	"encoding/json"
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/alicebob/miniredis/v2"
	"github.com/ez-api/ez-api/internal/model"
	"github.com/ez-api/ez-api/internal/service"
	"github.com/gin-gonic/gin"
	"github.com/redis/go-redis/v9"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

func TestAdminMasterRealtimeEndpoints(t *testing.T) {
	gin.SetMode(gin.TestMode)

	dsn := fmt.Sprintf("file:%s?mode=memory&cache=shared", t.Name())
	db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
	if err != nil {
		t.Fatalf("open sqlite: %v", err)
	}
	if err := db.AutoMigrate(&model.Master{}); err != nil {
		t.Fatalf("migrate: %v", err)
	}

	m := &model.Master{Name: "m1", Group: "g", Status: "active", Epoch: 1}
	if err := db.Create(m).Error; err != nil {
		t.Fatalf("create master: %v", err)
	}

	mr := miniredis.RunT(t)
	rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
	mr.Set(fmt.Sprintf("master:stats:%d:requests", m.ID), "12")
	mr.Set(fmt.Sprintf("master:stats:%d:tokens", m.ID), "34")
	mr.HSet(fmt.Sprintf("master:rate:%d", m.ID), "qps", "2")
	mr.HSet(fmt.Sprintf("master:rate:%d", m.ID), "limit", "5")
	mr.HSet(fmt.Sprintf("master:rate:%d", m.ID), "blocked", "0")
	mr.HSet(fmt.Sprintf("master:rate:%d", m.ID), "updated_at", "1700000200")

	syncSvc := service.NewSyncService(rdb)
	masterSvc := service.NewMasterService(db)
	statsSvc := service.NewStatsService(rdb)
	adminHandler := NewAdminHandler(db, db, masterSvc, syncSvc, statsSvc, nil)

	r := gin.New()
	r.GET("/admin/masters/:id", adminHandler.GetMaster)
	r.GET("/admin/masters/:id/realtime", adminHandler.GetMasterRealtime)

	rr := httptest.NewRecorder()
	req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/admin/masters/%d", m.ID), nil)
	r.ServeHTTP(rr, req)
	if rr.Code != http.StatusOK {
		t.Fatalf("expected 200, got %d body=%s", rr.Code, rr.Body.String())
	}
	var view MasterView
	if err := json.Unmarshal(rr.Body.Bytes(), &view); err != nil {
		t.Fatalf("unmarshal master view: %v", err)
	}
	if view.Realtime == nil || view.Realtime.QPS != 2 || view.Realtime.QPSLimit != 5 {
		t.Fatalf("unexpected realtime in master view: %+v", view.Realtime)
	}

	rr = httptest.NewRecorder()
	req = httptest.NewRequest(http.MethodGet, fmt.Sprintf("/admin/masters/%d/realtime", m.ID), nil)
	r.ServeHTTP(rr, req)
	if rr.Code != http.StatusOK {
		t.Fatalf("expected 200, got %d body=%s", rr.Code, rr.Body.String())
	}
	var realtime MasterRealtimeView
	if err := json.Unmarshal(rr.Body.Bytes(), &realtime); err != nil {
		t.Fatalf("unmarshal realtime: %v", err)
	}
	if realtime.Requests != 12 || realtime.Tokens != 34 || realtime.QPS != 2 {
		t.Fatalf("unexpected realtime payload: %+v", realtime)
	}
}

func TestMasterSelfRealtime(t *testing.T) {
	gin.SetMode(gin.TestMode)

	dsn := fmt.Sprintf("file:%s?mode=memory&cache=shared", t.Name())
	db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
	if err != nil {
		t.Fatalf("open sqlite: %v", err)
	}
	if err := db.AutoMigrate(&model.Master{}); err != nil {
		t.Fatalf("migrate: %v", err)
	}

	m := &model.Master{Name: "m1", Group: "g", Status: "active", Epoch: 1}
	if err := db.Create(m).Error; err != nil {
		t.Fatalf("create master: %v", err)
	}

	mr := miniredis.RunT(t)
	rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
	mr.Set(fmt.Sprintf("master:stats:%d:requests", m.ID), "6")
	mr.Set(fmt.Sprintf("master:stats:%d:tokens", m.ID), "8")

	syncSvc := service.NewSyncService(rdb)
	masterSvc := service.NewMasterService(db)
	statsSvc := service.NewStatsService(rdb)
	masterHandler := NewMasterHandler(db, db, masterSvc, syncSvc, statsSvc, nil)

	withMaster := func(next gin.HandlerFunc) gin.HandlerFunc {
		return func(c *gin.Context) {
			c.Set("master", m)
			next(c)
		}
	}

	r := gin.New()
	r.GET("/v1/realtime", withMaster(masterHandler.GetSelfRealtime))

	rr := httptest.NewRecorder()
	req := httptest.NewRequest(http.MethodGet, "/v1/realtime", nil)
	r.ServeHTTP(rr, req)
	if rr.Code != http.StatusOK {
		t.Fatalf("expected 200, got %d body=%s", rr.Code, rr.Body.String())
	}
	var realtime MasterRealtimeView
	if err := json.Unmarshal(rr.Body.Bytes(), &realtime); err != nil {
		t.Fatalf("unmarshal realtime: %v", err)
	}
	if realtime.Requests != 6 || realtime.Tokens != 8 {
		t.Fatalf("unexpected realtime payload: %+v", realtime)
	}
}
