package image

import (
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/statefuncs"
)

func resourceImageFromBlankComputeSchemaV1() *schema.Resource {
	return &schema.Resource{
		Schema: map[string]*schema.Schema{
			"compute_id": {
				Type:        schema.TypeInt,
				Required:    true,
				Description: "Compute Id",
			},
			"name": {
				Type:        schema.TypeString,
				Required:    true,
				Description: "Name of the rescue disk",
			},
			"boot_type": {
				Type:         schema.TypeString,
				Required:     true,
				ValidateFunc: validation.StringInSlice([]string{"bios", "uefi"}, true),
				Description:  "Boot type of image BIOS or UEFI",
			},
			"type": {
				Type:         schema.TypeString,
				Required:     true,
				ValidateFunc: validation.StringInSlice([]string{"linux", "windows", "other"}, true),
				Description:  "Image type linux, windows or other",
			},

			"username": {
				Type:        schema.TypeString,
				Optional:    true,
				Computed:    true,
				Description: "Optional username for the image",
			},
			"password": {
				Type:        schema.TypeString,
				Optional:    true,
				Computed:    true,
				Description: "Optional password for the image",
			},
			"account_id": {
				Type:        schema.TypeInt,
				Optional:    true,
				Computed:    true,
				Description: "AccountId to make the image exclusive",
			},
			"sep_id": {
				Type:        schema.TypeInt,
				Optional:    true,
				Computed:    true,
				Description: "storage endpoint provider ID",
			},
			"pool_name": {
				Type:        schema.TypeString,
				Optional:    true,
				Computed:    true,
				Description: "pool for image create",
			},
			"hot_resize": {
				Type:        schema.TypeBool,
				Optional:    true,
				Computed:    true,
				Description: "Does this machine supports hot resize",
			},
			"async_mode": {
				Type:        schema.TypeBool,
				Optional:    true,
				Default:     false,
				Description: "create an image in async/sync mode",
			},

			"image_id": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"unc_path": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"ckey": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"acl": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"architecture": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"bootable": {
				Type:     schema.TypeBool,
				Computed: true,
			},
			"compute_ci_id": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"cd_presented_to": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"deleted_time": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"desc": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"drivers": {
				Type:     schema.TypeList,
				Computed: true,
				Elem: &schema.Schema{
					Type: schema.TypeString,
				},
			},
			"enabled": {
				Type:     schema.TypeBool,
				Computed: true,
			},
			"gid": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"guid": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"history": {
				Type:     schema.TypeList,
				Computed: true,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"guid": {
							Type:     schema.TypeString,
							Computed: true,
						},
						"id": {
							Type:     schema.TypeInt,
							Computed: true,
						},
						"timestamp": {
							Type:     schema.TypeInt,
							Computed: true,
						},
					},
				},
			},
			"last_modified": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"link_to": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"milestones": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"image_name": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"network_interface_naming": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"provider_name": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"purge_attempts": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"present_to": {
				Type:     schema.TypeList,
				Computed: true,
				Elem: &schema.Schema{
					Type: schema.TypeInt,
				},
			},
			"res_id": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"rescuecd": {
				Type:     schema.TypeBool,
				Computed: true,
			},
			"shared_with": {
				Type:     schema.TypeList,
				Computed: true,
				Elem: &schema.Schema{
					Type: schema.TypeInt,
				},
			},
			"size": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"status": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"tech_status": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"version": {
				Type:     schema.TypeString,
				Computed: true,
			},
		},
	}
}

