package handler

import (
	"kra/internal/service"
	"kra/internal/service/dto"
	"strconv"
	"time"

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

type User struct {
	service *service.UserService
	auth    *service.AuthService
}

func NewUser(user *service.UserService, auth *service.AuthService) *User {
	return &User{service: user, auth: auth}
}

func (h *User) List(c *gin.Context) {
	var req dto.UserListRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if req.Page == 0 {
		Fail(c, "Page值不能为空")
		return
	}
	if req.PageSize == 0 {
		Fail(c, "PageSize值不能为空")
		return
	}
	items, total, err := h.service.ListUsersRequest(c.Request.Context(), &req)
	if err != nil {
		Fail(c, "获取失败")
		return
	}
	Write(c, CodeSuccess, PageResult{List: items, Total: total, Page: req.Page, PageSize: req.PageSize}, "获取成功")
}

func (h *User) Create(c *gin.Context) {
	var req dto.UserRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if req.Username == "" {
		Fail(c, "Username值不能为空")
		return
	}
	if req.Password == "" {
		Fail(c, "Password值不能为空")
		return
	}
	if req.NickName == "" {
		Fail(c, "NickName值不能为空")
		return
	}
	if req.AuthorityID == 0 {
		Fail(c, "AuthorityId值不能为空")
		return
	}
	result, err := h.service.CreateUserRequest(c.Request.Context(), &req)
	if err != nil {
		if service.IsPasswordPolicyError(err) {
			Fail(c, err.Error())
			return
		}
		if result == nil {
			result = &dto.UserResponse{}
		}
		Write(c, CodeError, gin.H{"user": result}, "注册失败")
		return
	}
	Write(c, CodeSuccess, gin.H{"user": result}, "注册成功")
}

func (h *User) Update(c *gin.Context) {
	var req dto.UserRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if req.ID == 0 {
		Fail(c, "ID值不能为空")
		return
	}
	if err := h.service.UpdateUserRequest(c.Request.Context(), &req); err != nil {
		Fail(c, "修改失败")
		return
	}
	Write(c, CodeSuccess, gin.H{}, "设置成功")
}

func (h *User) UpdateSelf(c *gin.Context) {
	claims := Claims(c)
	if claims == nil {
		NoAuth(c, "未登录或非法访问")
		return
	}
	var req dto.SelfUserRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if err := h.service.UpdateSelfUserRequest(c.Request.Context(), claims.ID, &req); err != nil {
		Fail(c, "修改失败")
		return
	}
	Write(c, CodeSuccess, gin.H{}, "设置成功")
}

func (h *User) Delete(c *gin.Context) {
	var req dto.IDRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if req.ID == 0 {
		Fail(c, "ID值不能为空")
		return
	}
	claims := Claims(c)
	if claims != nil && claims.ID == req.ID {
		Fail(c, "删除失败, 无法删除自己。")
		return
	}
	if err := h.service.DeleteUser(c.Request.Context(), req.ID); err != nil {
		Fail(c, "删除失败")
		return
	}
	Write(c, CodeSuccess, gin.H{}, "删除成功")
}

func (h *User) ResetPassword(c *gin.Context) {
	var req dto.ResetPasswordRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if err := h.service.ResetPassword(c.Request.Context(), req.ID, req.Password); err != nil {
		if service.IsPasswordPolicyError(err) {
			Fail(c, err.Error())
			return
		}
		Fail(c, "重置失败"+err.Error())
		return
	}
	Write(c, CodeSuccess, gin.H{}, "重置成功")
}

func (h *User) ChangePassword(c *gin.Context) {
	claims := Claims(c)
	var req dto.ChangePasswordRequest
	if claims == nil {
		NoAuth(c, "未登录或非法访问")
		return
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if req.Password == "" {
		Fail(c, "Password值不能为空")
		return
	}
	if req.NewPassword == "" {
		Fail(c, "NewPassword值不能为空")
		return
	}
	if err := h.service.ChangePassword(c.Request.Context(), claims.ID, req.Password, req.NewPassword); err != nil {
		if service.IsPasswordPolicyError(err) {
			Fail(c, err.Error())
			return
		}
		Fail(c, "修改失败，原密码与当前账户不符")
		return
	}
	Write(c, CodeSuccess, gin.H{}, "修改成功")
}

func (h *User) SetSelfSetting(c *gin.Context) {
	claims := Claims(c)
	var setting map[string]any
	if claims == nil {
		NoAuth(c, "未登录或非法访问")
		return
	}
	if err := c.ShouldBindJSON(&setting); err != nil {
		Fail(c, err.Error())
		return
	}
	if err := h.service.SetUserSetting(c.Request.Context(), claims.ID, setting); err != nil {
		Fail(c, "设置失败")
		return
	}
	Write(c, CodeSuccess, gin.H{}, "设置成功")
}

func (h *User) SetAuthorities(c *gin.Context) {
	var req dto.UserAuthoritiesRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if err := h.service.SetUserAuthorities(c.Request.Context(), req.ID, req.AuthorityIDs); err != nil {
		Fail(c, "修改失败")
		return
	}
	Write(c, CodeSuccess, gin.H{}, "修改成功")
}

func (h *User) SwitchAuthority(c *gin.Context) {
	claims := Claims(c)
	var req dto.SwitchAuthorityRequest
	if claims == nil {
		NoAuth(c, "未登录或非法访问")
		return
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if req.AuthorityID == 0 {
		Fail(c, "AuthorityId值不能为空")
		return
	}
	login, err := h.auth.SwitchAuthority(c.Request.Context(), claims, req.AuthorityID)
	if err != nil {
		Fail(c, err.Error())
		return
	}
	c.Header("new-token", login.Token)
	c.Header("new-expires-at", strconv.FormatInt(login.ExpiresAt/1000, 10))
	maxAge := int(time.UnixMilli(login.ExpiresAt).Unix() - time.Now().Unix())
	SetTokenCookie(c, login.Token, maxAge)
	Write(c, CodeSuccess, gin.H{}, "修改成功")
}

func (h *User) Get(c *gin.Context) {
	claims := Claims(c)
	if claims == nil {
		NoAuth(c, "未登录或非法访问")
		return
	}
	result, err := h.service.UserByUUID(c.Request.Context(), claims.UUID)
	if err != nil {
		Fail(c, "获取失败")
		return
	}
	Write(c, CodeSuccess, gin.H{"userInfo": result}, "获取成功")
}
