package segments

import (
	"context"

	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/sdn/segments"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
)

func utilitySegmentCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*segments.SegmentResponse, error) {
	c := m.(*controller.ControllerCfg)

	req := segments.GetRequest{}

	if d.Id() != "" {
		req.SegmentID = d.Id()
	} else {
		req.SegmentID = d.Get("segment_id").(string)
	}

	segment, err := c.SDN().Segments().Get(ctx, req)
	if err != nil {
		return nil, err
	}

	return segment, nil
}