func resourceImageFromBlankDiskSchemaV1() *schema.Resource {
	return &schema.Resource{
		Schema: map[string]*schema.Schema{
			"disk_id": {
				Type:        schema.TypeInt,
				Required:    true,
				Description: "Disk Id",
			},
			"name": {
				Type:        schema.TypeString,
				Required:    true,
				Description: "Name of the rescue disk",
			},
			"boot_type": {
				Type:         schema.TypeString,
				Required:     true,
				ValidateFunc: validation.StringInSlice([]string{"bios", "uefi"}, true),
				Description:  "Boot type of image BIOS or UEFI",
			},
			"type": {
				Type:         schema.TypeString,
				Required:     true,
				ValidateFunc: validation.StringInSlice([]string{"linux", "windows", "other"}, true),
				Description:  "Image type linux, windows or other",
			},
			"architecture": {
				Type:         schema.TypeString,
				Required:     true,
				ValidateFunc: validation.StringInSlice([]string{"X86_64"}, true),
				Description:  "binary architecture of this image, one of X86_64",
			},

			"username": {
				Type:        schema.TypeString,
				Optional:    true,
				Computed:    true,
				Description: "Optional username for the image",
			},
			"password": {
				Type:        schema.TypeString,
				Optional:    true,
				Computed:    true,
				Description: "Optional password for the image",
			},
			"account_id": {
				Type:        schema.TypeInt,
				Optional:    true,
				Computed:    true,
				Description: "AccountId to make the image exclusive",
			},
			"sep_id": {
				Type:        schema.TypeInt,
				Optional:    true,
				Computed:    true,
				Description: "storage endpoint provider ID",
			},
			"pool_name": {
				Type:        schema.TypeString,
				Optional:    true,
				Computed:    true,
				Description: "pool for image create",
			},
			"drivers": {
				Type:     schema.TypeList,
				Required: true,
				Elem: &schema.Schema{
					StateFunc:    statefuncs.StateFuncToUpper,
					ValidateFunc: validation.StringInSlice([]string{"SVA_KVM_X86", "KVM_X86"}, false), // observe case while validating
					Type:         schema.TypeString,
				},
				Description: "List of types of compute suitable for image. Example: [ \"KVM_X86\" ]",
			},
			"bootable": {
				Type:        schema.TypeBool,
				Optional:    true,
				Default:     true,
				Description: "bootable image",
			},
			"hot_resize": {
				Type:        schema.TypeBool,
				Optional:    true,
				Computed:    true,
				Description: "Does this machine supports hot resize",
			},
			"async_mode": {
				Type:        schema.TypeBool,
				Optional:    true,
				Default:     false,
				Description: "create an image in async/sync mode",
			},

			"image_id": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"unc_path": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"ckey": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"acl": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"compute_ci_id": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"cd_presented_to": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"deleted_time": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"desc": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"enabled": {
				Type:     schema.TypeBool,
				Computed: true,
			},
			"gid": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"guid": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"history": {
				Type:     schema.TypeList,
				Computed: true,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"guid": {
							Type:     schema.TypeString,
							Computed: true,
						},
						"id": {
							Type:     schema.TypeInt,
							Computed: true,
						},
						"timestamp": {
							Type:     schema.TypeInt,
							Computed: true,
						},
					},
				},
			},
			"last_modified": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"link_to": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"milestones": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"image_name": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"network_interface_naming": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"provider_name": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"purge_attempts": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"present_to": {
				Type:     schema.TypeList,
				Computed: true,
				Elem: &schema.Schema{
					Type: schema.TypeInt,
				},
			},
			"res_id": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"rescuecd": {
				Type:     schema.TypeBool,
				Computed: true,
			},
			"shared_with": {
				Type:     schema.TypeList,
				Computed: true,
				Elem: &schema.Schema{
					Type: schema.TypeInt,
				},
			},
			"size": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"status": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"tech_status": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"version": {
				Type:     schema.TypeString,
				Computed: true,
			},
		},
	}
}

