/*
Copyright (c) 2019-2024 Digital Energy Cloud Solutions LLC. All Rights Reserved.
Authors:
Petr Krutov, <petr.krutov@digitalenergy.online>
Stanislav Solovev, <spsolovev@digitalenergy.online>
Kasim Baybikov, <kmbaybikov@basistech.ru>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/*
Terraform DECORT provider - manage resources provided by DECORT (Digital Energy Cloud
Orchestration Technology) with Terraform by Hashicorp.

Source code: https://repository.basistech.ru/BASIS/terraform-provider-decort

Please see README.md to learn where to place source code so that it
builds seamlessly.

Documentation: https://repository.basistech.ru/BASIS/terraform-provider-decort/wiki
*/

package vfpool

import (
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	log "github.com/sirupsen/logrus"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/vfpool"
)

func flattenVFPoolList(vfpooll *vfpool.ListVFPool) []map[string]interface{} {
	log.Debugf("flattenVFPoolList start")
	res := make([]map[string]interface{}, 0, len(vfpooll.Data))
	for _, vfpool := range vfpooll.Data {
		temp := map[string]interface{}{
			"account_access": vfpool.AccountAccess,
			"created_time":   vfpool.CreatedTime,
			"description":    vfpool.Description,
			"gid":            vfpool.GID,
			"guid":           vfpool.GUID,
			"vfpool_id":      vfpool.ID,
			"name":           vfpool.Name,
			"rg_access":      vfpool.RGAccess,
			"status":         vfpool.Status,
			"updated_time":   vfpool.UpdatedTime,
			"vfs":            flattenVFSList(vfpool.VFS),
		}
		res = append(res, temp)
	}
	log.Debugf("flattenVFPoolList end")
	return res

}

func flattenVFPoolDataSource(d *schema.ResourceData, item *vfpool.RecordVFPool) error {
	log.Debugf("flattenVFPoolDataSource: start decoded VFPool name %q / ID %d",
		item.Name, item.ID)

	d.Set("account_access", item.AccountAccess)
	d.Set("created_time", item.CreatedTime)
	d.Set("description", item.Description)
	d.Set("gid", item.GID)
	d.Set("guid", item.GUID)
	d.Set("name", item.Name)
	d.Set("rg_access", item.RGAccess)
	d.Set("status", item.Status)
	d.Set("updated_time", item.UpdatedTime)
	d.Set("vfs", flattenVFSList(item.VFS))

	log.Debugf("flattenVFPoolDataSource: decoded VFPool name %q / ID %d, complete",
		item.Name, item.ID)
	return nil
}

func flattenVFPoolResource(d *schema.ResourceData, item *vfpool.RecordVFPool) error {
	log.Debugf("flattenVFPoolResource: start decoded VFPool name %q / ID %d",
		item.Name, item.ID)

	d.Set("account_access", item.AccountAccess)
	d.Set("created_time", item.CreatedTime)
	d.Set("description", item.Description)
	d.Set("gid", item.GID)
	d.Set("guid", item.GUID)
	d.Set("vfpool_id", item.ID)
	d.Set("name", item.Name)
	d.Set("rg_access", item.RGAccess)
	d.Set("status", item.Status)
	d.Set("updated_time", item.UpdatedTime)
	d.Set("vfs", flattenVFSList(item.VFS))

	log.Debugf("flattenVFPoolResource: decoded VFPool name %q / ID %d, complete",
		item.Name, item.ID)
	return nil
}


func flattenVFSList(vfsItem []vfpool.VFS) []map[string]interface{} {
	res := make([]map[string]interface{}, 0, len(vfsItem))
	for _, item := range vfsItem {
		temp := map[string]interface{}{
			"node_id": item.NodeID,
			"vf_list": flattenVFList(item.VFList),
		}
		res = append(res, temp)
	}
	return res
}

func flattenVFList(vfItem vfpool.VFList) []map[string]interface{} {
	res := make([]map[string]interface{}, 0, len(vfItem))
	for _, item := range vfItem {
		temp := map[string]interface{}{
			"nic_name": item.NicName,
			"vfs_info": flattenVFInfoList(item.VFSInfo),
		}
		res = append(res, temp)
	}
	return res
}

func flattenVFInfoList(vfInfo vfpool.VFSInfoList) []map[string]interface{} {
	res := make([]map[string]interface{}, 0, len(vfInfo))
	for _, item := range vfInfo {
		temp := map[string]interface{}{
			"id":      item.ID,
			"claimed": item.Claimed,
			"vm_id":   item.VMID,
		}
		res = append(res, temp)
	}
	return res
}
