package controller

import (
	"net/http"

	"metazone.cc/metalab/internal/common"

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

// ChangePassword 修改密码（需登录）
func (sc *SettingsController) ChangePassword(c *gin.Context) {
	uid, exists := c.Get("uid")
	if !exists {
		common.Error(c, http.StatusUnauthorized, "请先登录")
		return
	}

	var req struct {
		CurrentPassword string `json:"current_password" binding:"required"`
		NewPassword     string `json:"new_password" binding:"required,min=8"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		common.Error(c, http.StatusBadRequest, "请检查输入")
		return
	}

	if err := sc.authService.ChangePassword(uid.(uint), req.CurrentPassword, req.NewPassword); err != nil {
		handleSettingsError(c, err)
		return
	}

	common.OkMessage(c, "密码已修改，请重新登录")
}

// DeleteAccount 自助注销账号（需登录，验证密码 + 注销原因）
func (sc *SettingsController) DeleteAccount(c *gin.Context) {
	uid, exists := c.Get("uid")
	if !exists {
		common.Error(c, http.StatusUnauthorized, "请先登录")
		return
	}

	var req struct {
		Password string `json:"password" binding:"required"`
		Reason   string `json:"reason"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		common.Error(c, http.StatusBadRequest, "请检查输入")
		return
	}

	if err := sc.authService.DeleteAccount(uid.(uint), req.Password, req.Reason); err != nil {
		handleSettingsError(c, err)
		return
	}

	common.OkMessage(c, "账号已注销，7 天内重新登录即可恢复")
}
