// Code generated by go-swagger; DO NOT EDIT.

// (c) 2012-2020 by Taxnexus, Inc.
// All rights reserved worldwide.
// Proprietary product; unlicensed use is not allowed

package members_models

import (
	"context"
	"encoding/json"

	"github.com/go-openapi/errors"
	"github.com/go-openapi/strfmt"
	"github.com/go-openapi/swag/jsonutils"
	"github.com/go-openapi/swag/typeutils"
	"github.com/go-openapi/validate"
)

// Subscription subscription
//
// swagger:model Subscription
type Subscription struct {
	AuditFields

	// cancel at period end
	CancelAtPeriodEnd *bool `json:"cancelAtPeriodEnd,omitempty"`

	// current period end
	// Format: date-time
	CurrentPeriodEnd *strfmt.DateTime `json:"currentPeriodEnd,omitempty"`

	// current period start
	// Format: date-time
	CurrentPeriodStart *strfmt.DateTime `json:"currentPeriodStart,omitempty"`

	// ends at
	// Format: date-time
	EndsAt *strfmt.DateTime `json:"endsAt,omitempty"`

	// Non-secret provider reference; blank values are normalized to null.
	// Max Length: 255
	// Min Length: 1
	ExternalCustomerID *string `json:"externalCustomerId,omitempty"`

	// Non-secret provider reference; blank values are normalized to null.
	// Max Length: 255
	// Min Length: 1
	ExternalSubscriptionID *string `json:"externalSubscriptionId,omitempty"`

	// payment method Id
	// Format: uuid
	PaymentMethodID *strfmt.UUID `json:"paymentMethodId,omitempty"`

	// plan Id
	// Required: true
	// Format: uuid
	PlanID *strfmt.UUID `json:"planId"`

	// Non-secret provider name; blank values are normalized to null.
	// Max Length: 80
	// Min Length: 1
	Provider *string `json:"provider,omitempty"`

	// starts at
	// Required: true
	// Format: date-time
	StartsAt *strfmt.DateTime `json:"startsAt"`

	// status
	// Required: true
	// Enum: ["trialing","active","past_due","paused","canceled","expired"]
	Status *string `json:"status"`

	// tenant Id
	// Required: true
	// Format: uuid
	TenantID *strfmt.UUID `json:"tenantId"`
}

// UnmarshalJSON unmarshals this object from a JSON structure
func (m *Subscription) UnmarshalJSON(raw []byte) error {
	// AO0
	var aO0 AuditFields
	if err := jsonutils.ReadJSON(raw, &aO0); err != nil {
		return err
	}
	m.AuditFields = aO0

	// AO1
	var dataAO1 struct {
		CancelAtPeriodEnd *bool `json:"cancelAtPeriodEnd,omitempty"`

		CurrentPeriodEnd *strfmt.DateTime `json:"currentPeriodEnd,omitempty"`

		CurrentPeriodStart *strfmt.DateTime `json:"currentPeriodStart,omitempty"`

		EndsAt *strfmt.DateTime `json:"endsAt,omitempty"`

		ExternalCustomerID *string `json:"externalCustomerId,omitempty"`

		ExternalSubscriptionID *string `json:"externalSubscriptionId,omitempty"`

		PaymentMethodID *strfmt.UUID `json:"paymentMethodId,omitempty"`

		PlanID *strfmt.UUID `json:"planId"`

		Provider *string `json:"provider,omitempty"`

		StartsAt *strfmt.DateTime `json:"startsAt"`

		Status *string `json:"status"`

		TenantID *strfmt.UUID `json:"tenantId"`
	}
	if err := jsonutils.ReadJSON(raw, &dataAO1); err != nil {
		return err
	}

	m.CancelAtPeriodEnd = dataAO1.CancelAtPeriodEnd

	m.CurrentPeriodEnd = dataAO1.CurrentPeriodEnd

	m.CurrentPeriodStart = dataAO1.CurrentPeriodStart

	m.EndsAt = dataAO1.EndsAt

	m.ExternalCustomerID = dataAO1.ExternalCustomerID

	m.ExternalSubscriptionID = dataAO1.ExternalSubscriptionID

	m.PaymentMethodID = dataAO1.PaymentMethodID

	m.PlanID = dataAO1.PlanID

	m.Provider = dataAO1.Provider

	m.StartsAt = dataAO1.StartsAt

	m.Status = dataAO1.Status

	m.TenantID = dataAO1.TenantID

	return nil
}

