package registry

import (
	"github.com/nspcc-dev/neo-go/pkg/io"
)

// ObjectInfo represents information about FrostFS object that has been created
// via gRPC/HTTP/S3 API.
type ObjectInfo struct {
	Id          uint64 // Identifier in bolt DB
	CreatedAt   int64  // UTC seconds from epoch when the object was created
	CID         string // Container ID in gRPC/HTTP
	OID         string // Object ID in gRPC/HTTP
	S3Bucket    string // Bucket name in S3
	S3Key       string // Object key in S3
	Status      string // Status of the object
	PayloadHash string // SHA256 hash of object payload that can be used for verification
}

func (o ObjectInfo) EncodeBinary(w *io.BinWriter) {
	o.encodeFilterableFields(w)
	w.WriteU64LE(o.Id)
	w.WriteString(o.CID)
	w.WriteString(o.OID)
	w.WriteString(o.S3Bucket)
	w.WriteString(o.S3Key)
	w.WriteString(o.PayloadHash)
}

func (o ObjectInfo) encodeFilterableFields(w *io.BinWriter) {
	w.WriteU64LE(uint64(o.CreatedAt))
	w.WriteString(o.Status)
}

func (o *ObjectInfo) DecodeBinary(r *io.BinReader) {
	o.decodeFilterableFields(r)
	o.Id = r.ReadU64LE()
	o.CID = r.ReadString()
	o.OID = r.ReadString()
	o.S3Bucket = r.ReadString()
	o.S3Key = r.ReadString()
	o.PayloadHash = r.ReadString()
}

func (o *ObjectInfo) decodeFilterableFields(r *io.BinReader) {
	o.CreatedAt = int64(r.ReadU64LE())
	o.Status = r.ReadString()
}

func (o ObjectInfo) Marshal() ([]byte, error) {
	w := io.NewBufBinWriter()
	o.EncodeBinary(w.BinWriter)
	err := w.Err // Bytes() sets Err to ErrDrained
	return w.Bytes(), err
}

func (o *ObjectInfo) Unmarshal(data []byte) error {
	r := io.NewBinReaderFromBuf(data)
	o.DecodeBinary(r)
	return r.Err
}