package api

import (
	"bytes"
	"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 TestMaster_ListTokens_AndUpdateToken(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{}, &model.Key{}, &model.LogRecord{}); err != nil {
		t.Fatalf("migrate: %v", err)
	}

	m := &model.Master{Name: "m1", Group: "g", Status: "active", Epoch: 1, DefaultNamespace: "ns", Namespaces: "ns", MaxChildKeys: 5, GlobalQPS: 3}
	if err := db.Create(m).Error; err != nil {
		t.Fatalf("create master: %v", err)
	}
	k := &model.Key{MasterID: m.ID, TokenHash: "h", Group: "g", Status: "active", Scopes: "chat:write", IssuedAtEpoch: 1, DefaultNamespace: "ns", Namespaces: "ns"}
	if err := db.Create(k).Error; err != nil {
		t.Fatalf("create key: %v", err)
	}

	mr := miniredis.RunT(t)
	rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
	syncSvc := service.NewSyncService(rdb)
	masterSvc := service.NewMasterService(db)
	statsSvc := service.NewStatsService(rdb)
	h := 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/tokens", withMaster(h.ListTokens))
	r.PUT("/v1/tokens/:id", withMaster(h.UpdateToken))

	rr := httptest.NewRecorder()
	req := httptest.NewRequest(http.MethodGet, "/v1/tokens", nil)
	r.ServeHTTP(rr, req)
	if rr.Code != http.StatusOK {
		t.Fatalf("expected 200, got %d body=%s", rr.Code, rr.Body.String())
	}
	var list []TokenView
	if err := json.Unmarshal(rr.Body.Bytes(), &list); err != nil {
		t.Fatalf("unmarshal list: %v", err)
	}
	if len(list) != 1 || list[0].ID != k.ID {
		t.Fatalf("unexpected list: %+v", list)
	}

	body := []byte(`{"scopes":"chat:write,chat:read","status":"suspended"}`)
	rr = httptest.NewRecorder()
	req = httptest.NewRequest(http.MethodPut, fmt.Sprintf("/v1/tokens/%d", k.ID), bytes.NewReader(body))
	req.Header.Set("Content-Type", "application/json")
	r.ServeHTTP(rr, req)
	if rr.Code != http.StatusOK {
		t.Fatalf("expected 200, got %d body=%s", rr.Code, rr.Body.String())
	}
	var updated TokenView
	if err := json.Unmarshal(rr.Body.Bytes(), &updated); err != nil {
		t.Fatalf("unmarshal updated: %v", err)
	}
	if updated.Status != "suspended" {
		t.Fatalf("expected status suspended, got %+v", updated)
	}
	if mr.HGet("auth:token:h", "status") != "suspended" {
		t.Fatalf("expected redis token status suspended, got %q", mr.HGet("auth:token:h", "status"))
	}
}
