package app

import (
	"git.echol.cn/loser/st/server/global"
	"git.echol.cn/loser/st/server/model/app/request"
	"git.echol.cn/loser/st/server/model/common"
	commonResponse "git.echol.cn/loser/st/server/model/common/response"
	"git.echol.cn/loser/st/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type AuthApi struct{}

// Register
// @Tags     AppAuth
// @Summary  用户注册
// @Produce  application/json
// @Param    data  body      request.RegisterRequest                                true  "用户注册信息"
// @Success  200   {object}  commonResponse.Response{msg=string}                    "注册成功"
// @Router   /app/auth/register [post]
func (a *AuthApi) Register(c *gin.Context) {
	var req request.RegisterRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	err = service.ServiceGroupApp.AppServiceGroup.AuthService.Register(&req)
	if err != nil {
		global.GVA_LOG.Error("注册失败", zap.Error(err))
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	commonResponse.OkWithMessage("注册成功", c)
}

// Login
// @Tags     AppAuth
// @Summary  用户登录
// @Produce  application/json
// @Param    data  body      request.LoginRequest                                      true  "用户登录信息"
// @Success  200   {object}  commonResponse.Response{data=response.LoginResponse}      "登录成功"
// @Router   /app/auth/login [post]
func (a *AuthApi) Login(c *gin.Context) {
	var req request.LoginRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	ip := c.ClientIP()
	resp, err := service.ServiceGroupApp.AppServiceGroup.AuthService.Login(&req, ip)
	if err != nil {
		global.GVA_LOG.Error("登录失败", zap.Error(err))
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	commonResponse.OkWithData(resp, c)
}

// RefreshToken
// @Tags     AppAuth
// @Summary  刷新Token
// @Produce  application/json
// @Param    data  body      request.RefreshTokenRequest                               true  "刷新Token"
// @Success  200   {object}  commonResponse.Response{data=response.LoginResponse}      "刷新成功"
// @Router   /app/auth/refresh [post]
func (a *AuthApi) RefreshToken(c *gin.Context) {
	var req request.RefreshTokenRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	resp, err := service.ServiceGroupApp.AppServiceGroup.AuthService.RefreshToken(&req)
	if err != nil {
		global.GVA_LOG.Error("刷新Token失败", zap.Error(err))
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	commonResponse.OkWithData(resp, c)
}

// Logout
// @Tags     AppAuth
// @Summary  用户登出
// @Produce  application/json
// @Success  200   {object}  commonResponse.Response{msg=string}  "登出成功"
// @Router   /app/auth/logout [post]
// @Security ApiKeyAuth
func (a *AuthApi) Logout(c *gin.Context) {
	userID := common.GetAppUserID(c)
	token := c.GetHeader("Authorization")
	if len(token) > 7 {
		token = token[7:] // 移除 "Bearer " 前缀
	}

	err := service.ServiceGroupApp.AppServiceGroup.AuthService.Logout(userID, token)
	if err != nil {
		global.GVA_LOG.Error("登出失败", zap.Error(err))
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	commonResponse.OkWithMessage("登出成功", c)
}

// GetUserInfo
// @Tags     AppAuth
// @Summary  获取用户信息
// @Produce  application/json
// @Success  200   {object}  commonResponse.Response{data=response.AppUserResponse}  "获取成功"
// @Router   /app/auth/userinfo [get]
// @Security ApiKeyAuth
func (a *AuthApi) GetUserInfo(c *gin.Context) {
	userID := common.GetAppUserID(c)

	resp, err := service.ServiceGroupApp.AppServiceGroup.AuthService.GetUserInfo(userID)
	if err != nil {
		global.GVA_LOG.Error("获取用户信息失败", zap.Error(err))
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	commonResponse.OkWithData(resp, c)
}

// UpdateProfile
// @Tags     AppAuth
// @Summary  更新用户信息
// @Produce  application/json
// @Param    data  body      request.UpdateProfileRequest                 true  "用户信息"
// @Success  200   {object}  commonResponse.Response{msg=string}          "更新成功"
// @Router   /app/user/profile [put]
// @Security ApiKeyAuth
func (a *AuthApi) UpdateProfile(c *gin.Context) {
	userID := common.GetAppUserID(c)

	var req request.UpdateProfileRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	err = service.ServiceGroupApp.AppServiceGroup.AuthService.UpdateProfile(userID, &req)
	if err != nil {
		global.GVA_LOG.Error("更新用户信息失败", zap.Error(err))
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	commonResponse.OkWithMessage("更新成功", c)
}

// ChangePassword
// @Tags     AppAuth
// @Summary  修改密码
// @Produce  application/json
// @Param    data  body      request.ChangePasswordRequest                true  "密码信息"
// @Success  200   {object}  commonResponse.Response{msg=string}          "修改成功"
// @Router   /app/user/change-password [post]
// @Security ApiKeyAuth
func (a *AuthApi) ChangePassword(c *gin.Context) {
	userID := common.GetAppUserID(c)

	var req request.ChangePasswordRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	err = service.ServiceGroupApp.AppServiceGroup.AuthService.ChangePassword(userID, &req)
	if err != nil {
		global.GVA_LOG.Error("修改密码失败", zap.Error(err))
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	commonResponse.OkWithMessage("修改成功", c)
}
