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

// TenantMembership tenant membership
//
// swagger:model TenantMembership
type TenantMembership struct {
	TenantMembershipAuditFields

	// access level
	// Required: true
	// Enum: ["owner","manager","member","read_only"]
	AccessLevel *string `json:"accessLevel"`

	// email
	// Required: true
	// Read Only: true
	// Format: email
	Email strfmt.Email `json:"email"`

	// full name
	// Read Only: true
	FullName *string `json:"fullName,omitempty"`

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

	// tenant Id
	// Required: true
	// Max Length: 36
	// Min Length: 1
	TenantID *string `json:"tenantId"`

	// user Id
	// Required: true
	// Format: uuid
	UserID *strfmt.UUID `json:"userId"`
}

// UnmarshalJSON unmarshals this object from a JSON structure
func (m *TenantMembership) UnmarshalJSON(raw []byte) error {
	// AO0
	var aO0 TenantMembershipAuditFields
	if err := jsonutils.ReadJSON(raw, &aO0); err != nil {
		return err
	}
	m.TenantMembershipAuditFields = aO0

	// AO1
	var dataAO1 struct {
		AccessLevel *string `json:"accessLevel"`

		Email strfmt.Email `json:"email"`

		FullName *string `json:"fullName,omitempty"`

		Status *string `json:"status"`

		TenantID *string `json:"tenantId"`

		UserID *strfmt.UUID `json:"userId"`
	}
	if err := jsonutils.ReadJSON(raw, &dataAO1); err != nil {
		return err
	}

	m.AccessLevel = dataAO1.AccessLevel

	m.Email = dataAO1.Email

	m.FullName = dataAO1.FullName

	m.Status = dataAO1.Status

	m.TenantID = dataAO1.TenantID

	m.UserID = dataAO1.UserID

	return nil
}

// MarshalJSON marshals this object to a JSON structure
func (m TenantMembership) MarshalJSON() ([]byte, error) {
	_parts := make([][]byte, 0, 2)

	aO0, err := jsonutils.WriteJSON(m.TenantMembershipAuditFields)
	if err != nil {
		return nil, err
	}
	_parts = append(_parts, aO0)
	var dataAO1 struct {
		AccessLevel *string `json:"accessLevel"`

		Email strfmt.Email `json:"email"`

		FullName *string `json:"fullName,omitempty"`

		Status *string `json:"status"`

		TenantID *string `json:"tenantId"`

		UserID *strfmt.UUID `json:"userId"`
	}

	dataAO1.AccessLevel = m.AccessLevel

	dataAO1.Email = m.Email

	dataAO1.FullName = m.FullName

	dataAO1.Status = m.Status

	dataAO1.TenantID = m.TenantID

	dataAO1.UserID = m.UserID

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

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

	// validation for a type composition with TenantMembershipAuditFields
	if err := m.TenantMembershipAuditFields.Validate(formats); err != nil {
		res = append(res, err)
	}

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

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

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

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

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

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

var tenantMembershipTypeAccessLevelPropEnum []any

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

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

func (m *TenantMembership) validateAccessLevel(formats strfmt.Registry) error {

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

	// value enum
	if err := m.validateAccessLevelEnum("accessLevel", "body", *m.AccessLevel); err != nil {
		return err
	}

	return nil
}

func (m *TenantMembership) validateEmail(formats strfmt.Registry) error {

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

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

	return nil
}

var tenantMembershipTypeStatusPropEnum []any

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

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

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

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

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

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

	return nil
}

func (m *TenantMembership) validateUserID(formats strfmt.Registry) error {

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

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

	return nil
}

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

	// validation for a type composition with TenantMembershipAuditFields
	if err := m.TenantMembershipAuditFields.ContextValidate(ctx, formats); err != nil {
		res = append(res, err)
	}

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

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

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

func (m *TenantMembership) contextValidateEmail(ctx context.Context, formats strfmt.Registry) error {

	if err := validate.ReadOnly(ctx, "email", "body", m.Email); err != nil {
		return err
	}

	return nil
}

func (m *TenantMembership) contextValidateFullName(ctx context.Context, formats strfmt.Registry) error {

	if err := validate.ReadOnly(ctx, "fullName", "body", m.FullName); err != nil {
		return err
	}

	return nil
}

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

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