/*
Copyright (c) 2019-2023 Digital Energy Cloud Solutions LLC. All Rights Reserved.
Authors:
Petr Krutov, <petr.krutov@digitalenergy.online>
Stanislav Solovev, <spsolovev@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 (
	"context"
	"encoding/json"

	"github.com/google/uuid"
	"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	log "github.com/sirupsen/logrus"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/constants"
)

func resourceSepConfigCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
	log.Debugf("resourceSepConfigCreate: called for sep id %d", d.Get("sep_id").(int))

	if _, ok := d.GetOk("sep_id"); ok {
		sepConfig, err := utilitySepConfigCheckPresence(ctx, d, m)

		if err != nil {
			return diag.FromErr(err)
		}

		if sepConfig == nil {
			return diag.Errorf("provided sep id config does not exist")
		}

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

	return resourceSepConfigRead(ctx, d, m)
}

func resourceSepConfigRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
	log.Debugf("resourceSepConfigRead: called for sep id: %d", d.Get("sep_id").(int))

	sepConfig, err := utilitySepConfigCheckPresence(ctx, d, m)
	if sepConfig == nil {
		d.SetId("")
		return diag.FromErr(err)
	}
	data, _ := json.Marshal(sepConfig)
	d.Set("config", string(data))
	return nil
}

func resourceSepConfigDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
	d.SetId("")
	return nil
}

func resourceSepConfigUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
	log.Debugf("resourceSepConfigEdit: called for sep id: %d", d.Get("sep_id").(int))

	if d.HasChange("config") {
		err := resourceSepUpdateConfig(ctx, d, m)
		if err != nil {
			return diag.FromErr(err)
		}
	}

	if d.HasChange("field_edit") {
		err := resourceSepFieldEdit(ctx, d, m)
		if err != nil {
			return diag.FromErr(err)
		}
	}

	return resourceSepConfigRead(ctx, d, m)
}

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

		CreateContext: resourceSepConfigCreate,
		ReadContext:   resourceSepConfigRead,
		UpdateContext: resourceSepConfigUpdate,
		DeleteContext: resourceSepConfigDelete,

		Importer: &schema.ResourceImporter{
			StateContext: schema.ImportStatePassthroughContext,
		},

		Timeouts: &schema.ResourceTimeout{
			Create:  &constants.Timeout600s,
			Read:    &constants.Timeout300s,
			Update:  &constants.Timeout600s,
			Delete:  &constants.Timeout300s,
			Default: &constants.Timeout300s,
		},

		Schema: resourceSepConfigSchemaMake(),
	}
}
