package timeutil

import (
	"time"

	"google.golang.org/protobuf/types/known/timestamppb"
)

// UnixMilliToTime converts Unix milliseconds to time.Time
func UnixMilliToTime(ms int64) time.Time {
	return time.Unix(0, ms*int64(time.Millisecond))
}

// TimeToUnixMilli converts time.Time to Unix milliseconds
func TimeToUnixMilli(t time.Time) int64 {
	return t.UnixMilli()
}

// ProtoToTime converts a protobuf Timestamp to time.Time
// Returns zero time if timestamp is nil
func ProtoToTime(ts *timestamppb.Timestamp) time.Time {
	if ts == nil {
		return time.Time{}
	}
	return ts.AsTime()
}

// TimeToProto converts time.Time to protobuf Timestamp
// Returns nil for zero time
func TimeToProto(t time.Time) *timestamppb.Timestamp {
	if t.IsZero() {
		return nil
	}
	return timestamppb.New(t)
}

// NowProto returns current time as protobuf Timestamp
func NowProto() *timestamppb.Timestamp {
	return timestamppb.Now()
}
