// Package team provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.5.1 DO NOT EDIT.
package team

import (
	"encoding/json"
	"fmt"

	"github.com/meshery/schemas/models/core"
)

// Team A Team is a group of one or more users. Teams are often used as a grouping mechanism for assigning permissions, whether in the context of an organization, a workspace, or some other domain within Meshery. Learn more at https://docs.meshery.io/concepts/logical/teams
type Team struct {
	// Id A Universally Unique Identifier used to uniquely identify entities in Meshery. The UUID core definition is used across different schemas.
	ID core.Uuid `db:"id" json:"id" yaml:"id"`

	// Name Team name
	Name string `db:"name" json:"name" yaml:"name"`

	// Description Team description
	Description *string `db:"description" json:"description,omitempty" yaml:"description,omitempty"`

	// Owner A Universally Unique Identifier used to uniquely identify entities in Meshery. The UUID core definition is used across different schemas.
	Owner *core.Uuid `db:"owner" json:"owner,omitempty" yaml:"owner,omitempty"`

	// Metadata Additional metadata for the team
	Metadata  core.Map          `db:"metadata" json:"metadata,omitempty" yaml:"metadata,omitempty"`
	CreatedAt core.Time `db:"created_at" json:"createdAt" yaml:"createdAt"`
	UpdatedAt core.Time `db:"updated_at" json:"updatedAt" yaml:"updatedAt"`

	// DeletedAt SQL null Timestamp to handle null values of time.
	DeletedAt core.NullTime `db:"deleted_at" json:"deletedAt" yaml:"deletedAt,omitempty"`
}

// TeamMember A user who is a prospective or existing team member. Returned by the "list users in team" endpoint. `joinedAt` is the first canonicalised projection field — other user fields (`id`, `firstName`, `lastName`, `email`, `avatarUrl`) continue to flow through `additionalProperties` pending migration of the user schema to the canonical-casing contract. See meshery/schemas#832 for the per-field roadmap.
type TeamMember struct {
	// JoinedAt SQL null Timestamp to handle null values of time.
	JoinedAt             core.NullTime  `db:"joined_at" json:"joinedAt" yaml:"joinedAt,omitempty"`
	AdditionalProperties map[string]interface{} `json:"-" yaml:"-"`
}

// TeamMembersPage Paginated list of team members.
type TeamMembersPage struct {
	// Data The data of the teammemberspage.
	Data *[]TeamMember `json:"data,omitempty" yaml:"data,omitempty"`

	// Page Current page number of the result set.
	Page *int `json:"page,omitempty" yaml:"page,omitempty"`

	// PageSize Number of items per page.
	PageSize *int `json:"pageSize,omitempty" yaml:"pageSize,omitempty"`

	// TotalCount Total number of items available.
	TotalCount *int `json:"totalCount,omitempty" yaml:"totalCount,omitempty"`
}

// TeamPage Paginated list of teams
type TeamPage struct {
	// Page Current page number of the result set.
	Page *int `json:"page,omitempty" yaml:"page,omitempty"`

	// PageSize Number of items per page.
	PageSize *int `json:"pageSize,omitempty" yaml:"pageSize,omitempty"`

	// Teams The teams of the teampage.
	Teams []Team `json:"teams,omitempty" yaml:"teams,omitempty"`

	// TotalCount Total number of items available.
	TotalCount *int `json:"totalCount,omitempty" yaml:"totalCount,omitempty"`
}

// TeamPayload Payload for creating a new team
type TeamPayload struct {
	Description core.Text `json:"description,omitempty" yaml:"description,omitempty"`

	// Metadata Additional client-supplied metadata for the team.
	Metadata *map[string]interface{} `json:"metadata,omitempty" yaml:"metadata,omitempty"`
	Name     core.Text       `json:"name" yaml:"name"`

	// NotifyTeamUpdate Whether to notify team members when the team is created or updated.
	NotifyTeamUpdate *bool `json:"notifyTeamUpdate,omitempty" yaml:"notifyTeamUpdate,omitempty"`
}

// TeamUpdatePayload Payload for updating an existing team
type TeamUpdatePayload struct {
	Description core.Text `json:"description,omitempty" yaml:"description,omitempty"`
	Name        core.Text `json:"name,omitempty" yaml:"name,omitempty"`
}

