package vehicle

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"wails-app/internal/model/common/response"
	"wails-app/internal/model/vehicle/request"
	vehicleResponse "wails-app/internal/model/vehicle/response"
)

// CreateFeeConfig 创建收费配置
func CreateFeeConfig(c *gin.Context) {
	var req request.FeeConfigCreate
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

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

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

// UpdateFeeConfig 更新收费配置
func UpdateFeeConfig(c *gin.Context) {
	var req request.FeeConfigUpdate
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

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

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

// GetFeeConfigByID 根据ID获取收费配置
func GetFeeConfigByID(c *gin.Context) {
	id := c.Query("id")
	uintID := uint(0)
	_, err := fmt.Sscanf(id, "%d", &uintID)
	if err != nil {
		response.FailWithMessage("无效的ID", c)
		return
	}

	config, err := feeConfigService.GetFeeConfigByID(uintID)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithData(config, c)
}

// ListFeeConfigs 获取收费配置列表
func ListFeeConfigs(c *gin.Context) {
	var req request.FeeConfigQuery
	if err := c.ShouldBindQuery(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

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

	response.OkWithData(vehicleResponse.FeeConfigList{
		Total: total,
		List:  list,
	}, c)
}

// DeleteFeeConfig 删除收费配置
func DeleteFeeConfig(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 = feeConfigService.DeleteFeeConfig(uintID)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

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