// conditions.go presents current-conditions payloads in metric and US shapes.
// Layer: adapters/inbound/httpapi/presenter current-conditions payload mapping.
package presenter

import (
	"gitea.maximumdirect.net/ejr/weatherapi/internal/app"
	"gitea.maximumdirect.net/ejr/weatherfeeder/standards"
)

// CurrentConditionsResponse is the response shape for /conditions/current.
// Unit-bearing fields are populated according to the requested unit mode.
type CurrentConditionsResponse struct {
	TemperatureC            *float64 `json:"temperatureC,omitempty" xml:"temperatureC,omitempty"`
	ApparentTemperatureC    *float64 `json:"apparentTemperatureC,omitempty" xml:"apparentTemperatureC,omitempty"`
	DewpointC               *float64 `json:"dewpointC,omitempty" xml:"dewpointC,omitempty"`
	WindSpeedKmh            *float64 `json:"windSpeedKmh,omitempty" xml:"windSpeedKmh,omitempty"`
	TemperatureF            *float64 `json:"temperatureF,omitempty" xml:"temperatureF,omitempty"`
	ApparentTemperatureF    *float64 `json:"apparentTemperatureF,omitempty" xml:"apparentTemperatureF,omitempty"`
	DewpointF               *float64 `json:"dewpointF,omitempty" xml:"dewpointF,omitempty"`
	WindSpeedMph            *float64 `json:"windSpeedMph,omitempty" xml:"windSpeedMph,omitempty"`
	RelativeHumidityPercent *float64 `json:"relativeHumidityPercent,omitempty" xml:"relativeHumidityPercent,omitempty"`
	WindDirectionDegrees    *float64 `json:"windDirectionDegrees,omitempty" xml:"windDirectionDegrees,omitempty"`
	ConditionText           string   `json:"conditionText,omitempty" xml:"conditionText,omitempty"`
	IsDay                   *bool    `json:"isDay,omitempty" xml:"isDay,omitempty"`
	IsDayText               string   `json:"-" xml:"-"`
}

func CurrentConditionsPayload(conditions *app.CurrentConditions, units Units, precision int) any {
	if conditions == nil {
		return nil
	}

	out := CurrentConditionsResponse{
		RelativeHumidityPercent: roundedPtr(copyFloat64Ptr(conditions.RelativeHumidityPercent), precision),
		WindDirectionDegrees:    roundedPtr(copyFloat64Ptr(conditions.WindDirectionDegrees), precision),
		ConditionText:           standards.WMOText(conditions.ConditionCode, conditions.IsDay),
		IsDay:                   copyBoolPtr(conditions.IsDay),
		IsDayText:               boolText(conditions.IsDay),
	}

	if units == UnitsUS {
		out.TemperatureF = roundedPtr(celsiusToFahrenheitPtr(conditions.TemperatureC), precision)
		out.ApparentTemperatureF = roundedPtr(celsiusToFahrenheitPtr(conditions.ApparentTemperatureC), precision)
		out.DewpointF = roundedPtr(celsiusToFahrenheitPtr(conditions.DewpointC), precision)
		out.WindSpeedMph = roundedPtr(scalePtr(conditions.WindSpeedKmh, kmhToMphFactor), precision)
		return out
	}

	out.TemperatureC = roundedPtr(copyFloat64Ptr(conditions.TemperatureC), precision)
	out.ApparentTemperatureC = roundedPtr(copyFloat64Ptr(conditions.ApparentTemperatureC), precision)
	out.DewpointC = roundedPtr(copyFloat64Ptr(conditions.DewpointC), precision)
	out.WindSpeedKmh = roundedPtr(copyFloat64Ptr(conditions.WindSpeedKmh), precision)
	return out
}
