package webserver

import (
	"errors"

	"git.ctdo.de/henne/blitzer-v2/db"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	"gorm.io/gorm"
)

func HandleTicket(ctx *gin.Context) {
	var ticket db.SpeedingTicket
	if err := db.DB.Where("id = ?", ctx.Param("id")).First(&ticket).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			ctx.String(404, "not found")
			return
		}
		logrus.Error(err)
		ctx.String(500, "internal server error")
		return
	}

	err := templates.ExecuteTemplate(ctx.Writer, "ticket", gin.H{"Ticket": ticket, "Exceeded": ticket.Speed - ticket.AllowedSpeed})
	if err != nil {
		logrus.Error(err)
	}
}
