package api

import (
	"github.com/gin-gonic/gin"
	"github.guxuan/haibei/internal/mods/common/biz"
	"github.guxuan/haibei/internal/mods/common/schema"
	"github.guxuan/haibei/pkg/util"
)

// Defining the `Company` api.
type ReceptionCenter struct {
	ReceptionCenterBIZ *biz.ReceptionCenter
}

// @Tags 楼盘模块
// @Security ApiKeyAuth
// @Summary Query ReceptionCenter list
// @Param current query int true "pagination index" default(1)
// @Param pageSize query int true "pagination size" default(10)
// @Success 200 {object} util.ResponseResult{data=[]schema.ReceptionCenter}
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/reception_center [get]
func (a *ReceptionCenter) Query(c *gin.Context) {
	ctx := c.Request.Context()
	var params schema.ReceptionCenterQueryParam
	if err := util.ParseQuery(c, &params); err != nil {
		util.ResError(c, err)
		return
	}

	result, err := a.ReceptionCenterBIZ.Query(ctx, params)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResPage(c, result.Data, result.PageResult)
}

// @Tags 楼盘模块
// @Security ApiKeyAuth
// @Summary Get ReceptionCenter record by ID
// @Param id path string true "unique id"
// @Success 200 {object} util.ResponseResult{data=schema.ReceptionCenter}
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/reception_center/{id} [get]
func (a *ReceptionCenter) Get(c *gin.Context) {
	ctx := c.Request.Context()
	id, err := util.GetQueryID(c)
	if err != nil {
		util.ResError(c, err)
		return
	}
	item, err := a.ReceptionCenterBIZ.Get(ctx, id)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResSuccess(c, item)
}

// @Tags 楼盘模块
// @Security ApiKeyAuth
// @Summary Create ReceptionCenter record
// @Param body body schema.ReceptionCenterForm true "Request body"
// @Success 200 {object} util.ResponseResult{data=schema.ReceptionCenter}
// @Failure 400 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/reception_center [post]
func (a *ReceptionCenter) Create(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(schema.ReceptionCenterForm)
	if err := util.ParseJSON(c, item); err != nil {
		util.ResError(c, err)
		return
	} else if err := item.Validate(); err != nil {
		util.ResError(c, err)
		return
	}

	result, err := a.ReceptionCenterBIZ.Create(ctx, item)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResSuccess(c, result)
}

// @Tags 楼盘模块
// @Security ApiKeyAuth
// @Summary Update ReceptionCenter record by ID
// @Param id path string true "unique id"
// @Param body body schema.ReceptionCenterForm true "Request body"
// @Success 200 {object} util.ResponseResult
// @Failure 400 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/reception_center/{id} [put]
func (a *ReceptionCenter) Update(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(schema.ReceptionCenterForm)
	if err := util.ParseJSON(c, item); err != nil {
		util.ResError(c, err)
		return
	} else if err := item.Validate(); err != nil {
		util.ResError(c, err)
		return
	}
	id, err := util.GetQueryID(c)
	if err != nil {
		util.ResError(c, err)
		return
	}
	err = a.ReceptionCenterBIZ.Update(ctx, id, item)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}

// @Tags 楼盘模块
// @Security ApiKeyAuth
// @Summary Delete ReceptionCenter record by ID
// @Param id path string true "unique id"
// @Success 200 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/reception_center/{id} [delete]
func (a *ReceptionCenter) Delete(c *gin.Context) {
	ctx := c.Request.Context()
	id, err := util.GetQueryID(c)
	if err != nil {
		util.ResError(c, err)
		return
	}
	err = a.ReceptionCenterBIZ.Delete(ctx, id)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}

// @Tags 楼盘模块
// @Security ApiKeyAuth
// @Summary Get ReceptionCenter QrCode by ID
// @Param id path string true "unique id"
// @Success 200 {object} util.ResponseResult{data=[]byte}
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/reception_center/qr_code/{id} [get]
func (a *ReceptionCenter) GetQrCode(c *gin.Context) {
	ctx := c.Request.Context()
	id, err := util.GetQueryID(c)
	if err != nil {
		util.ResError(c, err)
		return
	}
	qrcode, err := a.ReceptionCenterBIZ.GetQrCode(ctx, id)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResSuccess(c, qrcode)
}

// @Tags 楼盘模块
// @Security ApiKeyAuth
// @Summary check QrCode
// @Param body body CheckQrCodeStruct true "Request body"
// @Success 200 {object} util.ResponseResult
// @Failure 400 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/reception_center [post]
func (a *ReceptionCenter) CheckQrCode(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(CheckQrCodeStruct)
	if err := util.ParseJSON(c, item); err != nil {
		util.ResError(c, err)
		return
	}

	err := a.ReceptionCenterBIZ.CheckQrcode(ctx, item.CustomerID, item.ReceptionCenterID)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}

type CheckQrCodeStruct struct {
	CustomerID        uint `json:"customer_id"`
	ReceptionCenterID uint `json:"reception_center_id"`
}
