// 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"
)

// Plan plan
//
// swagger:model Plan
type Plan struct {
	AuditFields

	// billing period
	// Required: true
	// Enum: ["none","month","year","custom"]
	BillingPeriod *string `json:"billingPeriod"`

	// currency
	// Required: true
	// Max Length: 3
	// Min Length: 3
	Currency *string `json:"currency"`

	// description
	Description *string `json:"description,omitempty"`

	// effective from
	// Format: date-time
	EffectiveFrom *strfmt.DateTime `json:"effectiveFrom,omitempty"`

	// effective until
	// Format: date-time
	EffectiveUntil *strfmt.DateTime `json:"effectiveUntil,omitempty"`

	// name
	// Required: true
	// Max Length: 255
	// Min Length: 1
	Name *string `json:"name"`

	// plan key
	// Required: true
	// Max Length: 80
	// Min Length: 1
	PlanKey *string `json:"planKey"`

	// status
	// Required: true
	// Enum: ["draft","active","retired"]
	Status *string `json:"status"`

	// Exact decimal string matching SQL DECIMAL(12,2).
	// Required: true
	// Max Length: 13
	// Min Length: 1
	// Pattern: ^(0|[1-9][0-9]{0,9})(\.[0-9]{1,2})?$
	UnitAmount *string `json:"unitAmount"`

	// version
	// Required: true
	// Maximum: 2.147483647e+09
	// Minimum: 1
	Version *int32 `json:"version"`
}

// UnmarshalJSON unmarshals this object from a JSON structure
func (m *Plan) 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 {
		BillingPeriod *string `json:"billingPeriod"`

		Currency *string `json:"currency"`

		Description *string `json:"description,omitempty"`

		EffectiveFrom *strfmt.DateTime `json:"effectiveFrom,omitempty"`

		EffectiveUntil *strfmt.DateTime `json:"effectiveUntil,omitempty"`

		Name *string `json:"name"`

		PlanKey *string `json:"planKey"`

		Status *string `json:"status"`

		UnitAmount *string `json:"unitAmount"`

		Version *int32 `json:"version"`
	}
	if err := jsonutils.ReadJSON(raw, &dataAO1); err != nil {
		return err
	}

	m.BillingPeriod = dataAO1.BillingPeriod

	m.Currency = dataAO1.Currency

	m.Description = dataAO1.Description

	m.EffectiveFrom = dataAO1.EffectiveFrom

	m.EffectiveUntil = dataAO1.EffectiveUntil

	m.Name = dataAO1.Name

	m.PlanKey = dataAO1.PlanKey

	m.Status = dataAO1.Status

	m.UnitAmount = dataAO1.UnitAmount

	m.Version = dataAO1.Version

	return nil
}

// MarshalJSON marshals this object to a JSON structure
func (m Plan) 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 {
		BillingPeriod *string `json:"billingPeriod"`

		Currency *string `json:"currency"`

		Description *string `json:"description,omitempty"`

		EffectiveFrom *strfmt.DateTime `json:"effectiveFrom,omitempty"`

		EffectiveUntil *strfmt.DateTime `json:"effectiveUntil,omitempty"`

		Name *string `json:"name"`

		PlanKey *string `json:"planKey"`

		Status *string `json:"status"`

		UnitAmount *string `json:"unitAmount"`

		Version *int32 `json:"version"`
	}

	dataAO1.BillingPeriod = m.BillingPeriod

	dataAO1.Currency = m.Currency

	dataAO1.Description = m.Description

	dataAO1.EffectiveFrom = m.EffectiveFrom

	dataAO1.EffectiveUntil = m.EffectiveUntil

	dataAO1.Name = m.Name

	dataAO1.PlanKey = m.PlanKey

	dataAO1.Status = m.Status

	dataAO1.UnitAmount = m.UnitAmount

	dataAO1.Version = m.Version

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

// Validate validates this plan
func (m *Plan) 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.validateBillingPeriod(formats); err != nil {
		res = append(res, err)
	}

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

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

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

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

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

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

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

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

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

var planTypeBillingPeriodPropEnum []any

func init() {
	var res []string
	if err := json.Unmarshal([]byte(`["none","month","year","custom"]`), &res); err != nil {
		panic(err)
	}
	for _, v := range res {
		planTypeBillingPeriodPropEnum = append(planTypeBillingPeriodPropEnum, v)
	}
}

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

func (m *Plan) validateBillingPeriod(formats strfmt.Registry) error {

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

	// value enum
	if err := m.validateBillingPeriodEnum("billingPeriod", "body", *m.BillingPeriod); err != nil {
		return err
	}

	return nil
}

func (m *Plan) validateCurrency(formats strfmt.Registry) error {

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

	if err := validate.MinLength("currency", "body", *m.Currency, 3); err != nil {
		return err
	}

	if err := validate.MaxLength("currency", "body", *m.Currency, 3); err != nil {
		return err
	}

	return nil
}

func (m *Plan) validateEffectiveFrom(formats strfmt.Registry) error {

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

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

	return nil
}

func (m *Plan) validateEffectiveUntil(formats strfmt.Registry) error {

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

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

	return nil
}

func (m *Plan) validateName(formats strfmt.Registry) error {

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

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

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

	return nil
}

func (m *Plan) validatePlanKey(formats strfmt.Registry) error {

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

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

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

	return nil
}

var planTypeStatusPropEnum []any

func init() {
	var res []string
	if err := json.Unmarshal([]byte(`["draft","active","retired"]`), &res); err != nil {
		panic(err)
	}
	for _, v := range res {
		planTypeStatusPropEnum = append(planTypeStatusPropEnum, v)
	}
}

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

func (m *Plan) 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 *Plan) validateUnitAmount(formats strfmt.Registry) error {

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

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

	if err := validate.MaxLength("unitAmount", "body", *m.UnitAmount, 13); err != nil {
		return err
	}

	if err := validate.Pattern("unitAmount", "body", *m.UnitAmount, `^(0|[1-9][0-9]{0,9})(\.[0-9]{1,2})?$`); err != nil {
		return err
	}

	return nil
}

func (m *Plan) validateVersion(formats strfmt.Registry) error {

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

	if err := validate.MinimumInt("version", "body", int64(*m.Version), 1, false); err != nil {
		return err
	}

	if err := validate.MaximumInt("version", "body", int64(*m.Version), 2.147483647e+09, false); err != nil {
		return err
	}

	return nil
}

// ContextValidate validate this plan based on the context it is used
func (m *Plan) 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 *Plan) MarshalBinary() ([]byte, error) {
	if m == nil {
		return nil, nil
	}
	return jsonutils.WriteJSON(m)
}

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