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

// PlanService plan service
//
// swagger:model PlanService
type PlanService struct {
	AuditFields

	// grant mode
	// Required: true
	// Enum: ["included","metered"]
	GrantMode *string `json:"grantMode"`

	// limit period
	// Required: true
	// Enum: ["none","day","month","billing_period","lifetime"]
	LimitPeriod *string `json:"limitPeriod"`

	// Exact decimal string matching SQL DECIMAL(20,6); null means unlimited. This wave does not meter or decrement it.
	// Max Length: 21
	// Min Length: 1
	// Pattern: ^(0|[1-9][0-9]{0,13})(\.[0-9]{1,6})?$
	LimitQuantity *string `json:"limitQuantity,omitempty"`

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

	// service Id
	// Required: true
	// Format: uuid
	ServiceID *strfmt.UUID `json:"serviceId"`

	// unit
	// Max Length: 40
	Unit *string `json:"unit,omitempty"`
}

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

		LimitPeriod *string `json:"limitPeriod"`

		LimitQuantity *string `json:"limitQuantity,omitempty"`

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

		ServiceID *strfmt.UUID `json:"serviceId"`

		Unit *string `json:"unit,omitempty"`
	}
	if err := jsonutils.ReadJSON(raw, &dataAO1); err != nil {
		return err
	}

	m.GrantMode = dataAO1.GrantMode

	m.LimitPeriod = dataAO1.LimitPeriod

	m.LimitQuantity = dataAO1.LimitQuantity

	m.PlanID = dataAO1.PlanID

	m.ServiceID = dataAO1.ServiceID

	m.Unit = dataAO1.Unit

	return nil
}

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

		LimitPeriod *string `json:"limitPeriod"`

		LimitQuantity *string `json:"limitQuantity,omitempty"`

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

		ServiceID *strfmt.UUID `json:"serviceId"`

		Unit *string `json:"unit,omitempty"`
	}

	dataAO1.GrantMode = m.GrantMode

	dataAO1.LimitPeriod = m.LimitPeriod

	dataAO1.LimitQuantity = m.LimitQuantity

	dataAO1.PlanID = m.PlanID

	dataAO1.ServiceID = m.ServiceID

	dataAO1.Unit = m.Unit

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

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

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

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

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

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

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

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

var planServiceTypeGrantModePropEnum []any

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

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

func (m *PlanService) validateGrantMode(formats strfmt.Registry) error {

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

	// value enum
	if err := m.validateGrantModeEnum("grantMode", "body", *m.GrantMode); err != nil {
		return err
	}

	return nil
}

var planServiceTypeLimitPeriodPropEnum []any

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

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

func (m *PlanService) validateLimitPeriod(formats strfmt.Registry) error {

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

	// value enum
	if err := m.validateLimitPeriodEnum("limitPeriod", "body", *m.LimitPeriod); err != nil {
		return err
	}

	return nil
}

func (m *PlanService) validateLimitQuantity(formats strfmt.Registry) error {

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

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

	if err := validate.MaxLength("limitQuantity", "body", *m.LimitQuantity, 21); err != nil {
		return err
	}

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

	return nil
}

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

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

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

	return nil
}

func (m *PlanService) validateUnit(formats strfmt.Registry) error {

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

	if err := validate.MaxLength("unit", "body", *m.Unit, 40); err != nil {
		return err
	}

	return nil
}

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

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