// discussion.go presents forecast discussion payloads.
// Layer: adapters/inbound/httpapi/presenter discussion payload mapping.
package presenter

import (
	"time"

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

type DiscussionKeyMessagesPayload struct {
	OfficeID    string                          `json:"officeId,omitempty" xml:"officeId,omitempty"`
	OfficeName  string                          `json:"officeName,omitempty" xml:"officeName,omitempty"`
	Product     model.ForecastDiscussionProduct `json:"product" xml:"product"`
	IssuedAt    time.Time                       `json:"issuedAt" xml:"issuedAt"`
	UpdatedAt   *time.Time                      `json:"updatedAt,omitempty" xml:"updatedAt,omitempty"`
	KeyMessages []string                        `json:"keyMessages" xml:"keyMessages"`
}

type DiscussionShortTermPayload struct {
	OfficeID   string                                  `json:"officeId,omitempty" xml:"officeId,omitempty"`
	OfficeName string                                  `json:"officeName,omitempty" xml:"officeName,omitempty"`
	Product    model.ForecastDiscussionProduct         `json:"product" xml:"product"`
	IssuedAt   time.Time                               `json:"issuedAt" xml:"issuedAt"`
	UpdatedAt  *time.Time                              `json:"updatedAt,omitempty" xml:"updatedAt,omitempty"`
	ShortTerm  *model.WeatherForecastDiscussionSection `json:"shortTerm,omitempty" xml:"shortTerm,omitempty"`
}

type DiscussionLongTermPayload struct {
	OfficeID   string                                  `json:"officeId,omitempty" xml:"officeId,omitempty"`
	OfficeName string                                  `json:"officeName,omitempty" xml:"officeName,omitempty"`
	Product    model.ForecastDiscussionProduct         `json:"product" xml:"product"`
	IssuedAt   time.Time                               `json:"issuedAt" xml:"issuedAt"`
	UpdatedAt  *time.Time                              `json:"updatedAt,omitempty" xml:"updatedAt,omitempty"`
	LongTerm   *model.WeatherForecastDiscussionSection `json:"longTerm,omitempty" xml:"longTerm,omitempty"`
}

func DiscussionPayload(run *model.WeatherForecastDiscussion, _ Units, tz *time.Location) any {
	if run == nil {
		return nil
	}

	out := model.WeatherForecastDiscussion{
		OfficeID:    run.OfficeID,
		OfficeName:  run.OfficeName,
		Product:     run.Product,
		IssuedAt:    inLocationTime(run.IssuedAt, tz),
		UpdatedAt:   inLocationTimePtr(run.UpdatedAt, tz),
		KeyMessages: append([]string(nil), run.KeyMessages...),
		ShortTerm:   copyDiscussionSection(run.ShortTerm, tz),
		LongTerm:    copyDiscussionSection(run.LongTerm, tz),
	}
	return &out
}

func DiscussionKeyMessagesOnlyPayload(run *model.WeatherForecastDiscussion, _ Units, tz *time.Location) any {
	if run == nil {
		return nil
	}

	return DiscussionKeyMessagesPayload{
		OfficeID:    run.OfficeID,
		OfficeName:  run.OfficeName,
		Product:     run.Product,
		IssuedAt:    inLocationTime(run.IssuedAt, tz),
		UpdatedAt:   inLocationTimePtr(run.UpdatedAt, tz),
		KeyMessages: append([]string{}, run.KeyMessages...),
	}
}

func DiscussionShortTermOnlyPayload(run *model.WeatherForecastDiscussion, _ Units, tz *time.Location) any {
	if run == nil {
		return nil
	}

	return DiscussionShortTermPayload{
		OfficeID:   run.OfficeID,
		OfficeName: run.OfficeName,
		Product:    run.Product,
		IssuedAt:   inLocationTime(run.IssuedAt, tz),
		UpdatedAt:  inLocationTimePtr(run.UpdatedAt, tz),
		ShortTerm:  copyDiscussionSection(run.ShortTerm, tz),
	}
}

func DiscussionLongTermOnlyPayload(run *model.WeatherForecastDiscussion, _ Units, tz *time.Location) any {
	if run == nil {
		return nil
	}

	return DiscussionLongTermPayload{
		OfficeID:   run.OfficeID,
		OfficeName: run.OfficeName,
		Product:    run.Product,
		IssuedAt:   inLocationTime(run.IssuedAt, tz),
		UpdatedAt:  inLocationTimePtr(run.UpdatedAt, tz),
		LongTerm:   copyDiscussionSection(run.LongTerm, tz),
	}
}

func copyDiscussionSection(in *model.WeatherForecastDiscussionSection, tz *time.Location) *model.WeatherForecastDiscussionSection {
	if in == nil {
		return nil
	}
	return &model.WeatherForecastDiscussionSection{
		Qualifier: in.Qualifier,
		IssuedAt:  inLocationTimePtr(in.IssuedAt, tz),
		Text:      in.Text,
	}
}
