package apis

import (
	"fmt"

	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	"github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
	_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"

	"go-admin/app/admin/models"
	"go-admin/app/admin/service"
	"go-admin/app/admin/service/dto"
	"go-admin/common/actions"
	statuscode "go-admin/common/status_code"
)

type LineUser struct {
	api.Api
}

// GetPage 获取会员表列表
// @Summary 获取会员表列表
// @Description 获取会员表列表
// @Tags 会员表
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]models.LineUser}} "{"code": 200, "data": [...]}"
// @Router /api/v1/line-user [get]
// @Security Bearer
func (e LineUser) GetPage(c *gin.Context) {
	req := dto.LineUserGetPageReq{}
	s := service.LineUser{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.Form, binding.Query).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	p := actions.GetPermissionFromContext(c)
	list := make([]models.LineUser, 0)
	var count int64

	err = s.GetPage(&req, p, &list, &count)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取会员表失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 获取会员表
// @Summary 获取会员表
// @Description 获取会员表
// @Tags 会员表
// @Param id path int false "id"
// @Success 200 {object} response.Response{data=models.LineUser} "{"code": 200, "data": [...]}"
// @Router /api/v1/line-user/{id} [get]
// @Security Bearer
func (e LineUser) Get(c *gin.Context) {
	req := dto.LineUserGetReq{}
	s := service.LineUser{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	var object models.LineUser

	p := actions.GetPermissionFromContext(c)
	err = s.Get(&req, p, &object)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取会员表失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(object, "查询成功")
}

// Insert 创建会员表
// @Summary 创建会员表
// @Description 创建会员表
// @Tags 会员表
// @Accept application/json
// @Product application/json
// @Param data body dto.LineUserInsertReq true "data"
// @Success 200 {object} response.Response	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/line-user [post]
// @Security Bearer
func (e LineUser) Insert(c *gin.Context) {
	req := dto.LineUserInsertReq{}
	s := service.LineUser{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	// 设置创建人
	req.SetCreateBy(user.GetUserId(c))

	err = s.Insert(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("创建会员表失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(req.GetId(), "创建成功")
}

// Update 修改会员表
// @Summary 修改会员表
// @Description 修改会员表
// @Tags 会员表
// @Accept application/json
// @Product application/json
// @Param id path int true "id"
// @Param data body dto.LineUserUpdateReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/line-user/{id} [put]
// @Security Bearer
func (e LineUser) Update(c *gin.Context) {
	req := dto.LineUserUpdateReq{}
	s := service.LineUser{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	req.SetUpdateBy(user.GetUserId(c))
	p := actions.GetPermissionFromContext(c)

	err = s.Update(&req, p)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("修改会员表失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "修改成功")
}

// Delete 删除会员表
// @Summary 删除会员表
// @Description 删除会员表
// @Tags 会员表
// @Param data body dto.LineUserDeleteReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/line-user [delete]
// @Security Bearer
func (e LineUser) Delete(c *gin.Context) {
	s := service.LineUser{}
	req := dto.LineUserDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	// req.SetUpdateBy(user.GetUserId(c))
	p := actions.GetPermissionFromContext(c)

	err = s.Remove(&req, p)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除会员表失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "删除成功")
}

// 重新获取资产
func (e LineUser) ReloadProperty(c *gin.Context) {
	s := service.LineUser{}
	req := dto.LineUserReloadPropertyReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	data := dto.LineUserPropertyResp{}
	code := s.GetProperty(req.UserId, &data)

	if code != statuscode.OK {
		if code == statuscode.UserApiUserNotBind {
			e.Error(500, nil, "用户未绑定api")
		} else {
			e.Error(500, nil, "获取失败")
		}

		return
	}

	e.OK(nil, "操作成功")
}
