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

import (
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/constants"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/validators"
)

func nodeMasterDefault() K8sNodeRecord {
	return K8sNodeRecord{
		Num:  1,
		Cpu:  2,
		Ram:  2048,
		Disk: 0,
	}
}

func nodeWorkerDefault() K8sNodeRecord {
	return K8sNodeRecord{
		Num:  1,
		Cpu:  1,
		Ram:  1024,
		Disk: 0,
	}
}

func parseDefaultNode(nodeList []interface{}) K8sNodeRecord {
	node := nodeList[0].(map[string]interface{})

	return K8sNodeRecord{
		Num:     node["num"].(int),
		Cpu:     node["cpu"].(int),
		Ram:     node["ram"].(int),
		Disk:    node["disk"].(int),
		SepID:   node["sep_id"].(int),
		SepPool: node["sep_pool"].(string),
	}
}

func mastersSchemaMake() map[string]*schema.Schema {
	masters := masterGroupSchemaMake()
	masters["num"] = &schema.Schema{
		Type:         schema.TypeInt,
		Required:     true,
		ValidateFunc: validation.IntInSlice([]int{1, 3, 5}),
		Description:  "Number of nodes to create. Can be either 1, 3 or 5",
	}
	masters["sep_id"] = &schema.Schema{
		Type:     schema.TypeInt,
		Optional: true,
	}
	masters["sep_pool"] = &schema.Schema{
		Type:     schema.TypeString,
		Optional: true,
	}
	masters["cpu"] = &schema.Schema{
		Type:     schema.TypeInt,
		Required: true,
		//ForceNew:    true,
		Description: "Node CPU count.",
	}
	masters["ram"] = &schema.Schema{
		Type:     schema.TypeInt,
		Required: true,
		//ForceNew:    true,
		ValidateFunc: validation.All(
			validation.IntAtLeast(constants.MIN_RAM_PER_COMPUTE),
			validators.DivisibleBy(constants.RAM_DIVISIBILITY),
		),
		Description: "Node RAM in MB.",
	}
	masters["disk"] = &schema.Schema{
		Type:     schema.TypeInt,
		Required: true,
		//ForceNew:    true,
		Description: "Node boot disk size in GB.",
	}

	return masters
}

func workersSchemaMake() map[string]*schema.Schema {
	return map[string]*schema.Schema{
		"name": {
			Type:     schema.TypeString,
			Required: true,
		},
		"num": {
			Type:     schema.TypeInt,
			Required: true,
		},
		"ram": {
			Type: schema.TypeInt,
			ValidateFunc: validation.All(
				validation.IntAtLeast(constants.MIN_RAM_PER_COMPUTE),
				validators.DivisibleBy(constants.RAM_DIVISIBILITY),
			),
			Required: true,
		},
		"cpu": {
			Type:     schema.TypeInt,
			Required: true,
		},
		"disk": {
			Type:     schema.TypeInt,
			Required: true,
		},
		"annotations": {
			Type:     schema.TypeList,
			Computed: true,
			Optional: true,
			Elem: &schema.Schema{
				Type: schema.TypeString,
			},
		},
		"detailed_info": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Resource{
				Schema: detailedInfoSchemaMake(),
			},
		},
		"chipset": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "Type of the emulated system.",
		},
		"guid": {
			Type:     schema.TypeString,
			Computed: true,
		},
		"id": {
			Type:     schema.TypeInt,
			Computed: true,
		},
		"labels": {
			Type:     schema.TypeList,
			Computed: true,
			Optional: true,
			Elem: &schema.Schema{
				Type: schema.TypeString,
			},
		},
		"taints": {
			Type:     schema.TypeList,
			Computed: true,
			Optional: true,
			Elem: &schema.Schema{
				Type: schema.TypeString,
			},
		},
		"sep_id": {
			Type:     schema.TypeInt,
			Optional: true,
		},
		"sep_pool": {
			Type:     schema.TypeString,
			Optional: true,
		},
	}
}
