package api

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

	"github.com/ez-api/ez-api/internal/model"
	"github.com/gin-gonic/gin"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

func TestMaster_ListSelfLogs_FiltersByMaster(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)
	}

	m1 := &model.Master{Name: "m1", Group: "g", Status: "active", Epoch: 1, MasterKeyDigest: "d1"}
	m2 := &model.Master{Name: "m2", Group: "g", Status: "active", Epoch: 1, MasterKeyDigest: "d2"}
	if err := db.Create(m1).Error; err != nil {
		t.Fatalf("create m1: %v", err)
	}
	if err := db.Create(m2).Error; err != nil {
		t.Fatalf("create m2: %v", err)
	}
	k1 := &model.Key{MasterID: m1.ID, TokenHash: "h1", Group: "g", Status: "active", IssuedAtEpoch: 1}
	k2 := &model.Key{MasterID: m2.ID, TokenHash: "h2", Group: "g", Status: "active", IssuedAtEpoch: 1}
	if err := db.Create(k1).Error; err != nil {
		t.Fatalf("create k1: %v", err)
	}
	if err := db.Create(k2).Error; err != nil {
		t.Fatalf("create k2: %v", err)
	}

	if err := db.Create(&model.LogRecord{Group: "rg", KeyID: k1.ID, ModelName: "ns.m", StatusCode: 200, LatencyMs: 10}).Error; err != nil {
		t.Fatalf("create log1: %v", err)
	}
	if err := db.Create(&model.LogRecord{Group: "rg", KeyID: k2.ID, ModelName: "ns.m", StatusCode: 400, LatencyMs: 20}).Error; err != nil {
		t.Fatalf("create log2: %v", err)
	}

	mh := &MasterHandler{db: db, logDB: db}

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

	r := gin.New()
	r.GET("/v1/logs", withMaster(mh.ListSelfLogs))

	rr := httptest.NewRecorder()
	req := httptest.NewRequest(http.MethodGet, "/v1/logs", nil)
	r.ServeHTTP(rr, req)
	if rr.Code != http.StatusOK {
		t.Fatalf("expected 200, got %d body=%s", rr.Code, rr.Body.String())
	}
	var resp ListMasterLogsResponse
	if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
		t.Fatalf("unmarshal: %v", err)
	}
	if resp.Total != 1 || len(resp.Items) != 1 {
		t.Fatalf("expected 1 item, got total=%d len=%d body=%s", resp.Total, len(resp.Items), rr.Body.String())
	}
	if resp.Items[0].KeyID != k1.ID {
		t.Fatalf("expected key_id %d, got %+v", k1.ID, resp.Items[0])
	}

	var raw map[string]any
	if err := json.Unmarshal(rr.Body.Bytes(), &raw); err != nil {
		t.Fatalf("unmarshal raw: %v", err)
	}
	items, ok := raw["items"].([]any)
	if !ok || len(items) == 0 {
		t.Fatalf("expected items array in response")
	}
	first, ok := items[0].(map[string]any)
	if !ok {
		t.Fatalf("expected item object in response")
	}
	if _, ok := first["provider_id"]; ok {
		t.Fatalf("expected provider_id to be omitted")
	}
	if _, ok := first["provider_type"]; ok {
		t.Fatalf("expected provider_type to be omitted")
	}
	if _, ok := first["provider_name"]; ok {
		t.Fatalf("expected provider_name to be omitted")
	}
	if _, ok := first["client_ip"]; ok {
		t.Fatalf("expected client_ip to be omitted")
	}
}

func TestAdmin_DeleteLogs_BeforeFilters(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.LogRecord{}); err != nil {
		t.Fatalf("migrate: %v", err)
	}

	now := time.Date(2025, 1, 2, 3, 4, 5, 0, time.UTC)
	older := now.Add(-48 * time.Hour)
	cutoff := now.Add(-24 * time.Hour)

	log1 := model.LogRecord{KeyID: 1, ModelName: "m1", StatusCode: 200}
	log1.CreatedAt = older
	log2 := model.LogRecord{KeyID: 2, ModelName: "m1", StatusCode: 200}
	log2.CreatedAt = older
	log3 := model.LogRecord{KeyID: 1, ModelName: "m1", StatusCode: 200}
	log3.CreatedAt = now

	if err := db.Create(&log1).Error; err != nil {
		t.Fatalf("create log1: %v", err)
	}
	if err := db.Create(&log2).Error; err != nil {
		t.Fatalf("create log2: %v", err)
	}
	if err := db.Create(&log3).Error; err != nil {
		t.Fatalf("create log3: %v", err)
	}

	h := &Handler{db: db, logDB: db}
	r := gin.New()
	r.DELETE("/admin/logs", h.DeleteLogs)

	body := []byte(fmt.Sprintf(`{"before":"%s","key_id":1,"model":"m1"}`, cutoff.Format(time.RFC3339)))
	req := httptest.NewRequest(http.MethodDelete, "/admin/logs", bytes.NewReader(body))
	rr := httptest.NewRecorder()
	r.ServeHTTP(rr, req)
	if rr.Code != http.StatusOK {
		t.Fatalf("expected 200, got %d body=%s", rr.Code, rr.Body.String())
	}

	var resp DeleteLogsResponse
	if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
		t.Fatalf("unmarshal: %v", err)
	}
	if resp.DeletedCount != 1 {
		t.Fatalf("expected deleted_count=1, got %d", resp.DeletedCount)
	}

	var remaining int64
	if err := db.Model(&model.LogRecord{}).Count(&remaining).Error; err != nil {
		t.Fatalf("count logs: %v", err)
	}
	if remaining != 2 {
		t.Fatalf("expected 2 logs remaining, got %d", remaining)
	}
}

