package sep

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

func dataSourceSepCSchemaMake() map[string]*schema.Schema {
	return map[string]*schema.Schema{
		"sep_id": {
			Type:        schema.TypeInt,
			Required:    true,
			Description: "sep type des id",
		},
		"config": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "config",
		},
		"consumed_by": {
			Type:        schema.TypeSet,
			Computed:    true,
			Description: "consumed by",
			Elem: &schema.Schema{
				Type: schema.TypeInt,
			},
		},
		"desc": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "description",
		},
		"gid": {
			Type:        schema.TypeInt,
			Computed:    true,
			Description: "gid",
		},
		"guid": {
			Type:        schema.TypeInt,
			Computed:    true,
			Description: "guid",
		},
		"multipath_num": {
			Type:        schema.TypeInt,
			Computed:    true,
			Description: "multipath_num",
		},
		"milestones": {
			Type:        schema.TypeInt,
			Computed:    true,
			Description: "milestones",
		},
		"name": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "name",
		},
		"obj_status": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "object status",
		},
		"provided_by": {
			Type:        schema.TypeList,
			Computed:    true,
			Description: "provided by",
			Elem: &schema.Schema{
				Type: schema.TypeInt,
			},
		},
		"shared_with": {
			Type:        schema.TypeList,
			Computed:    true,
			Description: "shared with",
			Elem: &schema.Schema{
				Type: schema.TypeInt,
			},
		},
		"tech_status": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "tech status",
		},
		"type": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "type",
		},
	}
}

func dataSourceSepConfigSchemaMake() map[string]*schema.Schema {
	return map[string]*schema.Schema{
		"sep_id": {
			Type:        schema.TypeInt,
			Required:    true,
			Description: "storage endpoint provider ID",
		},
		"config": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "sep config json string",
		},
	}
}

func dataSourceSepTemplateSchemaMake() map[string]*schema.Schema {
	return map[string]*schema.Schema{
		"sep_type": {
			Type:         schema.TypeString,
			Required:     true,
			ValidateFunc: validation.StringInSlice([]string{"des", "hitachi", "dorado", "tatlin", "shared", "local", "ustor"}, false),
			Description:  "type of sep",
		},
		"lang": {
			Type:         schema.TypeString,
			Required:     true,
			ValidateFunc: validation.StringInSlice([]string{"ru", "en"}, false),
			Description:  "language",
		},
		"sep_template": {
			Type:     schema.TypeString,
			Computed: true,
		},
	}
}

func dataSourceSepConsumptionSchemaMake() map[string]*schema.Schema {
	return map[string]*schema.Schema{
		"sep_id": {
			Type:        schema.TypeInt,
			Required:    true,
			Description: "sep id",
		},
		"by_pool": {
			Type:        schema.TypeList,
			Computed:    true,
			Description: "consumption divided by pool",
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"name": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "pool name",
					},
					"disk_count": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "number of disks",
					},
					"disk_usage": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "disk usage",
					},
					"snapshot_count": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "number of snapshots",
					},
					"snapshot_usage": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "snapshot usage",
					},
					"usage": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "usage",
					},
					"usage_limit": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "usage limit",
					},
				},
			},
		},
		"total": {
			Type:        schema.TypeList,
			Computed:    true,
			Description: "total consumption",
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"capacity_limit": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "capacity limit",
					},
					"disk_count": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "number of disks",
					},
					"disk_usage": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "disk usage",
					},
					"snapshot_count": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "number of snapshots",
					},
					"snapshot_usage": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "snapshot usage",
					},
					"usage": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "usage",
					},
					"usage_limit": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "usage limit",
					},
				},
			},
		},
		"type": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "sep type",
		},
	}
}

func dataSourceSepDiskListSchemaMake() map[string]*schema.Schema {
	rets := map[string]*schema.Schema{
		"sep_id": {
			Type:        schema.TypeInt,
			Required:    true,
			Description: "storage endpoint provider ID",
		},
		"pool_name": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "pool name",
		},
		"items": {
			Type:        schema.TypeList,
			Computed:    true,
			Description: "sep disk list",
			Elem: &schema.Schema{
				Type: schema.TypeInt,
			},
		},
	}

	return rets
}