// MarshalJSON marshals this object to a JSON structure
func (m Subscription) MarshalJSON() ([]byte, error) {
	_parts := make([][]byte, 0, 2)

	aO0, err := jsonutils.WriteJSON(m.AuditFields)
	if err != nil {
		return nil, err
	}
	_parts = append(_parts, aO0)
	var dataAO1 struct {
		CancelAtPeriodEnd *bool `json:"cancelAtPeriodEnd,omitempty"`

		CurrentPeriodEnd *strfmt.DateTime `json:"currentPeriodEnd,omitempty"`

		CurrentPeriodStart *strfmt.DateTime `json:"currentPeriodStart,omitempty"`

		EndsAt *strfmt.DateTime `json:"endsAt,omitempty"`

		ExternalCustomerID *string `json:"externalCustomerId,omitempty"`

		ExternalSubscriptionID *string `json:"externalSubscriptionId,omitempty"`

		PaymentMethodID *strfmt.UUID `json:"paymentMethodId,omitempty"`

		PlanID *strfmt.UUID `json:"planId"`

		Provider *string `json:"provider,omitempty"`

		StartsAt *strfmt.DateTime `json:"startsAt"`

		Status *string `json:"status"`

		TenantID *strfmt.UUID `json:"tenantId"`
	}

	dataAO1.CancelAtPeriodEnd = m.CancelAtPeriodEnd

	dataAO1.CurrentPeriodEnd = m.CurrentPeriodEnd

	dataAO1.CurrentPeriodStart = m.CurrentPeriodStart

	dataAO1.EndsAt = m.EndsAt

	dataAO1.ExternalCustomerID = m.ExternalCustomerID

	dataAO1.ExternalSubscriptionID = m.ExternalSubscriptionID

	dataAO1.PaymentMethodID = m.PaymentMethodID

	dataAO1.PlanID = m.PlanID

	dataAO1.Provider = m.Provider

	dataAO1.StartsAt = m.StartsAt

	dataAO1.Status = m.Status

	dataAO1.TenantID = m.TenantID

	jsonDataAO1, errAO1 := jsonutils.WriteJSON(dataAO1)
	if errAO1 != nil {
		return nil, errAO1
	}
	_parts = append(_parts, jsonDataAO1)
	return jsonutils.ConcatJSON(_parts...), nil
}

