package handler

import (
	"net/http"

	"github.com/gin-gonic/gin"

	"git.yeij.top/AskaEth/Cyrene/gateway/internal/config"
)

// ThinkingScheduleHandler handles CRUD for the thinking schedule config.
type ThinkingScheduleHandler struct {
	store *config.ThinkingScheduleStore
}

// NewThinkingScheduleHandler creates a new handler.
func NewThinkingScheduleHandler(store *config.ThinkingScheduleStore) *ThinkingScheduleHandler {
	return &ThinkingScheduleHandler{store: store}
}

// GetSchedule returns the current schedule config.
// GET /api/v1/admin/thinking-schedule
func (h *ThinkingScheduleHandler) GetSchedule(c *gin.Context) {
	cfg := h.store.GetConfig()
	if cfg == nil {
		cfg = config.DefaultThinkingScheduleConfig()
	}
	c.JSON(http.StatusOK, cfg)
}

// SetSchedule replaces the entire schedule config.
// PUT /api/v1/admin/thinking-schedule
func (h *ThinkingScheduleHandler) SetSchedule(c *gin.Context) {
	var cfg config.ThinkingScheduleConfig
	if err := c.ShouldBindJSON(&cfg); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数无效: " + err.Error()})
		return
	}

	if err := h.store.SetConfig(&cfg); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"status": "saved"})
}
