/*
Copyright (c) 2019-2022 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.
*/

package location

import (
	"context"
	"fmt"

	log "github.com/sirupsen/logrus"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/locations"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
)

var DefaultGridID int

func UtilityLocationGetDefaultGridID(ctx context.Context, m interface{}) (int, error) {
	c := m.(*controller.ControllerCfg)
	req := locations.ListRequest{}

	log.Debug("utilityLocationGetDefaultGridID: retrieving locations list")
	locList, err := c.CloudAPI().Locations().List(ctx, req)
	if err != nil {
		return 0, err
	}

	if len(locList.Data) == 0 {
		DefaultGridID = 0
		return 0, fmt.Errorf("utilityLocationGetDefaultGridID: retrieved 0 length locations list")
	}

	DefaultGridID = int(locList.Data[0].GID)
	log.Debugf("utilityLocationGetDefaultGridID: default location GridID %d, name %s", DefaultGridID, locList.Data[0].Name)

	return DefaultGridID, nil
}
