package stpolicy

import (
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/stpolicy"
)

func flattenStoragePolicyData(d *schema.ResourceData, storagePolicy *stpolicy.InfoStoragePolicy) {
	d.Set("description", storagePolicy.Description)
	d.Set("guid", storagePolicy.GUID)
	d.Set("limit_iops", storagePolicy.LimitIOPS)
	d.Set("name", storagePolicy.Name)
	d.Set("status", storagePolicy.Status)
	d.Set("access_seps_pools", flattenAccessSEPPools(storagePolicy.AccessSEPPools))
	d.Set("usage", flattenUsage(storagePolicy.Usage))
}

func flattenAccessSEPPools(accessSEPPools stpolicy.ListAccessSEPPools) []map[string]interface{} {
	res := make([]map[string]interface{}, 0, len(accessSEPPools))
	for _, asp := range accessSEPPools {
		temp := map[string]interface{}{
			"sep_id":          asp.SEPID,
			"sep_name":        asp.Name,
			"pool_names":      asp.PoolNames,
			"sep_tech_status": asp.SepTechStatus,
		}

		res = append(res, temp)
	}

	return res
}

func flattenUsage(usage stpolicy.Usage) []map[string]interface{} {
	res := make([]map[string]interface{}, 0)

	temp := map[string]interface{}{
		"accounts":  usage.Accounts,
		"resgroups": usage.Resgroups,
	}

	res = append(res, temp)
	return res
}

func flattenStoragePolicyList(storagePolicyList *stpolicy.ListStoragePolicies) []map[string]interface{} {
	res := make([]map[string]interface{}, 0, len(storagePolicyList.Data))
	for _, v := range storagePolicyList.Data {
		temp := map[string]interface{}{
			"storage_policy_id": v.ID,
			"description":       v.Description,
			"guid":              v.GUID,
			"limit_iops":        v.LimitIOPS,
			"name":              v.Name,
			"status":            v.Status,
			"access_seps_pools": flattenAccessSEPPools(v.AccessSEPPools),
			"usage":             flattenUsage(v.Usage),
		}
		res = append(res, temp)
	}
	return res
}
