package schemas

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
	"github.com/hashicorp/terraform-plugin-framework/resource/schema"
	"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
	"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
	"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
	"github.com/hashicorp/terraform-plugin-framework/schema/validator"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

func MakeSchemaResourceVINS() map[string]schema.Attribute {
	return map[string]schema.Attribute{
		// required attributes
		"name": schema.StringAttribute{
			Required:    true,
			Description: "vins name",
		},

		// optional attributes
		"rg_id": schema.Int64Attribute{
			Optional:    true, // either rg_id or account_id must be specified
			Computed:    true,
			Description: "resource group id, used for creating vins in resource group",
		},
		"account_id": schema.Int64Attribute{
			Optional:    true, // either rg_id or account_id must be specified
			Computed:    true,
			Description: "account id, used for creating vins in account",
		},

		"ipcidr": schema.StringAttribute{
			Optional:    true,
			Description: "private network IP CIDR, used for creating vins either in resource group or in account",
		},
		"pre_reservations_num": schema.Int64Attribute{
			Optional:    true,
			Computed:    true,
			Description: "number of pre created reservations, used for creating vins either in resource group or in account",
			// default is 32
		},
		"desc": schema.StringAttribute{
			Optional:    true,
			Computed:    true,
			Description: "description, used for creating vins either in resource group or in account",
			// default is ""
		},
		"dns": schema.SetAttribute{
			Optional:    true,
			ElementType: types.StringType,
			Description: "list of DNS ip address",
		},

		"gid": schema.Int64Attribute{
			Optional:    true,
			Computed:    true,
			Description: "grid (platform) ID, used for creating vins in account",
		},

		"enable": schema.BoolAttribute{
			Optional:    true,
			Computed:    true,
			Default:     booldefault.StaticBool(true),
			Description: "flag to enable/disable vins",
			// default is true
		},
		"permanently": schema.BoolAttribute{
			Optional:    true,
			Computed:    true,
			Default:     booldefault.StaticBool(true),
			Description: "flag to delete vins permanently",
			// default is true
		},
		"force": schema.BoolAttribute{
			Optional:    true,
			Computed:    true,
			Default:     booldefault.StaticBool(true),
			Description: "flag to force delete of non-empty vins",
			// default is true
		},
		"restore": schema.BoolAttribute{
			Optional:    true,
			Computed:    true,
			Default:     booldefault.StaticBool(true),
			Description: "flag to restore vins",
			// default is true
		},
		"vnfdev_restart": schema.BoolAttribute{
			Optional:    true,
			Description: "flag to restart vnfdev",
			// default is false
		},
		"vnfdev_redeploy": schema.BoolAttribute{
			Optional:    true,
			Description: "flag to redeploy vnfdev",
			// default is false
		},

		"ext_net": schema.SingleNestedAttribute{
			Optional:    true,
			Description: "connect/disconnect vins to/from external network",
			Attributes: map[string]schema.Attribute{
				"ext_net_id": schema.Int64Attribute{
					Optional:    true,
					Description: "external network ID",
					// default is -1
				},
				"ext_net_ip": schema.StringAttribute{
					Optional:    true,
					Description: "directly set IP address",
					// default is ""
				},
			},
		},
		"ip": schema.ListNestedAttribute{
			Optional:    true,
			Description: "ip reserve/release",
			NestedObject: schema.NestedAttributeObject{
				Attributes: map[string]schema.Attribute{
					"type": schema.StringAttribute{
						Required:    true,
						Description: "type of the reservation",
						Validators: []validator.String{
							stringvalidator.OneOfCaseInsensitive("DHCP", "VIP", "EXCLUDED"), // case is ignored
						},
					},
					"ip_addr": schema.StringAttribute{
						Optional:    true,
						Description: "IP address to use. Non-empty string is required for type EXCLUDE. Ignored for types DHCP and VIP.",
					},
					"mac_addr": schema.StringAttribute{
						Optional:    true,
						Description: "MAC address to associate with IP reservation. Ignored for type EXCLUDE, non-empty string is required for DHCP and VIP.",
					},
					"compute_id": schema.Int64Attribute{
						Optional:    true,
						Description: "ID of the compute, associated with this reservation of type DHCP. Ignored for other types.",
					},
				},
			},
		},
		"nat_rule": schema.ListNestedAttribute{
			Optional:    true,
			Description: "create/delete NAT (port forwarding rule) on vins",
			NestedObject: schema.NestedAttributeObject{
				Attributes: map[string]schema.Attribute{
					"int_ip": schema.StringAttribute{
						Required:    true,
						Description: "internal IP address to apply this rule to",
					},
					"int_port": schema.Int64Attribute{
						Optional:    true,
						Computed:    true,
						Description: "internal IP port number to use for this rule",
					},
					"ext_port_start": schema.Int64Attribute{
						Required:    true,
						Description: "external IP start port to use for this rule",
					},
					"ext_port_end": schema.Int64Attribute{
						Optional:    true,
						Computed:    true,
						Description: "external IP end port to use for this rule",
					},
					"proto": schema.StringAttribute{
						Optional:    true,
						Computed:    true,
						Description: "IP protocol type (tcp or udp)",
						Validators: []validator.String{
							stringvalidator.OneOf("tcp", "udp"), // case is not ignored
						},
					},
					"rule_id": schema.Int64Attribute{
						Computed:    true,
						Description: "rule id",
					},
				},
			},
		},

		// computed attributes
		"id": schema.StringAttribute{
			Computed: true,
			PlanModifiers: []planmodifier.String{
				stringplanmodifier.UseStateForUnknown(),
			},
		},
		"vins_id": schema.Int64Attribute{
			Computed:    true,
			Description: "Unique ID of the ViNS. If ViNS ID is specified, then ViNS name, rg_id and account_id are ignored.",
		},
		"last_updated": schema.StringAttribute{
			Computed: true,
		},
		"vnf_dev": schema.SingleNestedAttribute{
			Computed: true,
			Attributes: map[string]schema.Attribute{
				"ckey": schema.StringAttribute{
					Computed: true,
				},
				"account_id": schema.Int64Attribute{
					Computed: true,
				},
				"capabilities": schema.ListAttribute{
					Computed:    true,
					ElementType: types.StringType,
				},
				"config": schema.SingleNestedAttribute{
					Computed: true,
					Attributes: map[string]schema.Attribute{
						"mgmt": schema.SingleNestedAttribute{
							Computed: true,
							Attributes: map[string]schema.Attribute{
								"ip_addr": schema.StringAttribute{
									Computed: true,
								},
								"password": schema.StringAttribute{
									Computed: true,
								},
								"ssh_key": schema.StringAttribute{
									Computed: true,
								},
								"user": schema.StringAttribute{
									Computed: true,
								},
							},
						},
						"resources": schema.SingleNestedAttribute{
							Computed: true,
							Attributes: map[string]schema.Attribute{
								"cpu": schema.Int64Attribute{
									Computed: true,
								},
								"ram": schema.Int64Attribute{
									Computed: true,
								},
								"stack_id": schema.Int64Attribute{
									Computed: true,
								},
								"uuid": schema.StringAttribute{
									Computed: true,
								},
							},
						},
					},
				},
				"config_saved": schema.BoolAttribute{
					Computed: true,
				},
				"custom_pre_cfg": schema.BoolAttribute{
					Computed: true,
				},
				"desc": schema.StringAttribute{
					Computed: true,
				},
				"gid": schema.Int64Attribute{
					Computed: true,
				},
				"guid": schema.Int64Attribute{
					Computed: true,
				},
				"vnf_id": schema.Int64Attribute{
					Computed: true,
				},
				"interfaces": schema.ListNestedAttribute{
					Computed: true,
					NestedObject: schema.NestedAttributeObject{
						Attributes: map[string]schema.Attribute{
							"bus_number": schema.Int64Attribute{
								Computed: true,
							},
							"conn_id": schema.Int64Attribute{
								Computed: true,
							},
							"conn_type": schema.StringAttribute{
								Computed: true,
							},
							"def_gw": schema.StringAttribute{
								Computed: true,
							},
							"enabled": schema.BoolAttribute{
								Computed: true,
							},
							"flipgroup_id": schema.Int64Attribute{
								Computed: true,
							},
							"guid": schema.StringAttribute{
								Computed: true,
							},
							"ip_address": schema.StringAttribute{
								Computed: true,
							},
							"libvirt_settings": schema.SingleNestedAttribute{
								Computed: true,
								Attributes: map[string]schema.Attribute{
									"txmode": schema.StringAttribute{
										Computed: true,
									},
									"ioeventfd": schema.StringAttribute{
										Computed: true,
									},
									"event_idx": schema.StringAttribute{
										Computed: true,
									},
									"queues": schema.Int64Attribute{
										Computed: true,
									},
									"rx_queue_size": schema.Int64Attribute{
										Computed: true,
									},
									"tx_queue_size": schema.Int64Attribute{
										Computed: true,
									},
									"guid": schema.StringAttribute{
										Computed: true,
									},
								},
							},
							"listen_ssh": schema.BoolAttribute{
								Computed: true,
							},
							"mac": schema.StringAttribute{
								Computed: true,
							},
							"mtu": schema.Int64Attribute{
								Computed: true,
							},
							"name": schema.StringAttribute{
								Computed: true,
							},
							"net_id": schema.Int64Attribute{
								Computed: true,
							},
							"net_mask": schema.Int64Attribute{
								Computed: true,
							},
							"net_type": schema.StringAttribute{
								Computed: true,
							},
							"node_id": schema.Int64Attribute{
								Computed: true,
							},
							"pci_slot": schema.Int64Attribute{
								Computed: true,
							},
							"qos": schema.SingleNestedAttribute{
								Computed: true,
								Attributes: map[string]schema.Attribute{
									"e_rate": schema.Int64Attribute{
										Computed: true,
									},
									"guid": schema.StringAttribute{
										Computed: true,
									},
									"in_burst": schema.Int64Attribute{
										Computed: true,
									},
									"in_rate": schema.Int64Attribute{
										Computed: true,
									},
								},
							},
							"target": schema.StringAttribute{
								Computed: true,
							},
							"type": schema.StringAttribute{
								Computed: true,
							},
							"vnfs": schema.ListAttribute{
								Computed:    true,
								ElementType: types.Int64Type,
							},
						},
					},
				},
				"lock_status": schema.StringAttribute{
					Computed: true,
				},
				"milestones": schema.Int64Attribute{
					Computed: true,
				},
				"vnf_name": schema.StringAttribute{
					Computed: true,
				},
				"vnc_password": schema.StringAttribute{
					Computed: true,
				},
				"status": schema.StringAttribute{
					Computed: true,
				},
				"tech_status": schema.StringAttribute{
					Computed: true,
				},
				"type": schema.StringAttribute{
					Computed: true,
				},
				"vins": schema.ListAttribute{
					Computed:    true,
					ElementType: types.Int64Type,
				},
			},
		},
		"account_name": schema.StringAttribute{
			Computed: true,
		},
		"computes": schema.ListNestedAttribute{
			Computed: true,
			NestedObject: schema.NestedAttributeObject{
				Attributes: map[string]schema.Attribute{
					"compute_id": schema.Int64Attribute{
						Computed: true,
					},
					"compute_name": schema.StringAttribute{
						Computed: true,
					},
				},
			},
		},
		"created_by": schema.StringAttribute{
			Computed: true,
		},
		"created_time": schema.Int64Attribute{
			Computed: true,
		},
		"default_gw": schema.StringAttribute{
			Computed: true,
		},
		"default_qos": schema.SingleNestedAttribute{
			Computed: true,
			Attributes: map[string]schema.Attribute{
				"e_rate": schema.Int64Attribute{
					Computed: true,
				},
				"guid": schema.StringAttribute{
					Computed: true,
				},
				"in_burst": schema.Int64Attribute{
					Computed: true,
				},
				"in_rate": schema.Int64Attribute{
					Computed: true,
				},
			},
		},
		"deleted_by": schema.StringAttribute{
			Computed: true,
		},
		"deleted_time": schema.Int64Attribute{
			Computed: true,
		},
		"guid": schema.Int64Attribute{
			Computed: true,
		},
		"lock_status": schema.StringAttribute{
			Computed: true,
		},
		"manager_id": schema.Int64Attribute{
			Computed: true,
		},
		"manager_type": schema.StringAttribute{
			Computed: true,
		},
		"milestones": schema.Int64Attribute{
			Computed: true,
		},
		"net_mask": schema.Int64Attribute{
			Computed: true,
		},
		"network": schema.StringAttribute{
			Computed: true,
		},
		"redundant": schema.BoolAttribute{
			Computed: true,
		},
		"rg_name": schema.StringAttribute{
			Computed: true,
		},
		"sec_vnf_dev_id": schema.Int64Attribute{
			Computed: true,
		},
		"status": schema.StringAttribute{
			Computed: true,
		},
		"updated_by": schema.StringAttribute{
			Computed: true,
		},
		"updated_time": schema.Int64Attribute{
			Computed: true,
		},
		"user_managed": schema.BoolAttribute{
			Computed: true,
		},
		"vnfs": schema.SingleNestedAttribute{
			Computed: true,
			Attributes: map[string]schema.Attribute{
				"dhcp": schema.SingleNestedAttribute{
					Computed: true,
					Attributes: map[string]schema.Attribute{
						"ckey": schema.StringAttribute{
							Computed: true,
						},
						"account_id": schema.Int64Attribute{
							Computed: true,
						},
						"config": schema.SingleNestedAttribute{
							Computed: true,
							Attributes: map[string]schema.Attribute{
								"default_gw": schema.StringAttribute{
									Computed: true,
								},
								"dns": schema.ListAttribute{
									Computed:    true,
									ElementType: types.StringType,
								},
								"ip_end": schema.StringAttribute{
									Computed: true,
								},
								"ip_start": schema.StringAttribute{
									Computed: true,
								},
								"lease": schema.Int64Attribute{
									Computed: true,
								},
								"netmask": schema.Int64Attribute{
									Computed: true,
								},
								"network": schema.StringAttribute{
									Computed: true,
								},
								"reservations": schema.ListNestedAttribute{
									Computed: true,
									NestedObject: schema.NestedAttributeObject{
										Attributes: map[string]schema.Attribute{
											"account_id": schema.Int64Attribute{
												Computed: true,
											},
											"ip": schema.StringAttribute{
												Computed: true,
											},
											"mac": schema.StringAttribute{
												Computed: true,
											},
											"type": schema.StringAttribute{
												Computed: true,
											},
											"vm_id": schema.Int64Attribute{
												Computed: true,
											},
										},
									},
								},
							},
						},
						"created_time": schema.Int64Attribute{
							Computed: true,
						},
						"devices": schema.SingleNestedAttribute{
							Computed: true,
							Attributes: map[string]schema.Attribute{
								"primary": schema.SingleNestedAttribute{
									Computed: true,
									Attributes: map[string]schema.Attribute{
										"dev_id": schema.Int64Attribute{
											Computed: true,
										},
										"iface01": schema.StringAttribute{
											Computed: true,
										},
										"iface02": schema.StringAttribute{
											Computed: true,
										},
									},
								},
							},
						},
						"gid": schema.Int64Attribute{
							Computed: true,
						},
						"guid": schema.Int64Attribute{
							Computed: true,
						},
						"dhcp_id": schema.Int64Attribute{
							Computed: true,
						},
						"lock_status": schema.StringAttribute{
							Computed: true,
						},
						"milestones": schema.Int64Attribute{
							Computed: true,
						},
						"owner_id": schema.Int64Attribute{
							Computed: true,
						},
						"owner_type": schema.StringAttribute{
							Computed: true,
						},
						"pure_virtual": schema.BoolAttribute{
							Computed: true,
						},
						"routes": schema.ListNestedAttribute{
							Computed: true,
							NestedObject: schema.NestedAttributeObject{
								Attributes: map[string]schema.Attribute{
									"route_id": schema.Int64Attribute{
										Computed: true,
									},
									"compute_ids": schema.ListAttribute{
										Computed:    true,
										ElementType: types.Int64Type,
									},
									"destination": schema.StringAttribute{
										Computed: true,
									},
									"gateway": schema.StringAttribute{
										Computed: true,
									},
									"guid": schema.StringAttribute{
										Computed: true,
									},
									"netmask": schema.StringAttribute{
										Computed: true,
									},
								},
							},
						},
						"status": schema.StringAttribute{
							Computed: true,
						},
						"tech_status": schema.StringAttribute{
							Computed: true,
						},
						"type": schema.StringAttribute{
							Computed: true,
						},
					},
				},
				"gw": schema.SingleNestedAttribute{
					Computed: true,
					Attributes: map[string]schema.Attribute{
						"ckey": schema.StringAttribute{
							Computed: true,
						},
						"account_id": schema.Int64Attribute{
							Computed: true,
						},
						"config": schema.SingleNestedAttribute{
							Computed: true,
							Attributes: map[string]schema.Attribute{
								"default_gw": schema.StringAttribute{
									Computed: true,
								},
								"ext_net_id": schema.Int64Attribute{
									Computed: true,
								},
								"ext_net_ip": schema.StringAttribute{
									Computed: true,
								},
								"ext_netmask": schema.Int64Attribute{
									Computed: true,
								},
								"qos": schema.SingleNestedAttribute{
									Computed: true,
									Attributes: map[string]schema.Attribute{
										"e_rate": schema.Int64Attribute{
											Computed: true,
										},
										"guid": schema.StringAttribute{
											Computed: true,
										},
										"in_burst": schema.Int64Attribute{
											Computed: true,
										},
										"in_rate": schema.Int64Attribute{
											Computed: true,
										},
									},
								},
							},
						},
						"created_time": schema.Int64Attribute{
							Computed: true,
						},
						"devices": schema.SingleNestedAttribute{
							Computed: true,
							Attributes: map[string]schema.Attribute{
								"primary": schema.SingleNestedAttribute{
									Computed: true,
									Attributes: map[string]schema.Attribute{
										"dev_id": schema.Int64Attribute{
											Computed: true,
										},
										"iface01": schema.StringAttribute{
											Computed: true,
										},
										"iface02": schema.StringAttribute{
											Computed: true,
										},
									},
								},
							},
						},
						"gid": schema.Int64Attribute{
							Computed: true,
						},
						"guid": schema.Int64Attribute{
							Computed: true,
						},
						"gw_id": schema.Int64Attribute{
							Computed: true,
						},
						"lock_status": schema.StringAttribute{
							Computed: true,
						},
						"milestones": schema.Int64Attribute{
							Computed: true,
						},
						"owner_id": schema.Int64Attribute{
							Computed: true,
						},
						"owner_type": schema.StringAttribute{
							Computed: true,
						},
						"pure_virtual": schema.BoolAttribute{
							Computed: true,
						},
						"routes": schema.ListNestedAttribute{
							Computed: true,
							NestedObject: schema.NestedAttributeObject{
								Attributes: map[string]schema.Attribute{
									"compute_ids": schema.ListAttribute{
										Computed:    true,
										ElementType: types.Int64Type,
									},
									"route_id": schema.Int64Attribute{
										Computed: true,
									},
									"guid": schema.StringAttribute{
										Computed: true,
									},
									"destination": schema.StringAttribute{
										Computed: true,
									},
									"netmask": schema.StringAttribute{
										Computed: true,
									},
									"gateway": schema.StringAttribute{
										Computed: true,
									},
								},
							},
						},
						"status": schema.StringAttribute{
							Computed: true,
						},
						"tech_status": schema.StringAttribute{
							Computed: true,
						},
						"type": schema.StringAttribute{
							Computed: true,
						},
					},
				},
				"nat": schema.SingleNestedAttribute{
					Computed: true,
					Attributes: map[string]schema.Attribute{
						"ckey": schema.StringAttribute{
							Computed: true,
						},
						"account_id": schema.Int64Attribute{
							Computed: true,
						},
						"config": schema.SingleNestedAttribute{
							Computed: true,
							Attributes: map[string]schema.Attribute{
								"net_mask": schema.Int64Attribute{
									Computed: true,
								},
								"network": schema.StringAttribute{
									Computed: true,
								},
								"rules": schema.ListNestedAttribute{
									Computed: true,
									NestedObject: schema.NestedAttributeObject{
										Attributes: map[string]schema.Attribute{
											"rule_id": schema.Int64Attribute{
												Computed: true,
											},
											"local_ip": schema.StringAttribute{
												Computed: true,
											},
											"local_port": schema.Int64Attribute{
												Computed: true,
											},
											"protocol": schema.StringAttribute{
												Computed: true,
											},
											"public_port_end": schema.Int64Attribute{
												Computed: true,
											},
											"public_port_start": schema.Int64Attribute{
												Computed: true,
											},
											"vm_id": schema.Int64Attribute{
												Computed: true,
											},
											"vm_name": schema.StringAttribute{
												Computed: true,
											},
										},
									},
								},
							},
						},
						"created_time": schema.Int64Attribute{
							Computed: true,
						},
						"devices": schema.SingleNestedAttribute{
							Computed: true,
							Attributes: map[string]schema.Attribute{
								"primary": schema.SingleNestedAttribute{
									Computed: true,
									Attributes: map[string]schema.Attribute{
										"dev_id": schema.Int64Attribute{
											Computed: true,
										},
										"iface01": schema.StringAttribute{
											Computed: true,
										},
										"iface02": schema.StringAttribute{
											Computed: true,
										},
									},
								},
							},
						},
						"gid": schema.Int64Attribute{
							Computed: true,
						},
						"guid": schema.Int64Attribute{
							Computed: true,
						},
						"nat_id": schema.Int64Attribute{
							Computed: true,
						},
						"lock_status": schema.StringAttribute{
							Computed: true,
						},
						"milestones": schema.Int64Attribute{
							Computed: true,
						},
						"owner_id": schema.Int64Attribute{
							Computed: true,
						},
						"owner_type": schema.StringAttribute{
							Computed: true,
						},
						"pure_virtual": schema.BoolAttribute{
							Computed: true,
						},
						"routes": schema.ListNestedAttribute{
							Computed: true,
							NestedObject: schema.NestedAttributeObject{
								Attributes: map[string]schema.Attribute{
									"compute_ids": schema.ListAttribute{
										Computed:    true,
										ElementType: types.Int64Type,
									},
									"route_id": schema.Int64Attribute{
										Computed: true,
									},
									"guid": schema.StringAttribute{
										Computed: true,
									},
									"destination": schema.StringAttribute{
										Computed: true,
									},
									"netmask": schema.StringAttribute{
										Computed: true,
									},
									"gateway": schema.StringAttribute{
										Computed: true,
									},
								},
							},
						},
						"status": schema.StringAttribute{
							Computed: true,
						},
						"tech_status": schema.StringAttribute{
							Computed: true,
						},
						"type": schema.StringAttribute{
							Computed: true,
						},
					},
				},
			},
		},
		"vxlan_id": schema.Int64Attribute{
			Computed: true,
		},
	}
}
