package templates

import (
	. "primesys_email/src/models/report"
	. "primesys_email/src/templates/utils"
)

func CreateTRDPatrolmanReportExcel(reports []DivisionTripReportModel, divisionName string, reportDate string) ([]byte, error) {
	t := tableReport{
		sheet:   "TRD Patrolman Report",
		title:   "TRD PATROLMAN REPORT",
		widths:  []float64{10, 28, 24, 20, 20, 16, 14, 26},
		headers: []string{"Sr.No", "Name", "Actual Beat", "Expected Time", "OnTrack Time", "Actual KM", "Overspeed", "Start% - End% Battery"},
	}

	return writeTableReport(t, reports, divisionName, reportDate, func(r DivisionTripReportModel, s tableStyles) []tableCell {
		return []tableCell{
			{DefaultString(r.DeviceName), s.name},
			{kmRange(r.TripActualStartKm, r.TripActualEndKm, 2), s.cell},
			{timeRange(r.TripStartTime, r.TripEndTime), s.cell},
			{timeRange(r.ActualStartTime, r.ActualEndTime), s.cell},
			{DefaultFloat(r.DistanceCoverTrip), s.cell},
			yesNoCell(overspeed(r), s),
			{batteryText(r.TripList, "- - -"), s.cell},
		}
	})
}

// overspeed is "Yes" above 8, "No" otherwise and "-" when there is no speed or trip
func overspeed(r DivisionTripReportModel) string {
	if r.TripMaxSpeed == nil || len(r.TripList) == 0 {
		return "-"
	}
	if *r.TripMaxSpeed > 8 {
		return "Yes"
	}
	return "No"
}