// Validate validates this subscription
func (m *Subscription) Validate(formats strfmt.Registry) error {
	var res []error

	// validation for a type composition with AuditFields
	if err := m.AuditFields.Validate(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateCurrentPeriodEnd(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateCurrentPeriodStart(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateEndsAt(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateExternalCustomerID(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateExternalSubscriptionID(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validatePaymentMethodID(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validatePlanID(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateProvider(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateStartsAt(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateStatus(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateTenantID(formats); err != nil {
		res = append(res, err)
	}

	if len(res) > 0 {
		return errors.CompositeValidationError(res...)
	}
	return nil
}

func (m *Subscription) validateCurrentPeriodEnd(formats strfmt.Registry) error {

	if typeutils.IsZero(m.CurrentPeriodEnd) { // not required
		return nil
	}

	if err := validate.FormatOf("currentPeriodEnd", "body", "date-time", m.CurrentPeriodEnd.String(), formats); err != nil {
		return err
	}

	return nil
}

func (m *Subscription) validateCurrentPeriodStart(formats strfmt.Registry) error {

	if typeutils.IsZero(m.CurrentPeriodStart) { // not required
		return nil
	}

	if err := validate.FormatOf("currentPeriodStart", "body", "date-time", m.CurrentPeriodStart.String(), formats); err != nil {
		return err
	}

	return nil
}

func (m *Subscription) validateEndsAt(formats strfmt.Registry) error {

	if typeutils.IsZero(m.EndsAt) { // not required
		return nil
	}

	if err := validate.FormatOf("endsAt", "body", "date-time", m.EndsAt.String(), formats); err != nil {
		return err
	}

	return nil
}

func (m *Subscription) validateExternalCustomerID(formats strfmt.Registry) error {

	if typeutils.IsZero(m.ExternalCustomerID) { // not required
		return nil
	}

	if err := validate.MinLength("externalCustomerId", "body", *m.ExternalCustomerID, 1); err != nil {
		return err
	}

	if err := validate.MaxLength("externalCustomerId", "body", *m.ExternalCustomerID, 255); err != nil {
		return err
	}

	return nil
}

func (m *Subscription) validateExternalSubscriptionID(formats strfmt.Registry) error {

	if typeutils.IsZero(m.ExternalSubscriptionID) { // not required
		return nil
	}

	if err := validate.MinLength("externalSubscriptionId", "body", *m.ExternalSubscriptionID, 1); err != nil {
		return err
	}

	if err := validate.MaxLength("externalSubscriptionId", "body", *m.ExternalSubscriptionID, 255); err != nil {
		return err
	}

	return nil
}

func (m *Subscription) validatePaymentMethodID(formats strfmt.Registry) error {

	if typeutils.IsZero(m.PaymentMethodID) { // not required
		return nil
	}

	if err := validate.FormatOf("paymentMethodId", "body", "uuid", m.PaymentMethodID.String(), formats); err != nil {
		return err
	}

	return nil
}

func (m *Subscription) validatePlanID(formats strfmt.Registry) error {

	if err := validate.Required("planId", "body", m.PlanID); err != nil {
		return err
	}

	if err := validate.FormatOf("planId", "body", "uuid", m.PlanID.String(), formats); err != nil {
		return err
	}

	return nil
}

func (m *Subscription) validateProvider(formats strfmt.Registry) error {

	if typeutils.IsZero(m.Provider) { // not required
		return nil
	}

	if err := validate.MinLength("provider", "body", *m.Provider, 1); err != nil {
		return err
	}

	if err := validate.MaxLength("provider", "body", *m.Provider, 80); err != nil {
		return err
	}

	return nil
}

func (m *Subscription) validateStartsAt(formats strfmt.Registry) error {

	if err := validate.Required("startsAt", "body", m.StartsAt); err != nil {
		return err
	}

	if err := validate.FormatOf("startsAt", "body", "date-time", m.StartsAt.String(), formats); err != nil {
		return err
	}

	return nil
}

var subscriptionTypeStatusPropEnum []any

func init() {
	var res []string
	if err := json.Unmarshal([]byte(`["trialing","active","past_due","paused","canceled","expired"]`), &res); err != nil {
		panic(err)
	}
	for _, v := range res {
		subscriptionTypeStatusPropEnum = append(subscriptionTypeStatusPropEnum, v)
	}
}

// property enum
func (m *Subscription) validateStatusEnum(path, location string, value string) error {
	if err := validate.EnumCase(path, location, value, subscriptionTypeStatusPropEnum, true); err != nil {
		return err
	}
	return nil
}

func (m *Subscription) validateStatus(formats strfmt.Registry) error {

	if err := validate.Required("status", "body", m.Status); err != nil {
		return err
	}

	// value enum
	if err := m.validateStatusEnum("status", "body", *m.Status); err != nil {
		return err
	}

	return nil
}

func (m *Subscription) validateTenantID(formats strfmt.Registry) error {

	if err := validate.Required("tenantId", "body", m.TenantID); err != nil {
		return err
	}

	if err := validate.FormatOf("tenantId", "body", "uuid", m.TenantID.String(), formats); err != nil {
		return err
	}

	return nil
}

// ContextValidate validate this subscription based on the context it is used
func (m *Subscription) ContextValidate(ctx context.Context, formats strfmt.Registry) error {
	var res []error

	// validation for a type composition with AuditFields
	if err := m.AuditFields.ContextValidate(ctx, formats); err != nil {
		res = append(res, err)
	}

	if len(res) > 0 {
		return errors.CompositeValidationError(res...)
	}
	return nil
}

// MarshalBinary interface implementation
func (m *Subscription) MarshalBinary() ([]byte, error) {
	if m == nil {
		return nil, nil
	}
	return jsonutils.WriteJSON(m)
}

// UnmarshalBinary interface implementation
func (m *Subscription) UnmarshalBinary(b []byte) error {
	var res Subscription
	if err := jsonutils.ReadJSON(b, &res); err != nil {
		return err
	}
	*m = res
	return nil
}
