package sharedmodel

import (
	"encoding/json"
	"time"

	"gorm.io/gorm"
)

type Subscription struct {
	ID                         string `gorm:"primarykey"`
	CreatedAt                  time.Time
	UpdatedAt                  time.Time
	DeletedAt                  gorm.DeletedAt `gorm:"index"`
	SubjectExternalId          string
	SubjectExternalIdSystem    string
	SubjectDisplayName         string
	SubjectGiven               ListOfStrings `gorm:"type:text"`
	SubjectOwnName             string
	SubjectOwnNamePrefix       string
	SubjectPartnerName         string
	SubjectPartnerNamePrefix   string
	SubjectBirthdate           string
	SubjectAddressStreet       string
	SubjectAddressStreetNumber string
	SubjectAddressPostalCode   string
	SubjectAddressCity         string
	SubjectAddressCountry      string
	ProtocolMeta               string
	ServiceConfigID            uint
	ServiceConfig              *ServiceConfig
}

func (s Subscription) GetProtocolMeta(meta interface{}) error {
	return json.Unmarshal([]byte(s.ProtocolMeta), meta)
}

func (s *Subscription) SetProtocolMeta(meta interface{}) error {
	b, err := json.Marshal(meta)

	if err != nil {
		return err
	}

	s.ProtocolMeta = string(b)
	return nil
}
