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 Company struct {
	CompanyBIZ *biz.Company
}

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

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

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

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

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

// @Tags 项目模块
// @Security ApiKeyAuth
// @Summary Delete company 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/companies/{id} [delete]
func (a *Company) Delete(c *gin.Context) {
	ctx := c.Request.Context()
	id, err := util.GetQueryID(c)
	if err != nil {
		util.ResError(c, err)
		return
	}
	err = a.CompanyBIZ.Delete(ctx, id)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}
