package segments

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

func l2ExternalNetworkSchema() map[string]*schema.Schema {
	return map[string]*schema.Schema{
		"id": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"display_name": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"description": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"bridge_network_name": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"hypervisors": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Schema{
				Type: schema.TypeString,
			},
		},
		"vlan_tag": {
			Type:     schema.TypeInt,
			Computed: true,
		},
		"version_id": {
			Type:     schema.TypeInt,
			Computed: true,
		},
		"created_at": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"created_by": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"updated_at": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"updated_by": {
			Type:     schema.TypeString,
			Computed: true,
		},
	}
}

func l2ConnectionPortSchema() map[string]*schema.Schema {
	return map[string]*schema.Schema{
		"id": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"access_group_id": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"version_id": {
			Type:     schema.TypeInt,
			Computed: true,
		},
		"created_at": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"created_by": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"updated_at": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"updated_by": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"l2_external_network": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Resource{
				Schema: l2ExternalNetworkSchema(),
			},
		},
	}
}

func dataSourceSegmentSchemaMake() map[string]*schema.Schema {
	sch := map[string]*schema.Schema{
		"segment_id": {
			Type:     schema.TypeString,
			Required: true,
		},
		"access_group_id": {
			Type:     schema.TypeString,
			Optional: true,
			Computed: true,
		},
		"access_group_name": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"created_at": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"description": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"dhcp_v4": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"dns": {
						Type:     schema.TypeList,
						Computed: true,
						Elem: &schema.Schema{
							Type: schema.TypeString,
						},
					},
					"excluded_address_ranges": {
						Type:     schema.TypeList,
						Computed: true,
						Elem: &schema.Schema{
							Type: schema.TypeString,
						},
					},
					"gateway": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"id": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"lease_time": {
						Type:     schema.TypeInt,
						Computed: true,
					},
					"server_ip": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"server_mac": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"enabled": {
						Type:     schema.TypeBool,
						Computed: true,
					},
				},
			},
		},
		"dhcp_v6": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"address_prefix": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"dns": {
						Type:     schema.TypeList,
						Computed: true,
						Elem: &schema.Schema{
							Type: schema.TypeString,
						},
					},
					"id": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"lease_time": {
						Type:     schema.TypeInt,
						Computed: true,
					},
					"server_mac": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"enabled": {
						Type:     schema.TypeBool,
						Computed: true,
					},
				},
			},
		},
		"display_name": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"enabled": {
			Type:     schema.TypeBool,
			Computed: true,
		},
		"l2_connection_port": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Resource{
				Schema: l2ConnectionPortSchema(),
			},
		},
		"logical_ports_info": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"display_name": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"id": {
						Type:     schema.TypeString,
						Computed: true,
					},
				},
			},
		},
		"routers_info": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"display_name": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"id": {
						Type:     schema.TypeString,
						Computed: true,
					},
				},
			},
		},
		"status": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"operation_status": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"hypervisors": {
						Type:     schema.TypeList,
						Computed: true,
						Elem: &schema.Resource{
							Schema: map[string]*schema.Schema{
								"operation_status": {
									Type:     schema.TypeString,
									Computed: true,
								},
								"name": {
									Type:     schema.TypeString,
									Computed: true,
								},
								"display_name": {
									Type:     schema.TypeString,
									Computed: true,
								},
								"hypervisor_status": {
									Type:     schema.TypeString,
									Computed: true,
								},
								"synced_at": {
									Type:     schema.TypeString,
									Computed: true,
								},
							},
						},
					},
				},
			},
		},
		"subnet_v4": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"subnet_v6": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"type": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"updated_at": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"version_id": {
			Type:     schema.TypeInt,
			Computed: true,
		},
	}
	return sch
}

