// 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 research_models

import (
	"context"
	stderrors "errors"
	"strconv"

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

// CompanyProductProposalRequest Update-only first slice; tenant, actor, client, status, and audit fields are server-owned
//
// swagger:model CompanyProductProposalRequest
type CompanyProductProposalRequest struct {

	// base version
	// Required: true
	// Format: date-time
	BaseVersion *strfmt.DateTime `json:"BaseVersion"`

	// company product ID
	// Required: true
	// Format: uuid
	CompanyProductID *strfmt.UUID `json:"CompanyProductID"`

	// fields
	// Required: true
	// Max Items: 10
	// Min Items: 1
	// Unique: true
	Fields []*CompanyProductProposalField `json:"Fields"`

	// idempotency key
	// Required: true
	// Max Length: 120
	// Min Length: 8
	// Pattern: ^[A-Za-z0-9._:-]+$
	IdempotencyKey *string `json:"IdempotencyKey"`
}

// Validate validates this company product proposal request
func (m *CompanyProductProposalRequest) Validate(formats strfmt.Registry) error {
	var res []error

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

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

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

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

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

func (m *CompanyProductProposalRequest) validateBaseVersion(formats strfmt.Registry) error {

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

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

	return nil
}

func (m *CompanyProductProposalRequest) validateCompanyProductID(formats strfmt.Registry) error {

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

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

	return nil
}

func (m *CompanyProductProposalRequest) validateFields(formats strfmt.Registry) error {

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

	iFieldsSize := int64(len(m.Fields))

	if err := validate.MinItems("Fields", "body", iFieldsSize, 1); err != nil {
		return err
	}

	if err := validate.MaxItems("Fields", "body", iFieldsSize, 10); err != nil {
		return err
	}

	if err := validate.UniqueItems("Fields", "body", m.Fields); err != nil {
		return err
	}

	for i := 0; i < len(m.Fields); i++ {
		if typeutils.IsZero(m.Fields[i]) { // not required
			continue
		}

		if m.Fields[i] != nil {
			if err := m.Fields[i].Validate(formats); err != nil {
				ve := new(errors.Validation)
				if stderrors.As(err, &ve) {
					return ve.ValidateName("Fields" + "." + strconv.Itoa(i))
				}
				ce := new(errors.CompositeError)
				if stderrors.As(err, &ce) {
					return ce.ValidateName("Fields" + "." + strconv.Itoa(i))
				}

				return err
			}
		}

	}

	return nil
}

func (m *CompanyProductProposalRequest) validateIdempotencyKey(formats strfmt.Registry) error {

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

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

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

	if err := validate.Pattern("IdempotencyKey", "body", *m.IdempotencyKey, `^[A-Za-z0-9._:-]+$`); err != nil {
		return err
	}

	return nil
}

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

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

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

func (m *CompanyProductProposalRequest) contextValidateFields(ctx context.Context, formats strfmt.Registry) error {

	for i := 0; i < len(m.Fields); i++ {

		if m.Fields[i] != nil {

			if typeutils.IsZero(m.Fields[i]) { // not required
				return nil
			}

			if err := m.Fields[i].ContextValidate(ctx, formats); err != nil {
				ve := new(errors.Validation)
				if stderrors.As(err, &ve) {
					return ve.ValidateName("Fields" + "." + strconv.Itoa(i))
				}
				ce := new(errors.CompositeError)
				if stderrors.As(err, &ce) {
					return ce.ValidateName("Fields" + "." + strconv.Itoa(i))
				}

				return err
			}
		}

	}

	return nil
}

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

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