package handlers

import (
	"net/http"
	"strings"

	"github.com/gin-gonic/gin"
)

// GetAdminRegistration
// @Summary 注册策略与邀请码列表
// @Tags admin
// @Produce json
// @Security AdminToken
// @Success 200 {object} map[string]interface{}
// @Failure 401 {object} map[string]interface{}
// @Router /api/admin/registration [get]
func (h *Handler) GetAdminRegistration(c *gin.Context) {
	cfg := h.store.GetRegistrationConfig()
	c.JSON(http.StatusOK, gin.H{
		"requireInviteCode":         cfg.RequireInviteCode,
		"forbiddenAccounts":         cfg.ForbiddenAccounts,
		"inviteRegisterRewardCoins": cfg.InviteRegisterRewardCoins,
		"invites":                   cfg.Invites,
	})
}

// PutAdminRegistrationPolicy
// @Summary 更新注册策略
// @Tags admin
// @Accept json
// @Produce json
// @Security AdminToken
// @Param body body UpdateRegistrationPolicyRequest true "策略"
// @Success 200 {object} map[string]interface{}
// @Failure 400 {object} map[string]interface{}
// @Failure 401 {object} map[string]interface{}
// @Failure 500 {object} map[string]interface{}
// @Router /api/admin/registration [put]
func (h *Handler) PutAdminRegistrationPolicy(c *gin.Context) {
	var req UpdateRegistrationPolicyRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request"})
		return
	}
	if err := h.store.UpdateRegistrationPolicy(req.RequireInviteCode, req.ForbiddenAccounts, req.InviteRegisterRewardCoins); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to save registration policy"})
		return
	}
	cfg := h.store.GetRegistrationConfig()
	c.JSON(http.StatusOK, gin.H{
		"requireInviteCode":         cfg.RequireInviteCode,
		"forbiddenAccounts":         cfg.ForbiddenAccounts,
		"inviteRegisterRewardCoins": cfg.InviteRegisterRewardCoins,
	})
}

// PostAdminInvite
// @Summary 创建邀请码
// @Tags admin
// @Accept json
// @Produce json
// @Security AdminToken
// @Param body body CreateInviteRequest true "邀请配置"
// @Success 201 {object} map[string]interface{}
// @Failure 400 {object} map[string]interface{}
// @Failure 401 {object} map[string]interface{}
// @Router /api/admin/registration/invites [post]
func (h *Handler) PostAdminInvite(c *gin.Context) {
	var req CreateInviteRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request"})
		return
	}
	entry, err := h.store.AddInviteEntry(req.Note, req.MaxUses, strings.TrimSpace(req.ExpiresAt))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusCreated, gin.H{"invite": entry})
}

// DeleteAdminInvite
// @Summary 删除邀请码
// @Tags admin
// @Produce json
// @Security AdminToken
// @Param code path string true "邀请码"
// @Success 200 {object} map[string]interface{}
// @Failure 400 {object} map[string]interface{}
// @Failure 401 {object} map[string]interface{}
// @Failure 404 {object} map[string]interface{}
// @Router /api/admin/registration/invites/{code} [delete]
func (h *Handler) DeleteAdminInvite(c *gin.Context) {
	code := strings.TrimSpace(c.Param("code"))
	if err := h.store.DeleteInviteEntry(code); err != nil {
		if strings.Contains(err.Error(), "not found") {
			c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
			return
		}
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"deleted": true})
}