func dataSourceSegmentListSchemaMake() 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", "subnet", "created_at", "updated_at"}, false),
			Description:  "sort by one of supported fields",
		},
		"sort_order": {
			Type:         schema.TypeString,
			Optional:     true,
			ValidateFunc: validation.StringInSlice([]string{"asc", "dec"}, false),
			Description:  "sort order",
		},
		"enabled": {
			Type:        schema.TypeBool,
			Optional:    true,
			Description: "find by enabled status",
		},
		"is_synced": {
			Type:        schema.TypeBool,
			Optional:    true,
			Description: "does core currently believe that its data is synchronized with the data in the OVN?",
		},
		"display_name": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "find by display name",
		},
		"subnet": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "IPv4 or IPv6 subnet for the current segment",
		},
		"access_group_id": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "find by access group id",
		},
		"created_from": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "find by created date",
		},
		"created_to": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "find by created date",
		},
		"updated_from": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "find by updated date",
		},
		"updated_to": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "find by updated date",
		},
		"operation_status": {
			Type:         schema.TypeString,
			Optional:     true,
			ValidateFunc: validation.StringInSlice([]string{"Idle", "SynchronizingAtCore", "SynchronizingAtOVN", "Synchronized", "NoHypervisorAtOVN", "FailedAtCore", "TemporaryFailedAtCore"}, false),
			Description:  "filter by operation status",
		},
		"items": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"id": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"access_group_id": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"access_group_name": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"created_at": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"description": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"dhcp_v4": {
						Type:     schema.TypeList,
						Computed: true,
						Elem: &schema.Resource{
							Schema: map[string]*schema.Schema{
								"dns": {
									Type:     schema.TypeList,
									Computed: true,
									Elem: &schema.Schema{
										Type: schema.TypeString,
									},
								},
								"excluded_address_ranges": {
									Type:     schema.TypeList,
									Computed: true,
									Elem: &schema.Schema{
										Type: schema.TypeString,
									},
								},
								"gateway": {
									Type:     schema.TypeString,
									Computed: true,
								},
								"id": {
									Type:     schema.TypeString,
									Computed: true,
								},
								"lease_time": {
									Type:     schema.TypeInt,
									Computed: true,
								},
								"server_ip": {
									Type:     schema.TypeString,
									Computed: true,
								},
								"server_mac": {
									Type:     schema.TypeString,
									Computed: true,
								},
								"enabled": {
									Type:     schema.TypeBool,
									Computed: true,
								},
							},
						},
					},
					"dhcp_v6": {
						Type:     schema.TypeList,
						Computed: true,
						Elem: &schema.Resource{
							Schema: map[string]*schema.Schema{
								"address_prefix": {
									Type:     schema.TypeString,
									Computed: true,
								},
								"dns": {
									Type:     schema.TypeList,
									Computed: true,
									Elem: &schema.Schema{
										Type: schema.TypeString,
									},
								},
								"id": {
									Type:     schema.TypeString,
									Computed: true,
								},
								"lease_time": {
									Type:     schema.TypeInt,
									Computed: true,
								},
								"server_mac": {
									Type:     schema.TypeString,
									Computed: true,
								},
								"enabled": {
									Type:     schema.TypeBool,
									Computed: true,
								},
							},
						},
					},
					"display_name": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"enabled": {
						Type:     schema.TypeBool,
						Computed: true,
					},
					"l2_connection_port": {
						Type:     schema.TypeList,
						Computed: true,
						Elem: &schema.Resource{
							Schema: l2ConnectionPortSchema(),
						},
					},
					"logical_ports_info": {
						Type:     schema.TypeList,
						Computed: true,
						Elem: &schema.Resource{
							Schema: map[string]*schema.Schema{
								"display_name": {
									Type:     schema.TypeString,
									Computed: true,
								},
								"id": {
									Type:     schema.TypeString,
									Computed: true,
								},
							},
						},
					},
					"routers_info": {
						Type:     schema.TypeList,
						Computed: true,
						Elem: &schema.Resource{
							Schema: map[string]*schema.Schema{
								"display_name": {
									Type:     schema.TypeString,
									Computed: true,
								},
								"id": {
									Type:     schema.TypeString,
									Computed: true,
								},
							},
						},
					},
					"status": {
						Type:     schema.TypeList,
						Computed: true,
						Elem: &schema.Resource{
							Schema: map[string]*schema.Schema{
								"operation_status": {
									Type:     schema.TypeString,
									Computed: true,
								},
								"hypervisors": {
									Type:     schema.TypeList,
									Computed: true,
									Elem: &schema.Resource{
										Schema: map[string]*schema.Schema{
											"operation_status": {
												Type:     schema.TypeString,
												Computed: true,
											},
											"name": {
												Type:     schema.TypeString,
												Computed: true,
											},
											"display_name": {
												Type:     schema.TypeString,
												Computed: true,
											},
											"hypervisor_status": {
												Type:     schema.TypeString,
												Computed: true,
											},
											"synced_at": {
												Type:     schema.TypeString,
												Computed: true,
											},
										},
									},
								},
							},
						},
					},
					"subnet_v4": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"subnet_v6": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"type": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"updated_at": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"version_id": {
						Type:     schema.TypeInt,
						Computed: true,
					},
				},
			},
		},
		"entry_count": {
			Type:        schema.TypeInt,
			Computed:    true,
			Description: "entry count",
		},
	}
	return res
}