func dataSourceSepListSchemaMake() map[string]*schema.Schema {
	rets := map[string]*schema.Schema{
		"by_id": {
			Type:        schema.TypeInt,
			Optional:    true,
			Description: "find by id",
		},
		"name": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "find by name",
		},
		"gid": {
			Type:        schema.TypeInt,
			Optional:    true,
			Description: "find by gid",
		},
		"type": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "find by sep type",
		},
		"provided_by": {
			Type:        schema.TypeInt,
			Optional:    true,
			Description: "find by provided physical node id",
		},
		"tech_status": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "find by techStatus",
		},
		"consumed_by": {
			Type:        schema.TypeInt,
			Optional:    true,
			Description: "find by consumed physical node id",
		},
		"sort_by": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "sort by one of supported fields, format +|-(field)",
		},
		"page": {
			Type:        schema.TypeInt,
			Optional:    true,
			Description: "page number",
		},
		"size": {
			Type:        schema.TypeInt,
			Optional:    true,
			Description: "page size",
		},
		"sep_ids": {
			Type:        schema.TypeList,
			Optional:    true,
			Description: "sort by list of SEP identifiers",
			Elem: &schema.Schema{
				Type: schema.TypeInt,
			},
		},
		"items": {
			Type:        schema.TypeList,
			Computed:    true,
			Description: "sep list",
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"config": {
						Description: "config",
						Type:        schema.TypeString,
						Computed:    true,
					},
					"consumed_by": {
						Type:        schema.TypeSet,
						Computed:    true,
						Description: "consumed by",
						Elem: &schema.Schema{
							Type: schema.TypeInt,
						},
					},
					"desc": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "description",
					},
					"gid": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "gid",
					},
					"guid": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "guid",
					},
					"sep_id": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "sep id",
					},
					"milestones": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "milestones",
					},
					"multipath_num": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "multipath_num",
					},
					"name": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "name",
					},
					"obj_status": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "object status",
					},
					"provided_by": {
						Type:        schema.TypeList,
						Computed:    true,
						Description: "provided by",
						Elem: &schema.Schema{
							Type: schema.TypeInt,
						},
					},
					"shared_with": {
						Type:        schema.TypeList,
						Computed:    true,
						Description: "shared with",
						Elem: &schema.Schema{
							Type: schema.TypeInt,
						},
					},
					"tech_status": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "tech status",
					},
					"type": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "type",
					},
				},
			},
		},
		"entry_count": {
			Type:        schema.TypeInt,
			Computed:    true,
			Description: "entryCount",
		},
	}

	return rets
}

func dataSourceSepPoolSchemaMake() map[string]*schema.Schema {
	return map[string]*schema.Schema{
		"sep_id": {
			Type:        schema.TypeInt,
			Required:    true,
			Description: "storage endpoint provider ID",
		},
		"pool_name": {
			Type:        schema.TypeString,
			Required:    true,
			Description: "pool name",
		},
		"pool": {
			Type:        schema.TypeList,
			Computed:    true,
			Description: "pool",
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"access_account_ids": {
						Type:        schema.TypeList,
						Computed:    true,
						Description: "access account ids",
						Elem: &schema.Schema{
							Type: schema.TypeInt,
						},
					},
					"access_res_group_ids": {
						Type:        schema.TypeList,
						Computed:    true,
						Description: "access res group ids",
						Elem: &schema.Schema{
							Type: schema.TypeInt,
						},
					},
					"name": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "name",
					},
					"pagecache_ratio": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "pagecache_ratio",
					},
					"reference_id": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "reference_id",
					},
					"types": {
						Type:        schema.TypeList,
						Computed:    true,
						Description: "types",
						Elem: &schema.Schema{
							Type: schema.TypeString,
						},
					},
					"uris": {
						Type:        schema.TypeSet,
						Computed:    true,
						Description: "uris",
						Elem: &schema.Resource{
							Schema: map[string]*schema.Schema{
								"ip": {
									Type:        schema.TypeString,
									Computed:    true,
									Description: "ip",
								},
								"port": {
									Type:        schema.TypeInt,
									Computed:    true,
									Description: "port",
								},
							},
						},
					},
					"usage_limit": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "usage limit",
					},
				},
			},
		},
	}
}

