/*
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.
*/

/*
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 locations

import (
	"context"

	"github.com/google/uuid"
	"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/locations"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/constants"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/flattens"
)

func flattenLocationsList(ll *locations.ListLocations) []map[string]interface{} {
	res := make([]map[string]interface{}, 0, len(ll.Data))
	for _, l := range ll.Data {
		temp := map[string]interface{}{
			"ckey":                l.CKey,
			"meta":                flattens.FlattenMeta(l.Meta),
			"auth_broker":         l.AuthBroker,
			"flag":                l.Flag,
			"gid":                 l.GID,
			"guid":                l.GUID,
			"id":                  l.ID,
			"location_code":       l.LocationCode,
			"name":                l.Name,
			"network_modes":       l.NetworkModes,
			"sdn_support":         l.SDNSupport,
			"zero_access_enabled": l.ZeroAccessEnabled,
			"bro_enabled":         l.BROEnabled,
		}
		res = append(res, temp)
	}
	return res

}

func dataSourceLocationsListRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
	locations, err := utilityLocationsListCheckPresence(ctx, d, m)
	if err != nil {
		d.SetId("")
		return diag.FromErr(err)
	}

	id := uuid.New()
	d.SetId(id.String())

	d.Set("items", flattenLocationsList(locations))
	d.Set("entry_count", locations.EntryCount)

	return nil
}

func dataSourceLocationsListSchemaMake() map[string]*schema.Schema {
	return map[string]*schema.Schema{
		"flag": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "Filter by flag",
		},
		"name": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "Filter by name",
		},
		"by_id": {
			Type:        schema.TypeInt,
			Optional:    true,
			Description: "Filter by ID",
		},
		"location_code": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "Filter by location code",
		},
		"sort_by": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "sort by one of supported fields, format +|-(field)",
		},
		"page": {
			Type:        schema.TypeInt,
			Optional:    true,
			Description: "page number",
		},
		"size": {
			Type:        schema.TypeInt,
			Optional:    true,
			Description: "page size",
		},
		"items": {
			Type:        schema.TypeList,
			Computed:    true,
			Description: "Locations list",
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"ckey": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"meta": {
						Type:     schema.TypeList,
						Computed: true,
						Elem: &schema.Schema{
							Type: schema.TypeString,
						},
					},
					"auth_broker": {
						Type:     schema.TypeList,
						Computed: true,
						Elem: &schema.Schema{
							Type: schema.TypeString,
						},
					},
					"flag": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"gid": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "Grid id",
					},
					"guid": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "location id",
					},
					"id": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "location id",
					},
					"location_code": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "Location code",
					},
					"name": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "Location name",
					},
					"network_modes": {
						Type:     schema.TypeList,
						Computed: true,
						Elem: &schema.Schema{
							Type: schema.TypeString,
						},
					},
					"sdn_support": {
						Type:        schema.TypeBool,
						Computed:    true,
						Description: "support of SDN",
					},
					"zero_access_enabled": {
						Type:     schema.TypeBool,
						Computed: true,
					},
					"bro_enabled": {
						Type:     schema.TypeBool,
						Computed: true,
					},
				},
			},
		},
		"entry_count": {
			Type:     schema.TypeInt,
			Computed: true,
		},
	}
}

func DataSourceLocationsList() *schema.Resource {
	return &schema.Resource{
		SchemaVersion: 1,

		ReadContext: dataSourceLocationsListRead,

		Timeouts: &schema.ResourceTimeout{
			Read:    &constants.Timeout30s,
			Default: &constants.Timeout60s,
		},

		Schema: dataSourceLocationsListSchemaMake(),
	}
}
