package parking

import (
	"fmt"
	"wails-app/internal/model/common/response"
	"wails-app/internal/model/parking/request"
	parkingResponse "wails-app/internal/model/parking/response"

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

func CreateBooth(c *gin.Context) {
	var req request.BoothCreate
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err := BoothService.CreateBooth(req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithMessage("创建成功", c)
}

func UpdateBooth(c *gin.Context) {
	var req request.BoothUpdate
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err := BoothService.UpdateBooth(req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithMessage("更新成功", c)
}

func GetBoothByID(c *gin.Context) {
	id := c.Query("id")
	uintID := uint(0)
	_, err := fmt.Sscanf(id, "%d", &uintID)
	if err != nil {
		response.FailWithMessage("无效的ID", c)
		return
	}

	booth, err := BoothService.GetBoothByID(uintID)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithData(booth, c)
}

func GetBoothByCode(c *gin.Context) {
	code := c.Query("code")
	if code == "" {
		response.FailWithMessage("岗亭编码不能为空", c)
		return
	}

	booth, err := BoothService.GetBoothByCode(code)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithData(booth, c)
}

func QueryAllBooths(c *gin.Context) {
	booths, err := BoothService.QueryAllBooths()
	if err != nil {
		response.FailWithMessage("THE QUERY FAILED", c)
	}
	response.OkWithData(booths, c)
}

func ListBooths(c *gin.Context) {
	var req request.BoothQuery
	if err := c.ShouldBindQuery(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	total, list, err := BoothService.ListBooths(req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithData(parkingResponse.BoothList{
		Total: total,
		List:  list,
	}, c)
}

func DeleteBooth(c *gin.Context) {
	id := c.Query("id")
	uintID := uint(0)
	_, err := fmt.Sscanf(id, "%d", &uintID)
	if err != nil {
		response.FailWithMessage("无效的ID", c)
		return
	}

	err = BoothService.DeleteBooth(uintID)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithMessage("删除成功", c)
}
