package utilities

import (
	"context"
	"fmt"

	"github.com/hashicorp/terraform-plugin-framework/diag"
	"github.com/hashicorp/terraform-plugin-log/tflog"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/vins"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/client"
)

func VINSStaticRouteDataSourceCheckPresence(ctx context.Context, vinsId, routeId uint64, c *client.Client) (*vins.ItemRoutes, diag.Diagnostics) {
	tflog.Info(ctx, "VINSStaticRouteDataSourceCheckPresence: Get info about vins static route", map[string]any{
		"vins_id":  vinsId,
		"route_id": routeId,
	})

	diags := diag.Diagnostics{}

	routesList, err := c.CloudAPI().VINS().StaticRouteList(ctx, vins.StaticRouteListRequest{VINSID: vinsId})
	if err != nil {
		diags.AddError("Cannot get info about vins static route", err.Error())
		return nil, diags
	}
	tflog.Info(ctx, "VINSStaticRouteDataSourceCheckPresence: successfull response from CloudAPI().VINS().StaticRouteList")

	staticRoute := &vins.ItemRoutes{}
	for _, route := range routesList.Data {
		if routeId == route.ID {
			staticRoute = &route
			return staticRoute, nil
		}
	}

	diags.AddError("Static route not found",
		fmt.Sprintf("Static route with id %d not found for vins with id %d", routeId, vinsId))
	return nil, diags
}
