/*
Copyright (c) 2019-2023 Digital Energy Cloud Solutions LLC. All Rights Reserved.
Authors:
Petr Krutov, <petr.krutov@digitalenergy.online>
Stanislav Solovev, <spsolovev@digitalenergy.online>
Sergey Kisil, <svkisil@digitalenergy.online>

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 sep

import (
	"encoding/json"

	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/sep"
)

func flattenSep(d *schema.ResourceData, desSep *sep.RecordSEP) {
	d.Set("consumed_by", desSep.ConsumedBy)
	d.Set("desc", desSep.Description)
	d.Set("gid", desSep.GID)
	d.Set("guid", desSep.GUID)
	d.Set("sep_id", desSep.ID)
	d.Set("milestones", desSep.Milestones)
	d.Set("multipath_num", desSep.MultipathNum)
	d.Set("name", desSep.Name)
	d.Set("obj_status", desSep.ObjStatus)
	d.Set("provided_by", desSep.ProvidedBy)
	d.Set("shared_with", desSep.SharedWith)
	d.Set("tech_status", desSep.TechStatus)
	d.Set("type", desSep.Type)
	data, _ := json.Marshal(desSep.Config)
	d.Set("config", string(data))
}

func flattenSepListItems(sl *sep.ListSEP) []map[string]interface{} {
	res := make([]map[string]interface{}, 0)
	for _, item := range sl.Data {
		data, _ := json.Marshal(item.Config)
		temp := map[string]interface{}{
			"consumed_by":   item.ConsumedBy,
			"desc":          item.Description,
			"gid":           item.GID,
			"guid":          item.GUID,
			"sep_id":        item.ID,
			"milestones":    item.Milestones,
			"name":          item.Name,
			"multipath_num": item.MultipathNum,
			"obj_status":    item.ObjStatus,
			"provided_by":   item.ProvidedBy,
			"shared_with":   item.SharedWith,
			"tech_status":   item.TechStatus,
			"type":          item.Type,
			"config":        string(data),
		}
		res = append(res, temp)
	}
	return res
}

func flattenSepPool(rp *sep.RecordPool) []map[string]interface{} {
	sh := make([]map[string]interface{}, 0)
	res := map[string]interface{}{
		"access_account_ids":   rp.AccessAccountIDs,
		"access_res_group_ids": rp.AccessResGroupIDs,
		"name":                 rp.Name,
		"pagecache_ratio":      rp.PageCacheRatio,
		"reference_id":         rp.ReferenceID,
		"types":                rp.Types,
		"uris":                 flattenSepPoolUris(rp),
		"usage_limit":          rp.UsageLimit,
	}
	sh = append(sh, res)
	return sh
}

func flattenSepPoolUris(rp *sep.RecordPool) []map[string]interface{} {
	res := make([]map[string]interface{}, 0)
	for _, ur := range rp.URIs {
		temp := map[string]interface{}{
			"ip":   ur.IP,
			"port": ur.Port,
		}
		res = append(res, temp)
	}
	return res
}

func flattenSepConsumption(d *schema.ResourceData, sepCons *sep.RecordConsumption) {
	d.Set("type", sepCons.Type)
	d.Set("total", flattenSepConsumptionTotal(sepCons.Total))
	d.Set("by_pool", flattenSepConsumptionPools(sepCons))
}

func flattenSepConsumptionTotal(sc sep.Total) []map[string]interface{} {
	sh := make([]map[string]interface{}, 0)
	temp := map[string]interface{}{
		"capacity_limit": sc.CapacityLimit,
		"disk_count":     sc.DiskCount,
		"disk_usage":     sc.DiskUsage,
		"snapshot_count": sc.SnapshotCount,
		"snapshot_usage": sc.SnapshotUsage,
		"usage":          sc.Usage,
		"usage_limit":    sc.UsageLimit,
	}
	sh = append(sh, temp)
	return sh
}

func flattenSepConsumptionPools(bp *sep.RecordConsumption) []map[string]interface{} {
	sh := make([]map[string]interface{}, 0)
	for key, value := range bp.ByPool {
		temp := map[string]interface{}{
			"name":           key,
			"disk_count":     value.DiskCount,
			"disk_usage":     value.DiskUsage,
			"snapshot_count": value.SnapshotCount,
			"snapshot_usage": value.SnapshotUsage,
			"usage":          value.Usage,
			"usage_limit":    value.UsageLimit,
		}
		sh = append(sh, temp)
	}
	return sh
}

func flattenAvailableSEPList(d *schema.ResourceData, sepList *sep.ListAvailableSEP) {
	d.Set("items", flattenSEPDataList(sepList.Data))
	d.Set("entry_count", sepList.EntryCount)
}

func flattenSEPDataList(sepDataList []sep.SEPData) []map[string]interface{} {
	sh := make([]map[string]interface{}, 0)

	for _, sepData := range sepDataList {
		temp := map[string]interface{}{
			"sep_id":   sepData.SEPID,
			"sep_name": sepData.SEPName,
			"sep_type": sepData.SEPType,
			"pools":    flattenPoolList(sepData.Pools),
		}
		sh = append(sh, temp)
	}

	return sh
}

func flattenPoolList(pools []sep.Pool) []map[string]interface{} {
	sh := make([]map[string]interface{}, 0)

	for _, pool := range pools {
		temp := map[string]interface{}{
			"name":   pool.Name,
			"types":  pool.Types,
			"system": pool.System,
		}
		sh = append(sh, temp)
	}

	return sh
}
