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

import (
	"context"

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

	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func utilityAuditListCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*audit.ListAudits, error) {
	c := m.(*controller.ControllerCfg)
	req := audit.ListRequest{}

	if timestampAt, ok := d.GetOk("timestamp_at"); ok {
		req.TimestampAt = uint64(timestampAt.(int))
	}
	if timestampTo, ok := d.GetOk("timestamp_to"); ok {
		req.TimestampTo = uint64(timestampTo.(int))
	}
	if user, ok := d.GetOk("user"); ok {
		req.User = user.(string)
	}
	if call, ok := d.GetOk("call"); ok {
		req.Call = call.(string)
	}
	if minStatusCode, ok := d.GetOk("min_status_code"); ok {
		req.MinStatusCode = uint64(minStatusCode.(int))
	}
	if maxStatusCode, ok := d.GetOk("max_status_code"); ok {
		req.MaxStatusCode = uint64(maxStatusCode.(int))
	}
	if sortBy, ok := d.GetOk("sort_by"); ok {
		req.SortBy = sortBy.(string)
	}
	if Page, ok := d.GetOk("page"); ok {
		req.Page = uint64(Page.(int))
	}
	if RequestID, ok := d.GetOk("request_id"); ok {
		req.RequestID = RequestID.(string)
	}
	if Size, ok := d.GetOk("size"); ok {
		req.Size = uint64(Size.(int))
	}
	if resgroupID, ok := d.GetOk("resgroup_id"); ok {
		req.RGID = uint64(resgroupID.(int))
	}
	if computeID, ok := d.GetOk("compute_id"); ok {
		req.ComputeID = uint64(computeID.(int))
	}
	if accountID, ok := d.GetOk("account_id"); ok {
		req.AccountID = uint64(accountID.(int))
	}
	if vinsID, ok := d.GetOk("vins_id"); ok {
		req.VINSID = uint64(vinsID.(int))
	}
	if serviceID, ok := d.GetOk("service_id"); ok {
		req.ServiceID = uint64(serviceID.(int))
	}
	if k8sID, ok := d.GetOk("k8s_id"); ok {
		req.K8SID = uint64(k8sID.(int))
	}
	if flipgroupID, ok := d.GetOk("flipgroup_id"); ok {
		req.FLIPGroupID = uint64(flipgroupID.(int))
	}
	if lbID, ok := d.GetOk("lb_id"); ok {
		req.LBID = uint64(lbID.(int))
	}
	if sepID, ok := d.GetOk("sep_id"); ok {
		req.SEPID = uint64(sepID.(int))
	}
	if excludeAuditLines, ok := d.GetOk("exclude_audit_lines"); ok {
		req.ExcludeAuditLines = excludeAuditLines.(bool)
	}

	log.Debugf("utilityAuditListCheckPresence: load audit list")
	auditList, err := c.CloudAPI().Audit().List(ctx, req)
	if err != nil {
		return nil, err
	}

	return auditList, nil
}
