// Package client provides service client factory for creating service clients.
package client

import (
	"context"
	"fmt"

	"git.dcentral.systems/toolz/goplt/internal/client/grpc"
	"git.dcentral.systems/toolz/goplt/pkg/registry"
	"git.dcentral.systems/toolz/goplt/pkg/services"
)

// ServiceClientFactory creates service clients for inter-service communication.
type ServiceClientFactory struct {
	registry registry.ServiceRegistry
}

// NewServiceClientFactory creates a new service client factory.
func NewServiceClientFactory(reg registry.ServiceRegistry) *ServiceClientFactory {
	return &ServiceClientFactory{
		registry: reg,
	}
}

// GetAuthClient returns an AuthServiceClient.
func (f *ServiceClientFactory) GetAuthClient() (services.AuthServiceClient, error) {
	return grpc.NewAuthClient(f.registry)
}

// GetIdentityClient returns an IdentityServiceClient.
func (f *ServiceClientFactory) GetIdentityClient() (services.IdentityServiceClient, error) {
	return grpc.NewIdentityClient(f.registry)
}

// GetAuthzClient returns an AuthzServiceClient.
func (f *ServiceClientFactory) GetAuthzClient() (services.AuthzServiceClient, error) {
	return grpc.NewAuthzClient(f.registry)
}

// GetAuditClient returns an AuditServiceClient.
func (f *ServiceClientFactory) GetAuditClient() (services.AuditServiceClient, error) {
	return grpc.NewAuditClient(f.registry)
}

// DiscoverService discovers service instances for a given service name.
func (f *ServiceClientFactory) DiscoverService(ctx context.Context, serviceName string) ([]*registry.ServiceInstance, error) {
	if f.registry == nil {
		return nil, fmt.Errorf("service registry is not available")
	}
	return f.registry.Discover(ctx, serviceName)
}
