package api

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

type House struct {
	HouseBIZ *biz.House
}

// @Tags 房源模块
// @Security ApiKeyAuth
// @Summary Query house 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.House}
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/houses [get]
func (a *House) Query(c *gin.Context) {
	ctx := c.Request.Context()
	var params schema.HouseQueryParam
	if err := util.ParseQuery(c, &params); err != nil {
		util.ResError(c, err)
		return
	}

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

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

// @Tags 房源模块
// @Security ApiKeyAuth
// @Summary Create house record
// @Param body body schema.HouseForm true "Request body"
// @Success 200 {object} util.ResponseResult{data=schema.House}
// @Failure 400 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/houses [post]
func (a *House) Create(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(schema.HouseForm)
	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.HouseBIZ.Create(ctx, item)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResSuccess(c, result)
}

// @Tags 房源模块
// @Security ApiKeyAuth
// @Summary Update house record by ID
// @Param id path string true "unique id"
// @Param body body schema.HouseForm 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/houses/{id} [put]
func (a *House) Update(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(schema.HouseForm)
	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.HouseBIZ.Update(ctx, id, item)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}

// @Tags 房源模块
// @Security ApiKeyAuth
// @Summary Delete house 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/houses/{id} [delete]
func (a *House) Delete(c *gin.Context) {
	ctx := c.Request.Context()
	id, err := util.GetQueryID(c)
	if err != nil {
		util.ResError(c, err)
		return
	}
	err = a.HouseBIZ.Delete(ctx, id)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}
