package client

import (
	"context"
	"fmt"

	"github.com/hashicorp/terraform-plugin-framework/datasource"
	"github.com/hashicorp/terraform-plugin-framework/resource"
	"github.com/hashicorp/terraform-plugin-log/tflog"
	decort "repository.basistech.ru/BASIS/decort-golang-sdk"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker"
)

type Client struct {
	Provider any
}

func DataSource(ctx context.Context, req *datasource.ConfigureRequest, resp *datasource.ConfigureResponse) *Client {
	if req.ProviderData == nil {
		tflog.Error(ctx, "Provider Configure is nill")
		return nil
	}

	res := Client{}
	if decort, ok := req.ProviderData.(*decort.DecortClient); ok {
		res.Provider = decort
		return &res
	}
	if bvs, ok := req.ProviderData.(*decort.BVSDecortClient); ok {
		res.Provider = bvs
		return &res
	}

	resp.Diagnostics.AddError(
		"Unexpected Data Source Configure Type",
		fmt.Sprintf("Expected *decort.DecortClient or *decort.BVSDecortClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
	)
	return nil

}

func Resource(ctx context.Context, req *resource.ConfigureRequest, resp *resource.ConfigureResponse) *Client {
	if req.ProviderData == nil {
		tflog.Error(ctx, "Provider Configure is nill")
		return nil
	}
	res := Client{}
	if decort, ok := req.ProviderData.(*decort.DecortClient); ok {
		res.Provider = decort
		return &res
	}
	if bvs, ok := req.ProviderData.(*decort.BVSDecortClient); ok {
		res.Provider = bvs
		return &res
	}

	resp.Diagnostics.AddError(
		"Unexpected Data Source Configure Type",
		fmt.Sprintf("Expected *decort.DecortClient or *decort.BVSDecortClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
	)
	return nil
}

func (c *Client) CloudAPI() *cloudapi.CloudAPI {
	if decort, ok := c.Provider.(*decort.DecortClient); ok {
		return decort.CloudAPI()
	}
	if bvs, ok := c.Provider.(*decort.BVSDecortClient); ok {
		return bvs.CloudAPI()
	}
	return nil
}

func (c *Client) CloudBroker() *cloudbroker.CloudBroker {
	if decort, ok := c.Provider.(*decort.DecortClient); ok {
		return decort.CloudBroker()
	}
	if bvs, ok := c.Provider.(*decort.BVSDecortClient); ok {
		return bvs.CloudBroker()
	}
	return nil
}
