// discussion_mapper.go maps forecast discussion rows into weather model payloads.
// Layer: adapters/outbound/postgres discussion feature.
package postgres

import (
	"database/sql"
	"strings"

	"gitea.maximumdirect.net/ejr/weatherfeeder/model"
)

func mapDiscussionParentRow(row discussionParentRow) model.WeatherForecastDiscussion {
	return model.WeatherForecastDiscussion{
		OfficeID:    stringValue(row.OfficeID),
		OfficeName:  stringValue(row.OfficeName),
		Product:     model.ForecastDiscussionProduct(strings.TrimSpace(row.Product)),
		IssuedAt:    row.IssuedAt.UTC(),
		UpdatedAt:   timePtr(row.UpdatedAt),
		ShortTerm:   discussionSectionPtr(row.ShortTermQualifier, row.ShortTermIssuedAt, row.ShortTermText),
		LongTerm:    discussionSectionPtr(row.LongTermQualifier, row.LongTermIssuedAt, row.LongTermText),
		KeyMessages: nil,
	}
}

func mapDiscussionKeyMessageRow(row discussionKeyMessageRow) string {
	return stringValue(row.MessageText)
}

func discussionSectionPtr(
	qualifier sql.NullString,
	issuedAt sql.NullTime,
	text sql.NullString,
) *model.WeatherForecastDiscussionSection {
	q := stringValue(qualifier)
	t := stringValue(text)
	i := timePtr(issuedAt)
	if q == "" && t == "" && i == nil {
		return nil
	}
	return &model.WeatherForecastDiscussionSection{
		Qualifier: q,
		IssuedAt:  i,
		Text:      t,
	}
}
