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

// CompanyProductProposalReviewRequest Complete atomic field review; rejected fields require a decision reason
//
// swagger:model CompanyProductProposalReviewRequest
type CompanyProductProposalReviewRequest struct {

	// expected last modified date
	// Required: true
	// Format: date-time
	ExpectedLastModifiedDate *strfmt.DateTime `json:"ExpectedLastModifiedDate"`

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

	// review reason
	// Required: true
	// Max Length: 1000
	// Min Length: 1
	ReviewReason *string `json:"ReviewReason"`
}

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

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

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

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

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

func (m *CompanyProductProposalReviewRequest) validateExpectedLastModifiedDate(formats strfmt.Registry) error {

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

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

	return nil
}

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

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

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

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

	return nil
}

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

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