package rg

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

func resourceRGResourceV1() *schema.Resource {
	return &schema.Resource{
		Schema: map[string]*schema.Schema{
			"account_id": {
				Type:         schema.TypeInt,
				Required:     true,
				ValidateFunc: validation.IntAtLeast(1),
				Description:  "Unique ID of the account, which this resource group belongs to.",
			},
			"gid": {
				Type:        schema.TypeInt,
				Required:    true,
				ForceNew:    true,
				Description: "Unique ID of the grid, where this resource group is deployed.",
			},
			"name": {
				Type:        schema.TypeString,
				Required:    true,
				Description: "Name of this resource group. Names are case sensitive and unique within the context of a account.",
			},
			"def_net_type": {
				Type:         schema.TypeString,
				Optional:     true,
				Computed:     true,
				ValidateFunc: validation.StringInSlice([]string{"PRIVATE", "PUBLIC", "NONE"}, false),
				Description:  "Type of the network, which this resource group will use as default for its computes - PRIVATE or PUBLIC or NONE.",
			},
			"def_net_id": {
				Type:        schema.TypeInt,
				Computed:    true,
				Description: "ID of the default network for this resource group (if any).",
			},
			"ipcidr": {
				Type:        schema.TypeString,
				Optional:    true,
				Description: "Address of the netowrk inside the private network segment (aka ViNS) if def_net_type=PRIVATE",
			},
			"ext_net_id": {
				Type:        schema.TypeInt,
				Optional:    true,
				Default:     0,
				Description: "ID of the external network for default ViNS. Pass 0 if def_net_type=PUBLIC or no external connection required for the defult ViNS when def_net_type=PRIVATE",
			},
			"ext_ip": {
				Type:        schema.TypeString,
				Optional:    true,
				Description: "IP address on the external netowrk to request when def_net_type=PRIVATE and ext_net_id is not 0",
			},
			"quota": {
				Type:     schema.TypeList,
				Optional: true,
				MaxItems: 1,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"cpu": {
							Type:        schema.TypeInt,
							Optional:    true,
							Default:     -1,
							Description: "Limit on the total number of CPUs in this resource group.",
						},
						"ram": {
							Type:        schema.TypeFloat, // NB: API expects and returns this as float in units of MB! This may be changed in the future.
							Optional:    true,
							Default:     -1,
							Description: "Limit on the total amount of RAM in this resource group, specified in MB.",
						},
						"disk": {
							Type:        schema.TypeInt,
							Optional:    true,
							Default:     -1,
							Description: "Limit on the total volume of storage resources in this resource group, specified in GB.",
						},
						"ext_traffic": {
							Type:        schema.TypeInt,
							Optional:    true,
							Default:     -1,
							Description: "Limit on the total ingress network traffic for this resource group, specified in GB.",
						},
						"ext_ips": {
							Type:        schema.TypeInt,
							Optional:    true,
							Default:     -1,
							Description: "Limit on the total number of external IP addresses this resource group can use.",
						},
						"gpu_units": {
							Type:        schema.TypeInt,
							Optional:    true,
							Default:     -1,
							Description: "Limit on the total number of virtual GPUs this resource group can use.",
						},
					},
				},
				Description: "Quota settings for this resource group.",
			},
			"access": {
				Type:     schema.TypeSet,
				Optional: true,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"user": {
							Type:        schema.TypeString,
							Required:    true,
							Description: "User or group name to grant access",
						},
						"right": {
							Type:        schema.TypeString,
							Required:    true,
							Description: "Access rights to set, one of 'R', 'RCX' or 'ARCXDU'",
						},
						"reason": {
							Type:        schema.TypeString,
							Optional:    true,
							Description: "Reason for action",
						},
					},
				},
			},
			"def_net": {
				Type:     schema.TypeSet,
				Optional: true,
				MaxItems: 1,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"net_type": {
							Type:         schema.TypeString,
							Required:     true,
							ValidateFunc: validation.StringInSlice([]string{"PRIVATE", "PUBLIC"}, false),
							Description:  "Network type to set. Must be on of 'PRIVATE' or 'PUBLIC'.",
						},
						"net_id": {
							Type:        schema.TypeInt,
							Optional:    true,
							Default:     0,
							Description: "Network segment ID. If netType is PUBLIC and netId is 0 then default external network segment will be selected. If netType is PRIVATE and netId=0, the first ViNS defined for this RG will be selected. Otherwise, netId identifies either existing external network segment or ViNS.",
						},
						"reason": {
							Type:        schema.TypeString,
							Optional:    true,
							Description: "Reason for action",
						},
					},
				},
			},
			"description": {
				Type:        schema.TypeString,
				Optional:    true,
				Computed:    true,
				Description: "User-defined text description of this resource group.",
			},
			"force": {
				Type:        schema.TypeBool,
				Optional:    true,
				Default:     false,
				Description: "Set to True if you want force delete non-empty RG",
			},
			"permanently": {
				Type:        schema.TypeBool,
				Optional:    true,
				Default:     false,
				Description: "Set to True if you want force delete non-empty RG",
			},
			"reason": {
				Type:        schema.TypeString,
				Optional:    true,
				Description: "Set to True if you want force delete non-empty RG",
			},
			"register_computes": {
				Type:        schema.TypeBool,
				Optional:    true,
				Default:     false,
				Description: "Register computes in registration system",
			},
			"enable": {
				Type:        schema.TypeBool,
				Optional:    true,
				Default:     true,
				Description: "flag for enable/disable RG",
			},
			"account_name": {
				Type:        schema.TypeString,
				Computed:    true,
				Description: "Name of the account, which this resource group belongs to.",
			},
			// "resources": {
			// 	Type:     schema.TypeList,
			// 	Computed: true,
			// 	Elem: &schema.Resource{
			// 		Schema: resourcesSchemaMake(),
			// 	},
			// },

			"acl": {
				Type:     schema.TypeList,
				Computed: true,
				Elem: &schema.Resource{
					Schema: aclSchemaMake(),
				},
			},
			"deleted_by": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"deleted_time": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"dirty": {
				Type:     schema.TypeBool,
				Computed: true,
			},
			"guid": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"rg_id": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"lock_status": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"milestones": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"secret": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"status": {
				Type:        schema.TypeString,
				Computed:    true,
				Description: "Current status of this resource group.",
			},
			"updated_by": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"updated_time": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"vins": {
				Type:     schema.TypeList,
				Computed: true,
				Elem: &schema.Schema{
					Type: schema.TypeInt,
				},
				Description: "List of VINs deployed in this resource group.",
			},

			"vms": {
				Type:     schema.TypeList,
				Computed: true,
				Elem: &schema.Schema{
					Type: schema.TypeInt,
				},
				Description: "List of computes deployed in this resource group.",
			},
			"res_types": {
				Type:     schema.TypeList,
				Computed: true,
				Elem: &schema.Schema{
					Type: schema.TypeString,
				},
			},
			"uniq_pools": {
				Type:     schema.TypeList,
				Computed: true,
				Elem: &schema.Schema{
					Type: schema.TypeString,
				},
			},
			"cpu_allocation_parameter": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"cpu_allocation_ratio": {
				Type:     schema.TypeFloat,
				Computed: true,
			},
		},
	}
}
