package user

import (
	"fmt"
	"net/http"

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

const version = "/api/v1"
const group = "/user"

type Controller struct {
	client *gorm.DB
}

type PostLoginParam struct {
	account  string `binding:"required"`
	password string `binding:"required"`
}

func (c *Controller) PostLogin(ctx *gin.Context) {
	param := &PostLoginParam{}
	if err := ctx.ShouldBindJSON(&param); err != nil {
		fmt.Println("参数解析失败，请确认参数是否为 JSON")
		fmt.Println(err)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"message": "success",
	})
}

func (c *Controller) RegisterRoute(gin *gin.Engine) {
	group := gin.Group(version).Group(group)
	group.POST("/login", c.PostLogin)
}

// New 获取用户模块的控制器
//
// 参数
//   - gorm.DB 该模块所使用的数据库链接
//
// 返回值
//   - Controller 控制器的实例指针
func New(client *gorm.DB) *Controller {
	return &Controller{client}
}
