package handlers

import (
	"fmt"
	"strconv"

	"github.com/gin-gonic/gin"
	"github.com/niangaodev/art-code/models"
	"github.com/niangaodev/art-code/repositories"
	"github.com/niangaodev/art-code/utils"
)

// AdminGetUsers 获取用户列表
func AdminGetUsers(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))

	keyword := c.Query("keyword")
	if keyword == "" {
		keyword = c.Query("q")
	}
	role := c.Query("role")
	isActive := -1
	if s := c.Query("isActive"); s != "" {
		if v, err := strconv.Atoi(s); err == nil {
			isActive = v
		}
	}

	users, total, err := repositories.GetUsers(page, pageSize, keyword, role, isActive)
	if err != nil {
		utils.ServerError(c, err)
		return
	}

	list := repositories.BuildUsersResponse(users)
	res := gin.H{
		"list":  list,
		"total": total,
		"page":  page,
		"size":  pageSize,
	}
	if list == nil {
		res["list"] = []interface{}{}
	}

	utils.Success(c, res)
}

// AdminCreateUser 创建用户
func AdminCreateUser(c *gin.Context) {
	var user models.User
	if err := c.ShouldBindJSON(&user); err != nil {
		utils.Error(c, 400, "Invalid request")
		return
	}

	// 密码加密
	hashedPassword, err := utils.HashPassword(user.Password)
	if err != nil {
		utils.ServerError(c, err)
		return
	}
	user.Password = hashedPassword

	if err := repositories.CreateUser(&user); err != nil {
		utils.ServerError(c, err)
		return
	}

	utils.SuccessWithMsg(c, "User created successfully", gin.H{"id": user.ID})
}

// AdminUpdateUser 更新用户
func AdminUpdateUser(c *gin.Context) {
	idStr := c.Param("id")
	var id uint
	if _, err := fmt.Sscanf(idStr, "%d", &id); err != nil {
		utils.Error(c, 400, "Invalid user ID")
		return
	}

	var user models.User
	if err := c.ShouldBindJSON(&user); err != nil {
		utils.Error(c, 400, "Invalid request")
		return
	}
	user.ID = id

	// 如果提供了密码，则加密
	if user.Password != "" {
		hashedPassword, err := utils.HashPassword(user.Password)
		if err != nil {
			utils.ServerError(c, err)
			return
		}
		user.Password = hashedPassword
	}

	if err := repositories.UpdateUser(&user); err != nil {
		utils.ServerError(c, err)
		return
	}

	utils.SuccessWithMsg(c, "User updated successfully", nil)
}

// AdminDeleteUser 删除用户
func AdminDeleteUser(c *gin.Context) {
	idStr := c.Param("id")
	var id uint
	if _, err := fmt.Sscanf(idStr, "%d", &id); err != nil {
		utils.Error(c, 400, "Invalid user ID")
		return
	}

	if err := repositories.DeleteUser(id); err != nil {
		utils.ServerError(c, err)
		return
	}

	utils.SuccessWithMsg(c, "User deleted successfully", nil)
}

// AdminGetUser 获取单个用户
func AdminGetUser(c *gin.Context) {
	idStr := c.Param("id")
	var id uint
	if _, err := fmt.Sscanf(idStr, "%d", &id); err != nil {
		utils.Error(c, 400, "Invalid user ID")
		return
	}

	user, err := repositories.GetUserByID(id)
	if err != nil {
		utils.ServerError(c, err)
		return
	}
	if user == nil {
		utils.Error(c, 404, "User not found")
		return
	}

	utils.Success(c, repositories.BuildUserResponse(user))
}