func TestAdmin_DeleteLogs_RequiresBefore(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.LogRecord{}); err != nil {
		t.Fatalf("migrate: %v", err)
	}

	h := &Handler{db: db, logDB: db}
	r := gin.New()
	r.DELETE("/admin/logs", h.DeleteLogs)

	req := httptest.NewRequest(http.MethodDelete, "/admin/logs", bytes.NewReader([]byte(`{}`)))
	rr := httptest.NewRecorder()
	r.ServeHTTP(rr, req)
	if rr.Code != http.StatusBadRequest {
		t.Fatalf("expected 400, got %d body=%s", rr.Code, rr.Body.String())
	}
}

func TestAdmin_ListLogs_IncludesRequestBody(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.LogRecord{}); err != nil {
		t.Fatalf("migrate: %v", err)
	}

	// Create log with request_body
	log1 := model.LogRecord{
		KeyID:       1,
		ModelName:   "gpt-4",
		StatusCode:  200,
		RequestBody: `{"messages":[{"role":"user","content":"hello"}]}`,
	}
	if err := db.Create(&log1).Error; err != nil {
		t.Fatalf("create log: %v", err)
	}

	h := &Handler{db: db, logDB: db}
	r := gin.New()
	r.GET("/admin/logs", h.ListLogs)

	req := httptest.NewRequest(http.MethodGet, "/admin/logs", nil)
	rr := httptest.NewRecorder()
	r.ServeHTTP(rr, req)
	if rr.Code != http.StatusOK {
		t.Fatalf("expected 200, got %d body=%s", rr.Code, rr.Body.String())
	}

	var resp ListLogsResponse
	if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
		t.Fatalf("unmarshal: %v", err)
	}
	if resp.Total != 1 || len(resp.Items) != 1 {
		t.Fatalf("expected 1 item, got total=%d len=%d", resp.Total, len(resp.Items))
	}
	if resp.Items[0].RequestBody != log1.RequestBody {
		t.Fatalf("expected request_body=%q, got %q", log1.RequestBody, resp.Items[0].RequestBody)
	}
}

func TestLogStats_GroupByModel(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.LogRecord{}); err != nil {
		t.Fatalf("migrate: %v", err)
	}

	// Create logs with different models
	logs := []model.LogRecord{
		{KeyID: 1, ModelName: "gpt-4", TokensIn: 100, TokensOut: 200, StatusCode: 200, LatencyMs: 100},
		{KeyID: 1, ModelName: "gpt-4", TokensIn: 150, TokensOut: 300, StatusCode: 200, LatencyMs: 200},
		{KeyID: 1, ModelName: "claude-3", TokensIn: 80, TokensOut: 160, StatusCode: 200, LatencyMs: 150},
	}
	for _, log := range logs {
		if err := db.Create(&log).Error; err != nil {
			t.Fatalf("create log: %v", err)
		}
	}

	h := &Handler{db: db, logDB: db}
	r := gin.New()
	r.GET("/admin/logs/stats", h.LogStats)

	req := httptest.NewRequest(http.MethodGet, "/admin/logs/stats?group_by=model", nil)
	rr := httptest.NewRecorder()
	r.ServeHTTP(rr, req)
	if rr.Code != http.StatusOK {
		t.Fatalf("expected 200, got %d body=%s", rr.Code, rr.Body.String())
	}

	var resp GroupedStatsResponse
	if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
		t.Fatalf("unmarshal: %v", err)
	}
	if len(resp.Items) != 2 {
		t.Fatalf("expected 2 groups, got %d: %+v", len(resp.Items), resp.Items)
	}

	// Should be sorted by count DESC, so gpt-4 first (2 logs) then claude-3 (1 log)
	if resp.Items[0].Model != "gpt-4" {
		t.Fatalf("expected first group to be gpt-4, got %s", resp.Items[0].Model)
	}
	if resp.Items[0].Count != 2 {
		t.Fatalf("expected gpt-4 count=2, got %d", resp.Items[0].Count)
	}
	if resp.Items[0].TokensIn != 250 {
		t.Fatalf("expected gpt-4 tokens_in=250, got %d", resp.Items[0].TokensIn)
	}
	if resp.Items[1].Model != "claude-3" {
		t.Fatalf("expected second group to be claude-3, got %s", resp.Items[1].Model)
	}
	if resp.Items[1].Count != 1 {
		t.Fatalf("expected claude-3 count=1, got %d", resp.Items[1].Count)
	}
}