func resourceSepSchemaMake() map[string]*schema.Schema {
	return map[string]*schema.Schema{
		"gid": {
			Type:        schema.TypeInt,
			Required:    true,
			Description: "grid (platform) ID",
		},
		"name": {
			Type:        schema.TypeString,
			Required:    true,
			Description: "SEP name",
		},
		"type": {
			Type:         schema.TypeString,
			Required:     true,
			ValidateFunc: validation.StringInSlice([]string{"des", "hitachi", "dorado", "tatlin", "shared", "local", "ustor"}, false),
			Description:  "type of storage",
		},
		"access_to_pool": {
			Type:        schema.TypeSet,
			MaxItems:    1,
			Optional:    true,
			Computed:    true,
			Description: "grant or revoke access  to pool",
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"pool_name": {
						Type:        schema.TypeString,
						Required:    true,
						Description: "pool name",
					},
					"account_id_pool": {
						Type:        schema.TypeInt,
						Optional:    true,
						Description: "account id to grant/revoke access to the specified pool sep",
					},
					"rg_id": {
						Type:        schema.TypeInt,
						Required:    true,
						Description: "resource group id to grant/revoke access to the specified pool sep",
					},
				},
			},
		},
		"account_ids": {
			Type:        schema.TypeSet,
			Optional:    true,
			Description: "lift of account ids to have access to sep",
			Elem: &schema.Schema{
				Type: schema.TypeInt,
			},
		},
		"enable": {
			Type:        schema.TypeBool,
			Optional:    true,
			Default:     false,
			Description: "enable SEP after creation",
		},
		"sep_id": {
			Type:        schema.TypeInt,
			Optional:    true,
			Computed:    true,
			Description: "sep type des id",
		},
		"pools": {
			Type:        schema.TypeSet,
			Optional:    true,
			Description: "add/delete pools to/from sep",
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"access_account_ids": {
						Type:        schema.TypeList,
						Required:    true,
						Description: "access account ids",
						Elem: &schema.Schema{
							Type: schema.TypeInt,
						},
					},
					"access_res_group_ids": {
						Type:        schema.TypeList,
						Required:    true,
						Description: "access res group ids",
						Elem: &schema.Schema{
							Type: schema.TypeInt,
						},
					},
					"name": {
						Type:        schema.TypeString,
						Optional:    true,
						Description: "name",
					},
					"types": {
						Type:        schema.TypeList,
						Optional:    true,
						Description: "types",
						Elem: &schema.Schema{
							Type: schema.TypeString,
						},
					},
					"uris": {
						Type:        schema.TypeSet,
						Optional:    true,
						Description: "uris",
						Elem: &schema.Resource{
							Schema: map[string]*schema.Schema{
								"ip": {
									Type:        schema.TypeString,
									Required:    true,
									Description: "ip",
								},
								"port": {
									Type:        schema.TypeInt,
									Required:    true,
									Description: "port",
								},
							},
						},
					},
					"usage_limit": {
						Type:        schema.TypeInt,
						Required:    true,
						Description: "usage limit",
					},
				},
			},
		},
		"config": {
			Type:             schema.TypeString,
			Required:         true,
			Description:      "sep config string",
			DiffSuppressFunc: resourceSepDiffSupperss,
		},
		"consumed_by": {
			Type:     schema.TypeSet,
			Optional: true,
			Computed: true,
			Elem: &schema.Schema{
				Type: schema.TypeInt,
			},
			Description: "list of consumer nodes IDs",
		},
		"desc": {
			Type:        schema.TypeString,
			Computed:    true,
			Optional:    true,
			Description: "sep description",
		},
		"guid": {
			Type:        schema.TypeInt,
			Computed:    true,
			Description: "guid",
		},
		"milestones": {
			Type:        schema.TypeInt,
			Computed:    true,
			Description: "milestones",
		},
		"multipath_num": {
			Type:        schema.TypeInt,
			Computed:    true,
			Description: "multipath_num",
		},
		"obj_status": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "object status",
		},
		"provided_by": {
			Type:     schema.TypeList,
			Optional: true,
			Computed: true,
			Elem: &schema.Schema{
				Type: schema.TypeInt,
			},
			Description: "list of provider nodes IDs",
		},
		"shared_with": {
			Type:     schema.TypeList,
			Optional: true,
			Computed: true,
			Elem: &schema.Schema{
				Type: schema.TypeInt,
			},
			Description: "list of shared with ids",
		},
		"tech_status": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "tech status",
		},
	}
}

func resourceSepConfigSchemaMake() map[string]*schema.Schema {
	return map[string]*schema.Schema{
		"sep_id": {
			Type:        schema.TypeInt,
			Required:    true,
			Description: "sep id",
		},
		"config": {
			Type:        schema.TypeString,
			Optional:    true,
			Computed:    true,
			Description: "config json string",
		},
		"field_edit": {
			Type:     schema.TypeList,
			MaxItems: 1,
			Optional: true,
			Computed: true,
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"field_name": {
						Type:        schema.TypeString,
						Required:    true,
						Description: "field name",
					},
					"field_value": {
						Type:        schema.TypeString,
						Required:    true,
						Description: "field value",
					},
					"field_type": {
						Type:        schema.TypeString,
						Required:    true,
						Description: "field type",
					},
				},
			},
		},
	}
}

func dataSourceAvailableSEPListSchemaMake() map[string]*schema.Schema {
	return map[string]*schema.Schema{
		"account_id": {
			Type:        schema.TypeInt,
			Required:    true,
			Description: "Account ID",
		},
		"rg_id": {
			Type:        schema.TypeInt,
			Optional:    true,
			Description: "Resource group ID",
		},
		"entry_count": {
			Type:        schema.TypeInt,
			Computed:    true,
			Description: "Number of available SEP entries",
		},
		"items": {
			Type:        schema.TypeList,
			Computed:    true,
			Description: "List of available SEPs",
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"sep_id": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "SEP ID",
					},
					"sep_name": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "SEP name",
					},
					"sep_type": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "SEP type",
					},
					"pools": {
						Type:        schema.TypeList,
						Computed:    true,
						Description: "List of pools in the SEP",
						Elem: &schema.Resource{
							Schema: map[string]*schema.Schema{
								"name": {
									Type:        schema.TypeString,
									Computed:    true,
									Description: "Pool name",
								},
								"types": {
									Type:        schema.TypeList,
									Computed:    true,
									Description: "List of pool types",
									Elem: &schema.Schema{
										Type: schema.TypeString,
									},
								},
								"system": {
									Type:        schema.TypeBool,
									Computed:    true,
									Description: "Is system pool",
								},
							},
						},
					},
				},
			},
		},
	}
}
