package telegram

import (
	"strconv"

	"github.com/tech/sendico/pkg/model"
)

type Update struct {
	UpdateID int64    `json:"update_id"`
	Message  *Message `json:"message,omitempty"`
}

type Message struct {
	MessageID      int64    `json:"message_id"`
	Date           int64    `json:"date,omitempty"`
	Chat           Chat     `json:"chat"`
	From           *User    `json:"from,omitempty"`
	Text           string   `json:"text,omitempty"`
	ReplyToMessage *Message `json:"reply_to_message,omitempty"`
}

type Chat struct {
	ID int64 `json:"id"`
}

type User struct {
	ID       int64  `json:"id"`
	Username string `json:"username,omitempty"`
}

func (m *Message) ToModel() *model.TelegramMessage {
	if m == nil {
		return nil
	}
	result := &model.TelegramMessage{
		ChatID:    strconv.FormatInt(m.Chat.ID, 10),
		MessageID: strconv.FormatInt(m.MessageID, 10),
		Text:      m.Text,
		SentAt:    m.Date,
	}
	if m.From != nil {
		result.FromUserID = strconv.FormatInt(m.From.ID, 10)
		result.FromUsername = m.From.Username
	}
	if m.ReplyToMessage != nil {
		result.ReplyToMessageID = strconv.FormatInt(m.ReplyToMessage.MessageID, 10)
	}
	return result
}
