package vehicle

import (
	"fmt"
	"wails-app/internal/model/common/response"
	"wails-app/internal/model/vehicle/request"
	vehicleResponse "wails-app/internal/model/vehicle/response"

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

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

	record, err := vehicleRecordService.GetVehicleRecordByID(uintID)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithData(record, c)
}

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

	page := 1
	pageSize := 10
	if p := c.Query("page"); p != "" {
		fmt.Sscanf(p, "%d", &page)
	}
	if ps := c.Query("page_size"); ps != "" {
		fmt.Sscanf(ps, "%d", &pageSize)
	}

	total, list, err := vehicleRecordService.ListVehicleRecordsByPlateNumber(plateNumber, page, pageSize)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

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

// ListVehicleRecords 获取所有车辆记录列表
func ListVehicleRecords(c *gin.Context) {
	var info request.VehicleRecordRequest

	err := c.ShouldBindJSON(&info)

	list, total, err := vehicleRecordService.ListVehicleRecords(info)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     info.Page,
		PageSize: info.PageSize,
	}, "获取成功", c)
}
