package vehicle

import (
	"fmt"
	"strconv"

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

// CreateVehicle 创建车辆信息
func CreateVehicle(c *gin.Context) {
	var req request.VehicleCreate
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err := vehicleService.CreateVehicle(req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

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

// UpdateVehicle 更新车辆信息
func UpdateVehicle(c *gin.Context) {
	var req request.VehicleUpdate
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

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

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

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

	vehicleInfo, err := vehicleService.GetVehicleByID(uintID)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithData(vehicleInfo, c)
}

// GetVehicleByPlateNumber 根据车牌号获取车辆信息
func GetVehicleByPlateNumber(c *gin.Context) {
	plateNumber := c.Query("plate_number")
	if plateNumber == "" {
		response.FailWithMessage("车牌号不能为空", c)
		return
	}

	vehicleInfo, err := vehicleService.GetVehicleByPlateNumber(plateNumber, "")
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithData(vehicleInfo, c)
}

func QueryAllVehicles(c *gin.Context) {
	vehicles, err := vehicleService.QueryAllVehicles()
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(vehicles, c)
}

// ListVehicles 获取车辆列表
func ListVehicles(c *gin.Context) {
	var req request.VehicleQuery
	if err := c.ShouldBindQuery(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

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

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

// DeleteVehicle 删除车辆
func DeleteVehicle(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 = vehicleService.DeleteVehicle(uintID)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

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

// VehicleEntry 车辆入场
func VehicleEntry(c *gin.Context) {
	var req request.VehicleEntry
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	resp, err := vehicleService.VehicleEntry(req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithData(resp, c)
}

// VehicleExit 车辆出场
func VehicleExit(c *gin.Context) {
	var req request.VehicleExit
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	resp, err := vehicleService.VehicleExit(req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithData(resp, c)
}

// GetInParkVehicles 获取所有在场车辆
func GetInParkVehicles(c *gin.Context) {
	lotID, _ := strconv.Atoi(c.Query("parking_lot_id"))
	plateNumber := c.Query("plate_number")
	list, err := vehicleService.GetInParkVehicles(uint(lotID), plateNumber)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(list, c)
}