func TestLogStats_DefaultBehavior(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.LogRecord{}); err != nil {
		t.Fatalf("migrate: %v", err)
	}

	// Create some logs
	logs := []model.LogRecord{
		{KeyID: 1, ModelName: "gpt-4", TokensIn: 100, TokensOut: 200, StatusCode: 200, LatencyMs: 100},
		{KeyID: 1, ModelName: "gpt-4", TokensIn: 150, TokensOut: 300, StatusCode: 500, LatencyMs: 200},
	}
	for _, log := range logs {
		if err := db.Create(&log).Error; err != nil {
			t.Fatalf("create log: %v", err)
		}
	}

	h := &Handler{db: db, logDB: db}
	r := gin.New()
	r.GET("/admin/logs/stats", h.LogStats)

	// Without group_by, should return aggregated stats
	req := httptest.NewRequest(http.MethodGet, "/admin/logs/stats", nil)
	rr := httptest.NewRecorder()
	r.ServeHTTP(rr, req)
	if rr.Code != http.StatusOK {
		t.Fatalf("expected 200, got %d body=%s", rr.Code, rr.Body.String())
	}

	var resp LogStatsResponse
	if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
		t.Fatalf("unmarshal: %v", err)
	}
	if resp.Total != 2 {
		t.Fatalf("expected total=2, got %d", resp.Total)
	}
	if resp.TokensIn != 250 {
		t.Fatalf("expected tokens_in=250, got %d", resp.TokensIn)
	}
	if resp.TokensOut != 500 {
		t.Fatalf("expected tokens_out=500, got %d", resp.TokensOut)
	}
	if len(resp.ByStatus) != 2 {
		t.Fatalf("expected 2 status buckets, got %d", len(resp.ByStatus))
	}
	if resp.ByStatus["200"] != 1 || resp.ByStatus["500"] != 1 {
		t.Fatalf("unexpected by_status: %+v", resp.ByStatus)
	}
}

func TestTrafficChart_TopNOtherAggregation(t *testing.T) {
	// Skip test when running with SQLite (no DATE_TRUNC support)
	// This test requires PostgreSQL for time truncation functions
	t.Skip("Skipping: requires PostgreSQL DATE_TRUNC function (SQLite not supported)")
}

func TestTrafficChart_MinuteGranularityValidation(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.LogRecord{}); err != nil {
		t.Fatalf("migrate: %v", err)
	}

	h := &Handler{db: db, logDB: db}
	r := gin.New()
	r.GET("/admin/logs/stats/traffic-chart", h.GetTrafficChart)

	tests := []struct {
		name       string
		query      string
		wantStatus int
		wantError  string
	}{
		{
			name:       "minute without since",
			query:      "?granularity=minute&until=1735689600",
			wantStatus: http.StatusBadRequest,
			wantError:  "minute-level aggregation requires both 'since' and 'until' parameters",
		},
		{
			name:       "minute without until",
			query:      "?granularity=minute&since=1735689600",
			wantStatus: http.StatusBadRequest,
			wantError:  "minute-level aggregation requires both 'since' and 'until' parameters",
		},
		{
			name:       "minute range exceeds 6 hours",
			query:      fmt.Sprintf("?granularity=minute&since=%d&until=%d", time.Now().Add(-8*time.Hour).Unix(), time.Now().Unix()),
			wantStatus: http.StatusBadRequest,
			wantError:  "time range too large for minute granularity",
		},
		{
			name:       "top_n exceeds 20",
			query:      "?top_n=25",
			wantStatus: http.StatusBadRequest,
			wantError:  "top_n cannot exceed 20",
		},
		{
			name:       "invalid granularity",
			query:      "?granularity=day",
			wantStatus: http.StatusBadRequest,
			wantError:  "granularity must be 'hour' or 'minute'",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			req := httptest.NewRequest(http.MethodGet, "/admin/logs/stats/traffic-chart"+tt.query, nil)
			rr := httptest.NewRecorder()
			r.ServeHTTP(rr, req)

			if rr.Code != tt.wantStatus {
				t.Fatalf("expected status %d, got %d body=%s", tt.wantStatus, rr.Code, rr.Body.String())
			}

			var resp map[string]any
			if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
				t.Fatalf("unmarshal: %v", err)
			}
			if errMsg, ok := resp["error"].(string); !ok || errMsg != tt.wantError {
				t.Fatalf("expected error=%q, got %v", tt.wantError, resp["error"])
			}
		})
	}
}

func TestTrafficChart_DefaultParameters(t *testing.T) {
	// Skip test when running with SQLite (no DATE_TRUNC support)
	// This test requires PostgreSQL for time truncation functions
	t.Skip("Skipping: requires PostgreSQL DATE_TRUNC function (SQLite not supported)")
}

func TestTrafficChart_EmptyResult(t *testing.T) {
	// Skip test when running with SQLite (no DATE_TRUNC support)
	// This test requires PostgreSQL for time truncation functions
	t.Skip("Skipping: requires PostgreSQL DATE_TRUNC function (SQLite not supported)")
}