// UsersTeamsMapping Join row between users and teams. The schema name is `UsersTeamsMapping` (rather than `TeamsUsersMapping`) so that pop's tableize default produces the live DB table name `users_teams_mappings`, eliminating the need for an explicit `TableName()` helper on the generated Go struct.
type UsersTeamsMapping struct {
	CreatedAt core.Time `db:"created_at" json:"createdAt" yaml:"createdAt,omitempty"`

	// DeletedAt SQL null Timestamp to handle null values of time.
	DeletedAt core.NullTime  `db:"deleted_at" json:"deletedAt" yaml:"deletedAt,omitempty"`
	ID        core.GeneralId `db:"id" json:"id" yaml:"id"`

	// RoleId A Universally Unique Identifier used to uniquely identify entities in Meshery. The UUID core definition is used across different schemas.
	RoleId *core.Uuid `db:"role_id" json:"roleId,omitempty" yaml:"roleId,omitempty"`

	// TeamId A Universally Unique Identifier used to uniquely identify entities in Meshery. The UUID core definition is used across different schemas.
	TeamId    *core.Uuid `db:"team_id" json:"teamId" yaml:"teamId,omitempty"`
	UpdatedAt core.Time  `db:"updated_at" json:"updatedAt" yaml:"updatedAt,omitempty"`

	// UserId A Universally Unique Identifier used to uniquely identify entities in Meshery. The UUID core definition is used across different schemas.
	UserId *core.Uuid `db:"owner" json:"userId" yaml:"userId,omitempty"`
}

// UsersTeamsMappingPage Paginated list of user-team mappings
type UsersTeamsMappingPage struct {
	// Page Current page number of the result set.
	Page *int `json:"page,omitempty" yaml:"page,omitempty"`

	// PageSize Number of items per page.
	PageSize *int `json:"pageSize,omitempty" yaml:"pageSize,omitempty"`

	// TotalCount Total number of items available.
	TotalCount *int `json:"totalCount,omitempty" yaml:"totalCount,omitempty"`

	// UsersTeamsMapping The user-team mappings on the current page.
	UsersTeamsMapping []UsersTeamsMapping `json:"usersTeamsMapping,omitempty" yaml:"usersTeamsMapping,omitempty"`
}

// Order defines model for order.
type Order = string

// OrgId defines model for orgId.
type OrgId = core.OrganizationId

// Page defines model for page.
type Page = int

// PageSize defines model for pageSize.
type PageSize = int

// Pagesize defines model for pagesize.
type Pagesize = int

// Search defines model for search.
type Search = string

// TeamId defines model for teamId.
type TeamId = core.TeamId

// UserId user's email or username
type UserId = core.UserId

// Getter for additional properties for TeamMember. Returns the specified
// element and whether it was found
func (a TeamMember) Get(fieldName string) (value interface{}, found bool) {
	if a.AdditionalProperties != nil {
		value, found = a.AdditionalProperties[fieldName]
	}
	return
}

// Setter for additional properties for TeamMember
func (a *TeamMember) Set(fieldName string, value interface{}) {
	if a.AdditionalProperties == nil {
		a.AdditionalProperties = make(map[string]interface{})
	}
	a.AdditionalProperties[fieldName] = value
}

// Override default JSON handling for TeamMember to handle AdditionalProperties
func (a *TeamMember) UnmarshalJSON(b []byte) error {
	object := make(map[string]json.RawMessage)
	err := json.Unmarshal(b, &object)
	if err != nil {
		return err
	}

	if raw, found := object["joinedAt"]; found {
		err = json.Unmarshal(raw, &a.JoinedAt)
		if err != nil {
			return fmt.Errorf("error reading 'joinedAt': %w", err)
		}
		delete(object, "joinedAt")
	}

	if len(object) != 0 {
		a.AdditionalProperties = make(map[string]interface{})
		for fieldName, fieldBuf := range object {
			var fieldVal interface{}
			err := json.Unmarshal(fieldBuf, &fieldVal)
			if err != nil {
				return fmt.Errorf("error unmarshaling field %s: %w", fieldName, err)
			}
			a.AdditionalProperties[fieldName] = fieldVal
		}
	}
	return nil
}

// Override default JSON handling for TeamMember to handle AdditionalProperties
func (a TeamMember) MarshalJSON() ([]byte, error) {
	var err error
	object := make(map[string]json.RawMessage)

	object["joinedAt"], err = json.Marshal(a.JoinedAt)
	if err != nil {
		return nil, fmt.Errorf("error marshaling 'joinedAt': %w", err)
	}

	for fieldName, field := range a.AdditionalProperties {
		object[fieldName], err = json.Marshal(field)
		if err != nil {
			return nil, fmt.Errorf("error marshaling '%s': %w", fieldName, err)
		}
	}
	return json.Marshal(object)
}
