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

// Tenant Account Tenant
//
// swagger:model Tenant
type Tenant struct {

	// The Account that owns this Tenant
	AccountID *string `json:"AccountID,omitempty"`

	// Is this Tenant currently active?
	Active *bool `json:"Active,omitempty"`

	// Created By
	CreatedByID *string `json:"CreatedByID,omitempty"`

	// Created Date
	CreatedDate *string `json:"CreatedDate,omitempty"`

	// databases
	Databases []*Database `json:"Databases"`

	// Record Id
	ID string `json:"ID,omitempty"`

	// Last Modified By
	LastModifiedByID *string `json:"LastModifiedByID,omitempty"`

	// Last Modifed Date
	LastModifiedDate *string `json:"LastModifiedDate,omitempty"`

	// roles
	Roles []*Role `json:"Roles"`

	// The current status of this Tenant
	Status *string `json:"Status,omitempty"`

	// Name of the Tenant Resource
	TenantName *string `json:"TenantName,omitempty"`

	// tenant users
	TenantUsers []*TenantUser `json:"TenantUsers"`

	// Type of tenant
	Type *string `json:"Type,omitempty"`

	// The version number of the Tenant Onboarding system used to create this tenant
	Version *string `json:"Version,omitempty"`
}

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

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

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

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

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

func (m *Tenant) validateDatabases(formats strfmt.Registry) error {
	if typeutils.IsZero(m.Databases) { // not required
		return nil
	}

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

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

				return err
			}
		}

	}

	return nil
}

func (m *Tenant) validateRoles(formats strfmt.Registry) error {
	if typeutils.IsZero(m.Roles) { // not required
		return nil
	}

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

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

				return err
			}
		}

	}

	return nil
}

func (m *Tenant) validateTenantUsers(formats strfmt.Registry) error {
	if typeutils.IsZero(m.TenantUsers) { // not required
		return nil
	}

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

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

				return err
			}
		}

	}

	return nil
}

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

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

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

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

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

func (m *Tenant) contextValidateDatabases(ctx context.Context, formats strfmt.Registry) error {

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

		if m.Databases[i] != nil {

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

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

				return err
			}
		}

	}

	return nil
}

func (m *Tenant) contextValidateRoles(ctx context.Context, formats strfmt.Registry) error {

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

		if m.Roles[i] != nil {

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

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

				return err
			}
		}

	}

	return nil
}

func (m *Tenant) contextValidateTenantUsers(ctx context.Context, formats strfmt.Registry) error {

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

		if m.TenantUsers[i] != nil {

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

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

				return err
			}
		}

	}

	return nil
}

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

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