package accessgroup

import (
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func dataSourceAccessGroupListSchemaMake() map[string]*schema.Schema {
	res := map[string]*schema.Schema{
		"page": {
			Type:        schema.TypeInt,
			Optional:    true,
			Description: "Page number",
		},
		"per_page": {
			Type:        schema.TypeInt,
			Optional:    true,
			Description: "Items per page",
		},
		"sort_by": {
			Type:         schema.TypeString,
			Optional:     true,
			ValidateFunc: validation.StringInSlice([]string{"display_name", "created_at", "updated_at", "deleted_at", "owner_login"}, false),
			Description:  "sort by one of supported fields",
		},
		"sort_order": {
			Type:         schema.TypeString,
			Optional:     true,
			ValidateFunc: validation.StringInSlice([]string{"asc", "desc"}, false),
			Description:  "sort order",
		},
		"enabled": {
			Type:        schema.TypeBool,
			Optional:    true,
			Description: "filter by enabled/disabled group",
		},
		"deleted": {
			Type:        schema.TypeBool,
			Optional:    true,
			Description: "filter by deleted/not deleted group",
		},
		"display_name": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "filter by display name",
		},
		"created_from": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "filter by the lower limit of the creation date",
		},
		"created_to": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "filter by the upper limit of the creation date",
		},
		"owner_display_name": {
			Type:     schema.TypeString,
			Optional: true,
		},
		"items": {
			Type:        schema.TypeList,
			Computed:    true,
			Description: "List of access groups",
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"id": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "The unique identifier",
					},
					"display_name": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "Display name",
					},
					"comment": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "Comment description",
					},
					"created_at": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "Creation timestamp",
					},
					"net_object_access_group": {
						Type:        schema.TypeList,
						Computed:    true,
						Description: "Net object access group configuration",
						Elem: &schema.Resource{
							Schema: map[string]*schema.Schema{
								"access_group_id": {
									Type:        schema.TypeString,
									Computed:    true,
									Description: "Access group ID",
								},
								"id": {
									Type:        schema.TypeString,
									Computed:    true,
									Description: "Unique identifier",
								},
								"version_id": {
									Type:        schema.TypeInt,
									Computed:    true,
									Description: "Version identifier",
								},
							},
						},
					},
					"default_security_policy": {
						Type:        schema.TypeList,
						Computed:    true,
						Description: "Default security policy configuration",
						Elem: &schema.Resource{
							Schema: map[string]*schema.Schema{
								"display_name": {
									Type:        schema.TypeString,
									Computed:    true,
									Description: "Policy display name",
								},
								"id": {
									Type:        schema.TypeString,
									Computed:    true,
									Description: "Policy unique identifier",
								},
								"description": {
									Type:        schema.TypeString,
									Computed:    true,
									Description: "Policy description",
								},
								"version_id": {
									Type:        schema.TypeInt,
									Computed:    true,
									Description: "Policy version identifier",
								},
								"access_group_id": {
									Type:        schema.TypeString,
									Computed:    true,
									Description: "Policy access group ID",
								},
								"default_acl_drop": {
									Type:        schema.TypeString,
									Computed:    true,
									Description: "Default ACL drop action",
								},
								"default_open_session_drop": {
									Type:        schema.TypeBool,
									Computed:    true,
									Description: "Default open session drop flag",
								},
							},
						},
					},
				},
			},
		},
	}

	return res
}

func dataSourceAccessGroupUserListSchemaMake() map[string]*schema.Schema {
	res := map[string]*schema.Schema{
		"access_group_id": {
			Type:        schema.TypeString,
			Required:    true,
			Description: "filter by access group id",
		},
		"global_role": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "filter by global role",
		},
		"access_group_role": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "filter by access group role",
		},
		"enabled": {
			Type:        schema.TypeBool,
			Optional:    true,
			Description: "filter by inclusion",
		},
		"deleted": {
			Type:        schema.TypeBool,
			Optional:    true,
			Description: "delete filter",
		},
		"display_name": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "filter by display name",
		},
		"login": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "filter by user login",
		},
		"created_by": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "who created the user",
		},
		"deleted_by": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "who deleted the user",
		},
		"disabled_by": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "who disabled the user",
		},
		"page": {
			Type:        schema.TypeInt,
			Optional:    true,
			Description: "result page number",
		},
		"per_page": {
			Type:        schema.TypeInt,
			Optional:    true,
			Description: "number of results per page",
		},
		"sort_by": {
			Type:         schema.TypeString,
			Optional:     true,
			ValidateFunc: validation.StringInSlice([]string{"display_name", "email", "phone", "created_at", "updated_at", "deleted_at"}, false),
			Description:  "sort by one of supported fields",
		},
		"sort_order": {
			Type:         schema.TypeString,
			Optional:     true,
			ValidateFunc: validation.StringInSlice([]string{"asc", "desc"}, false),
			Description:  "sorting order",
		},
		"created_from": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "filter by the lower limit of the creation date",
		},
		"created_to": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "filter by the upper limit of the creation date",
		},
		"items": {
			Type:        schema.TypeList,
			Computed:    true,
			Description: "List of users",
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"id": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "The unique identifier",
					},
					"display_name": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "Display name",
					},
					"role_id": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "Role identifier",
					},
					"login": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "Login",
					},
				},
			},
		},
	}

	return res
}

func resourceAccessGroupSchemaMake() map[string]*schema.Schema {
	res := map[string]*schema.Schema{
		"display_name": {
			Type:        schema.TypeString,
			Required:    true,
			Description: "group name",
		},
		"comment": {
			Type:        schema.TypeString,
			Required:    true,
			Description: "description (comment) of the group",
		},
		"users": {
			Type:        schema.TypeList,
			Optional:    true,
			Description: "managing users who are part of a group",
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"access_group_role_id": {
						Type:        schema.TypeString,
						Required:    true,
						Description: "id of the assigned role",
					},
					"user_id": {
						Type:        schema.TypeString,
						Required:    true,
						Description: "user ID",
					},
				},
			},
		},
		"default_security_policy": {
			Type:        schema.TypeList,
			Optional:    true,
			Computed:    true,
			MaxItems:    1,
			Description: "Default security policy configuration",
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"default_acl_drop": {
						Type:        schema.TypeString,
						Optional:    true,
						Computed:    true,
						Description: "Default ACL drop action",
					},
					"default_open_session_drop": {
						Type:        schema.TypeBool,
						Optional:    true,
						Computed:    true,
						Description: "Default open session drop flag",
					},
					"display_name": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "Policy display name",
					},
					"id": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "Policy unique identifier",
					},
					"description": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "Policy description",
					},
					"version_id": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "Policy version identifier",
					},
					"access_group_id": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "Policy access group ID",
					},
				},
			},
		},
		// Computed fields
		"created_at": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "Creation timestamp",
		},
		"net_object_access_group": {
			Type:        schema.TypeList,
			Computed:    true,
			Description: "Net object access group configuration",
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"access_group_id": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "Access group ID",
					},
					"id": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "Unique identifier",
					},
					"version_id": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "Version identifier",
					},
				},
			},
		},
	}

	return res
}
