package bservice

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

func flattenResourceBasicServiceGroup(d *schema.ResourceData, bsg *bservice.RecordGroup) {
	d.Set("account_id", bsg.AccountID)
	d.Set("account_name", bsg.AccountName)
	d.Set("computes", flattenBSGroupComputes(bsg.Computes))
	d.Set("consistency", bsg.Consistency)
	d.Set("cpu", bsg.CPU)
	d.Set("created_by", bsg.CreatedBy)
	d.Set("created_time", bsg.CreatedTime)
	d.Set("deleted_by", bsg.DeletedBy)
	d.Set("deleted_time", bsg.DeletedTime)
	d.Set("disk", bsg.Disk)
	d.Set("driver", bsg.Driver)
	d.Set("extnets", bsg.ExtNets)
	d.Set("gid", bsg.GID)
	d.Set("guid", bsg.GUID)
	d.Set("image_id", bsg.ImageID)
	d.Set("milestones", bsg.Milestones)
	d.Set("compgroup_name", bsg.Name)
	d.Set("compgroup_id", bsg.ID)
	d.Set("parents", bsg.Parents)
	d.Set("ram", bsg.RAM)
	d.Set("rg_id", bsg.RGID)
	d.Set("rg_name", bsg.RGName)
	d.Set("role", bsg.Role)
	d.Set("sep_id", bsg.SEPID)
	d.Set("sep_pool", bsg.PoolName)
	d.Set("seq_no", bsg.SeqNo)
	d.Set("status", bsg.Status)
	d.Set("tech_status", bsg.TechStatus)
	d.Set("timeout_start", bsg.TimeoutStart)
	d.Set("updated_by", bsg.UpdatedBy)
	d.Set("updated_time", bsg.UpdatedTime)
	d.Set("vinses", bsg.VINSes)
}

func flattenGroups(groups bservice.ListGroups) []map[string]interface{} {
	res := make([]map[string]interface{}, 0)
	for _, group := range groups {
		temp := map[string]interface{}{
			"computes":    group.Computes,
			"consistency": group.Consistency,
			"id":          group.ID,
			"name":        group.Name,
			"status":      group.Status,
			"tech_status": group.TechStatus,
		}
		res = append(res, temp)
	}
	return res
}

func flattenService(d *schema.ResourceData, bs *bservice.RecordBasicService) {
	d.Set("account_id", bs.AccountID)
	d.Set("account_name", bs.AccountName)
	d.Set("base_domain", bs.BaseDomain)
	d.Set("computes", flattenBasicServiceComputes(bs.Computes))
	d.Set("cpu_total", bs.CPUTotal)
	d.Set("created_by", bs.CreatedBy)
	d.Set("created_time", bs.CreatedTime)
	d.Set("deleted_by", bs.DeletedBy)
	d.Set("deleted_time", bs.DeletedTime)
	d.Set("disk_total", bs.DiskTotal)
	d.Set("gid", bs.GID)
	d.Set("groups", flattenGroups(bs.Groups))
	d.Set("guid", bs.GUID)
	d.Set("milestones", bs.Milestones)
	d.Set("service_name", bs.Name)
	d.Set("service_id", bs.ID)
	d.Set("parent_srv_id", bs.ParentSrvID)
	d.Set("parent_srv_type", bs.ParentSrvType)
	d.Set("ram_total", bs.RAMTotal)
	d.Set("rg_id", bs.RGID)
	d.Set("rg_name", bs.RGName)
	d.Set("snapshots", flattenBasicServiceSnapshots(bs.Snapshots))
	d.Set("ssh_key", bs.SSHKey)
	d.Set("ssh_user", bs.SSHUser)
	d.Set("status", bs.Status)
	d.Set("tech_status", bs.TechStatus)
	d.Set("updated_by", bs.UpdatedBy)
	d.Set("updated_time", bs.UpdatedTime)
	d.Set("user_managed", bs.UserManaged)
	d.Set("zone_id", bs.ZoneID)
}

func flattenBasicServiceComputes(bscs bservice.ListComputes) []map[string]interface{} {
	res := make([]map[string]interface{}, 0)
	for _, bsc := range bscs {
		temp := map[string]interface{}{
			"account_id":     bsc.AccountID,
			"architecture":   bsc.Architecture,
			"compgroup_id":   bsc.CompGroupID,
			"compgroup_name": bsc.CompGroupName,
			"compgroup_role": bsc.CompGroupRole,
			"id":             bsc.ID,
			"name":           bsc.Name,
			"rg_id":          bsc.RGID,
			"node_id":        bsc.NodeID,
			"status":         bsc.Status,
			"tech_status":    bsc.TechStatus,
		}
		res = append(res, temp)
	}

	return res
}

func flattenBasicServiceSnapshots(bsrvss bservice.ListSnapshots) []map[string]interface{} {
	res := make([]map[string]interface{}, 0, len(bsrvss))
	for _, bsrvs := range bsrvss {
		temp := map[string]interface{}{
			"guid":      bsrvs.GUID,
			"label":     bsrvs.Label,
			"timestamp": bsrvs.Timestamp,
			"valid":     bsrvs.Valid,
		}
		res = append(res, temp)
	}
	return res
}

func flattenBasicServiceSnapshotsList(bsrvss *bservice.ListInfoSnapshots) []map[string]interface{} {
	res := make([]map[string]interface{}, 0, len(bsrvss.Data))
	for _, bsrvs := range bsrvss.Data {
		temp := map[string]interface{}{
			"guid":      bsrvs.GUID,
			"label":     bsrvs.Label,
			"timestamp": bsrvs.Timestamp,
			"valid":     bsrvs.Valid,
		}
		res = append(res, temp)
	}
	return res
}
