package utils

import (
	"time"

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

func ToTimestamp(t interface{}) *timestamppb.Timestamp {
	if t == nil {
		return nil
	}

	var tm time.Time
	switch v := t.(type) {
	case *time.Time:
		if v == nil {
			return nil
		}
		tm = *v
	case time.Time:
		tm = v
	default:
		return nil
	}

	if tm.IsZero() {
		return nil
	}

	return timestamppb.New(tm)
}

func FromTimestamp(t interface{}) *time.Time {
	var tm time.Time
	switch v := t.(type) {
	case timestamppb.Timestamp:
		tm = v.AsTime()
	case *timestamppb.Timestamp:
		if v == nil {
			return nil
		}
		tm = v.AsTime()
	default:
		return nil
	}

	return &tm
}