func dataSourceImageExtendSchemaV1() map[string]*schema.Schema {
	return map[string]*schema.Schema{
		"image_id": {
			Type:     schema.TypeInt,
			Required: true,
		},
		"show_all": {
			Type:     schema.TypeBool,
			Default:  false,
			Optional: true,
		},
		"unc_path": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"ckey": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"account_id": {
			Type:     schema.TypeInt,
			Computed: true,
		},
		"acl": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"architecture": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"boot_type": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"bootable": {
			Type:     schema.TypeBool,
			Computed: true,
		},
		"compute_ci_id": {
			Type:     schema.TypeInt,
			Computed: true,
		},
		"cd_presented_to": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"deleted_time": {
			Type:     schema.TypeInt,
			Computed: true,
		},
		"desc": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"drivers": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Schema{
				Type: schema.TypeString,
			},
		},
		"enabled": {
			Type:     schema.TypeBool,
			Computed: true,
		},
		"gid": {
			Type:     schema.TypeInt,
			Computed: true,
		},
		"guid": {
			Type:     schema.TypeInt,
			Computed: true,
		},
		"history": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"guid": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"id": {
						Type:     schema.TypeInt,
						Computed: true,
					},
					"timestamp": {
						Type:     schema.TypeInt,
						Computed: true,
					},
				},
			},
		},
		"hot_resize": {
			Type:     schema.TypeBool,
			Computed: true,
		},
		"last_modified": {
			Type:     schema.TypeInt,
			Computed: true,
		},
		"link_to": {
			Type:     schema.TypeInt,
			Computed: true,
		},
		"milestones": {
			Type:     schema.TypeInt,
			Computed: true,
		},
		"image_name": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"network_interface_naming": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"password": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"pool_name": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"provider_name": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"purge_attempts": {
			Type:     schema.TypeInt,
			Computed: true,
		},
		"present_to": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Schema{
				Type: schema.TypeInt,
			},
		},
		"res_id": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"rescuecd": {
			Type:     schema.TypeBool,
			Computed: true,
		},
		"sep_id": {
			Type:     schema.TypeInt,
			Computed: true,
		},
		"shared_with": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Schema{
				Type: schema.TypeInt,
			},
		},
		"size": {
			Type:     schema.TypeInt,
			Computed: true,
		},
		"status": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"tech_status": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"type": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"username": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"version": {
			Type:     schema.TypeString,
			Computed: true,
		},
	}
}
func resourceMainImageSchemaV1(sch map[string]*schema.Schema) map[string]*schema.Schema {
	delete(sch, "show_all")
	sch["name"] = &schema.Schema{
		Type:        schema.TypeString,
		Required:    true,
		Description: "Name of the rescue disk",
	}

	sch["url"] = &schema.Schema{
		Type:        schema.TypeString,
		Required:    true,
		Description: "URL where to download media from",
	}

	sch["image_id"] = &schema.Schema{
		Type:        schema.TypeInt,
		Optional:    true,
		Computed:    true,
		Description: "image id",
	}

	sch["boot_type"] = &schema.Schema{
		Type:         schema.TypeString,
		Required:     true,
		ValidateFunc: validation.StringInSlice([]string{"bios", "uefi"}, true),
		Description:  "Boot type of image bios or uefi",
	}

	sch["type"] = &schema.Schema{
		Type:         schema.TypeString,
		Required:     true,
		ValidateFunc: validation.StringInSlice([]string{"linux", "windows", "other"}, true),
		Description:  "Image type linux, windows or other",
	}

	sch["hot_resize"] = &schema.Schema{
		Type:        schema.TypeBool,
		Optional:    true,
		Computed:    true,
		Description: "Does this machine supports hot resize",
	}

	sch["username"] = &schema.Schema{
		Type:        schema.TypeString,
		Optional:    true,
		Computed:    true,
		Description: "Optional username for the image",
	}

	sch["password"] = &schema.Schema{
		Type:        schema.TypeString,
		Optional:    true,
		Computed:    true,
		Description: "Optional password for the image",
	}

	sch["account_id"] = &schema.Schema{
		Type:        schema.TypeInt,
		Required:    true,
		Description: "AccountId to make the image exclusive",
	}

	sch["username_dl"] = &schema.Schema{
		Type:        schema.TypeString,
		Optional:    true,
		Description: "username for upload binary media",
	}

	sch["password_dl"] = &schema.Schema{
		Type:        schema.TypeString,
		Optional:    true,
		Description: "password for upload binary media",
	}

	sch["pool_name"] = &schema.Schema{
		Type:        schema.TypeString,
		Optional:    true,
		Computed:    true,
		Description: "pool for image create",
	}

	sch["sep_id"] = &schema.Schema{
		Type:        schema.TypeInt,
		Optional:    true,
		Computed:    true,
		Description: "storage endpoint provider ID",
	}

	sch["architecture"] = &schema.Schema{
		Type:         schema.TypeString,
		Optional:     true,
		Computed:     true,
		ValidateFunc: validation.StringInSlice([]string{"X86_64"}, true),
		Description:  "binary architecture of this image, one of X86_64",
	}

	sch["drivers"] = &schema.Schema{
		Type:     schema.TypeList,
		Required: true,
		Elem: &schema.Schema{
			Type: schema.TypeString,
		},
	}

	sch["network_interface_naming"] = &schema.Schema{
		Type:         schema.TypeString,
		Optional:     true,
		Computed:     true,
		ValidateFunc: validation.StringInSlice([]string{"eth", "ens"}, true),
		Description:  "select a network interface naming pattern for your Linux machine. eth - onboard, ens - pci slot naming",
	}

	return sch
}

func resourceImageSchemaV1() *schema.Resource {
	return &schema.Resource{Schema: resourceMainImageSchemaV1(dataSourceImageExtendSchemaV1())}
}

func resourceMainImageVirtualSchemaV1(sch map[string]*schema.Schema) map[string]*schema.Schema {
	delete(sch, "show_all")
	sch["name"] = &schema.Schema{
		Type:        schema.TypeString,
		Required:    true,
		Description: "Name of the rescue disk",
	}

	sch["link_to"] = &schema.Schema{
		Type:        schema.TypeInt,
		Required:    true,
		Description: "ID of real image to link this virtual image to upon creation",
	}

	sch["image_id"] = &schema.Schema{
		Type:        schema.TypeInt,
		Computed:    true,
		Description: "Image id",
	}

	return sch
}

func resourceImageVirtualSchemaV1() *schema.Resource {
	return &schema.Resource{Schema: resourceMainImageVirtualSchemaV1(dataSourceImageExtendSchemaV1())}
}