func resourceSegmentSchemaMake() map[string]*schema.Schema {
	res := map[string]*schema.Schema{
		"access_group_id": {
			Type:     schema.TypeString,
			Required: true,
		},
		"description": {
			Type:     schema.TypeString,
			Required: true,
		},
		"display_name": {
			Type:     schema.TypeString,
			Required: true,
		},
		"enabled": {
			Type:     schema.TypeBool,
			Required: true,
		},
		"force": {
			Type:     schema.TypeBool,
			Optional: true,
		},
		"subnet_v4": {
			Type:     schema.TypeString,
			Optional: true,
			Computed: true,
		},
		"subnet_v6": {
			Type:     schema.TypeString,
			Optional: true,
			Computed: true,
		},
		"dhcp_v4": {
			Type:     schema.TypeList,
			Optional: true,
			Computed: true,
			MaxItems: 1,
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"dns": {
						Type:     schema.TypeList,
						Optional: true,
						Computed: true,
						Elem: &schema.Schema{
							Type: schema.TypeString,
						},
					},
					"excluded_address_ranges": {
						Type:     schema.TypeList,
						Optional: true,
						Computed: true,
						Elem: &schema.Schema{
							Type: schema.TypeString,
						},
					},
					"gateway": {
						Type:     schema.TypeString,
						Required: true,
					},
					"id": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"lease_time": {
						Type:     schema.TypeInt,
						Optional: true,
						Computed: true,
					},
					"server_ip": {
						Type:     schema.TypeString,
						Required: true,
					},
					"server_mac": {
						Type:     schema.TypeString,
						Optional: true,
						Computed: true,
					},
					"enabled": {
						Type:     schema.TypeBool,
						Required: true,
					},
				},
			},
		},
		"dhcp_v6": {
			Type:     schema.TypeList,
			Optional: true,
			Computed: true,
			MaxItems: 1,
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"address_prefix": {
						Type:     schema.TypeString,
						Required: true,
					},
					"dns": {
						Type:     schema.TypeList,
						Optional: true,
						Computed: true,
						Elem: &schema.Schema{
							Type: schema.TypeString,
						},
					},
					"id": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"lease_time": {
						Type:     schema.TypeInt,
						Optional: true,
						Computed: true,
					},
					"server_mac": {
						Type:     schema.TypeString,
						Optional: true,
						Computed: true,
					},
					"enabled": {
						Type:     schema.TypeBool,
						Required: true,
					},
				},
			},
		},
		"access_group_name": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"created_at": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"l2_connection_port": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Resource{
				Schema: l2ConnectionPortSchema(),
			},
		},
		"logical_ports_info": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"display_name": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"id": {
						Type:     schema.TypeString,
						Computed: true,
					},
				},
			},
		},
		"routers_info": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"display_name": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"id": {
						Type:     schema.TypeString,
						Computed: true,
					},
				},
			},
		},
		"status": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"operation_status": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"hypervisors": {
						Type:     schema.TypeList,
						Computed: true,
						Elem: &schema.Resource{
							Schema: map[string]*schema.Schema{
								"operation_status": {
									Type:     schema.TypeString,
									Computed: true,
								},
								"name": {
									Type:     schema.TypeString,
									Computed: true,
								},
								"display_name": {
									Type:     schema.TypeString,
									Computed: true,
								},
								"hypervisor_status": {
									Type:     schema.TypeString,
									Computed: true,
								},
								"synced_at": {
									Type:     schema.TypeString,
									Computed: true,
								},
							},
						},
					},
				},
			},
		},
		"type": {
			Type:     schema.TypeString,
			Optional: true,
			Computed: true,
		},
		"updated_at": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"version_id": {
			Type:     schema.TypeInt,
			Computed: true,
		},
	}
	return res
}
