// Code generated by Wire. DO NOT EDIT.

//go:generate go run -mod=mod github.com/google/wire/cmd/wire
//go:build !wireinject
// +build !wireinject

package main

import (
	"github.com/chainloop-dev/chainloop/app/controlplane/internal/conf/controlplane/config/v1"
	"github.com/chainloop-dev/chainloop/app/controlplane/internal/dispatcher"
	"github.com/chainloop-dev/chainloop/app/controlplane/internal/server"
	"github.com/chainloop-dev/chainloop/app/controlplane/internal/service"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/auditor"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/authz"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/biz"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/conf/controlplane/config/v1"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/policies"
	"github.com/chainloop-dev/chainloop/app/controlplane/plugins/sdk/v1"
	"github.com/chainloop-dev/chainloop/pkg/blobmanager/loader"
	"github.com/chainloop-dev/chainloop/pkg/credentials"
	"github.com/go-kratos/kratos/v2/log"
)

import (
	_ "net/http/pprof"
)

// Injectors from wire.go:

func wireApp(bootstrap *conf.Bootstrap, readerWriter credentials.ReaderWriter, logger log.Logger, availablePlugins sdk.AvailablePlugins) (*app, func(), error) {
	confData := bootstrap.Data
	data_Database := confData.Database
	databaseConfig := newDataConf(data_Database)
	dataData, cleanup, err := data.NewData(databaseConfig, logger)
	if err != nil {
		return nil, nil, err
	}
	userRepo := data.NewUserRepo(dataData, logger)
	groupRepo := data.NewGroupRepo(dataData, logger)
	membershipRepo := data.NewMembershipRepo(dataData, groupRepo, logger)
	organizationRepo := data.NewOrganizationRepo(dataData, logger)
	casBackendRepo := data.NewCASBackendRepo(dataData, logger)
	providers := loader.LoadProviders(readerWriter)
	bootstrap_CASServer := bootstrap.CasServer
	casServerDefaultOpts := newCASServerOptions(bootstrap_CASServer)
	bootstrap_NatsServer := bootstrap.NatsServer
	conn, err := newNatsConnection(bootstrap_NatsServer)
	if err != nil {
		cleanup()
		return nil, nil, err
	}
	auditLogPublisher, err := auditor.NewAuditLogPublisher(conn, logger)
	if err != nil {
		cleanup()
		return nil, nil, err
	}
	auditorUseCase := biz.NewAuditorUseCase(auditLogPublisher, logger)
	casBackendUseCase, err := biz.NewCASBackendUseCase(casBackendRepo, readerWriter, providers, casServerDefaultOpts, auditorUseCase, logger)
	if err != nil {
		cleanup()
		return nil, nil, err
	}
	integrationRepo := data.NewIntegrationRepo(dataData, logger)
	integrationAttachmentRepo := data.NewIntegrationAttachmentRepo(dataData, logger)
	workflowRepo := data.NewWorkflowRepo(dataData, logger)
	newIntegrationUseCaseOpts := &biz.NewIntegrationUseCaseOpts{
		IRepo:   integrationRepo,
		IaRepo:  integrationAttachmentRepo,
		WfRepo:  workflowRepo,
		CredsRW: readerWriter,
		Logger:  logger,
	}
	integrationUseCase := biz.NewIntegrationUseCase(newIntegrationUseCaseOpts)
	v := bootstrap.Onboarding
	organizationUseCase := biz.NewOrganizationUseCase(organizationRepo, casBackendUseCase, auditorUseCase, integrationUseCase, membershipRepo, v, logger)
	membershipUseCase := biz.NewMembershipUseCase(membershipRepo, organizationUseCase, auditorUseCase, userRepo, logger)
	allowList := newAuthAllowList(bootstrap)
	userAccessSyncerUseCase := biz.NewUserAccessSyncerUseCase(logger, userRepo, allowList)
	newUserUseCaseParams := &biz.NewUserUseCaseParams{
		UserRepo:            userRepo,
		MembershipUseCase:   membershipUseCase,
		OrganizationUseCase: organizationUseCase,
		OnboardingConfig:    v,
		Logger:              logger,
		AuditorUseCase:      auditorUseCase,
		UserAccessSyncer:    userAccessSyncerUseCase,
	}
	userUseCase := biz.NewUserUseCase(newUserUseCaseParams)
	robotAccountRepo := data.NewRobotAccountRepo(dataData, logger)
	auth := bootstrap.Auth
	robotAccountUseCase := biz.NewRootAccountUseCase(robotAccountRepo, workflowRepo, auth, logger)
	casCredentialsUseCase, err := biz.NewCASCredentialsUseCase(auth)
	if err != nil {
		cleanup()
		return nil, nil, err
	}
	v2 := _wireValue
	casClientUseCase := biz.NewCASClientUseCase(casCredentialsUseCase, bootstrap_CASServer, logger, v2...)
	referrerRepo := data.NewReferrerRepo(dataData, workflowRepo, logger)
	referrerSharedIndex := bootstrap.ReferrerSharedIndex
	referrerUseCase, err := biz.NewReferrerUseCase(referrerRepo, workflowRepo, membershipUseCase, referrerSharedIndex, logger)
	if err != nil {
		cleanup()
		return nil, nil, err
	}
	apiTokenRepo := data.NewAPITokenRepo(dataData, logger)
	apiTokenJWTConfig := newJWTConfig(auth)
	config := authzConfig(bootstrap)
	enforcer, err := authz.NewDatabaseEnforcer(databaseConfig, config)
	if err != nil {
		cleanup()
		return nil, nil, err
	}
	apiTokenUseCase, err := biz.NewAPITokenUseCase(apiTokenRepo, apiTokenJWTConfig, enforcer, organizationUseCase, auditorUseCase, logger)
	if err != nil {
		cleanup()
		return nil, nil, err
	}
	projectsRepo := data.NewProjectsRepo(dataData, logger)
	workflowContractRepo := data.NewWorkflowContractRepo(dataData, logger)
	v3 := bootstrap.PolicyProviders
	v4 := newPolicyProviderConfig(v3)
	registry, err := policies.NewRegistry(logger, v4...)
	if err != nil {
		cleanup()
		return nil, nil, err
	}
	workflowContractUseCase := biz.NewWorkflowContractUseCase(workflowContractRepo, registry, auditorUseCase, logger)
	workflowUseCase := biz.NewWorkflowUsecase(workflowRepo, projectsRepo, workflowContractUseCase, auditorUseCase, membershipUseCase, logger)
	orgInvitationRepo := data.NewOrgInvitation(dataData, logger)
	orgInvitationUseCase, err := biz.NewOrgInvitationUseCase(orgInvitationRepo, membershipRepo, userRepo, auditorUseCase, groupRepo, projectsRepo, logger)
	if err != nil {
		cleanup()
		return nil, nil, err
	}
	groupUseCase := biz.NewGroupUseCase(logger, groupRepo, membershipRepo, userRepo, orgInvitationUseCase, auditorUseCase, orgInvitationRepo, enforcer, membershipUseCase)
	projectUseCase := biz.NewProjectsUseCase(logger, projectsRepo, membershipRepo, auditorUseCase, groupUseCase, membershipUseCase, orgInvitationUseCase, orgInvitationRepo, enforcer)
	v5 := serviceOpts(logger, enforcer, projectUseCase, groupUseCase)
	workflowService := service.NewWorkflowService(workflowUseCase, workflowContractUseCase, projectUseCase, v5...)
	confServer := bootstrap.Server
	authService, err := service.NewAuthService(userUseCase, organizationUseCase, membershipUseCase, orgInvitationUseCase, auth, confServer, auditorUseCase, v5...)
	if err != nil {
		cleanup()
		return nil, nil, err
	}
	robotAccountService := service.NewRobotAccountService(robotAccountUseCase, v5...)
	workflowRunRepo := data.NewWorkflowRunRepo(dataData, logger)
	signingUseCase, err := biz.NewChainloopSigningUseCase(bootstrap, logger)
	if err != nil {
		cleanup()
		return nil, nil, err
	}
	workflowRunUseCase, err := biz.NewWorkflowRunUseCase(workflowRunRepo, workflowRepo, signingUseCase, auditorUseCase, logger)
	if err != nil {
		cleanup()
		return nil, nil, err
	}
	newWorkflowRunServiceOpts := &service.NewWorkflowRunServiceOpts{
		WorkflowRunUC:      workflowRunUseCase,
		WorkflowUC:         workflowUseCase,
		WorkflowContractUC: workflowContractUseCase,
		CredsReader:        readerWriter,
		Opts:               v5,
	}
	workflowRunService := service.NewWorkflowRunService(newWorkflowRunServiceOpts)
	attestationUseCase := biz.NewAttestationUseCase(casClientUseCase, logger)
	fanOutDispatcher := dispatcher.New(integrationUseCase, workflowUseCase, workflowRunUseCase, readerWriter, casClientUseCase, availablePlugins, logger)
	casMappingRepo := data.NewCASMappingRepo(dataData, casBackendRepo, logger)
	casMappingUseCase := biz.NewCASMappingUseCase(casMappingRepo, membershipUseCase, logger)
	v6 := bootstrap.PrometheusIntegration
	orgMetricsRepo := data.NewOrgMetricsRepo(dataData, logger)
	orgMetricsUseCase, err := biz.NewOrgMetricsUseCase(orgMetricsRepo, organizationRepo, workflowUseCase, logger)
	if err != nil {
		cleanup()
		return nil, nil, err
	}
	prometheusUseCase := biz.NewPrometheusUseCase(v6, organizationUseCase, orgMetricsUseCase, logger)
	projectVersionRepo := data.NewProjectVersionRepo(dataData, logger)
	projectVersionUseCase := biz.NewProjectVersionUseCase(projectVersionRepo, logger)
	newAttestationServiceOpts := &service.NewAttestationServiceOpts{
		WorkflowRunUC:      workflowRunUseCase,
		WorkflowUC:         workflowUseCase,
		WorkflowContractUC: workflowContractUseCase,
		OCIUC:              casBackendUseCase,
		CredsReader:        readerWriter,
		IntegrationUseCase: integrationUseCase,
		CasCredsUseCase:    casCredentialsUseCase,
		AttestationUC:      attestationUseCase,
		FanoutDispatcher:   fanOutDispatcher,
		CASMappingUseCase:  casMappingUseCase,
		ReferrerUC:         referrerUseCase,
		OrgUC:              organizationUseCase,
		PromUC:             prometheusUseCase,
		ProjectUC:          projectUseCase,
		ProjectVersionUC:   projectVersionUseCase,
		SigningUseCase:     signingUseCase,
		Opts:               v5,
	}
	attestationService := service.NewAttestationService(newAttestationServiceOpts)
	workflowContractService := service.NewWorkflowSchemaService(workflowContractUseCase, v5...)
	contextService := service.NewContextService(casBackendUseCase, userUseCase, v5...)
	casCredentialsService := service.NewCASCredentialsService(casCredentialsUseCase, casMappingUseCase, casBackendUseCase, enforcer, v5...)
	orgMetricsService := service.NewOrgMetricsService(orgMetricsUseCase, v5...)
	integrationsService := service.NewIntegrationsService(integrationUseCase, workflowUseCase, availablePlugins, v5...)
	organizationService := service.NewOrganizationService(membershipUseCase, organizationUseCase, v5...)
	casBackendService := service.NewCASBackendService(casBackendUseCase, providers, v5...)
	casRedirectService, err := service.NewCASRedirectService(casMappingUseCase, casCredentialsUseCase, bootstrap_CASServer, v5...)
	if err != nil {
		cleanup()
		return nil, nil, err
	}
	orgInvitationService := service.NewOrgInvitationService(orgInvitationUseCase, v5...)
	referrerService := service.NewReferrerService(referrerUseCase, v5...)
	apiTokenService := service.NewAPITokenService(apiTokenUseCase, v5...)
	attestationStateRepo := data.NewAttestationStateRepo(dataData, logger)
	attestationStateUseCase, err := biz.NewAttestationStateUseCase(attestationStateRepo, workflowRunRepo)
	if err != nil {
		cleanup()
		return nil, nil, err
	}
	newAttestationStateServiceOpt := &service.NewAttestationStateServiceOpt{
		AttestationStateUseCase: attestationStateUseCase,
		WorkflowUseCase:         workflowUseCase,
		WorkflowRunUseCase:      workflowRunUseCase,
		Opts:                    v5,
	}
	attestationStateService := service.NewAttestationStateService(newAttestationStateServiceOpt)
	userService := service.NewUserService(membershipUseCase, organizationUseCase, v5...)
	signingService := service.NewSigningService(signingUseCase, v5...)
	prometheusService := service.NewPrometheusService(organizationUseCase, prometheusUseCase, v5...)
	groupService := service.NewGroupService(groupUseCase, v5...)
	projectService := service.NewProjectService(v5...)
	federatedAuthentication := bootstrap.FederatedAuthentication
	validator, err := newProtoValidator()
	if err != nil {
		cleanup()
		return nil, nil, err
	}
	opts := &server.Opts{
		UserUseCase:         userUseCase,
		RobotAccountUseCase: robotAccountUseCase,
		CASBackendUseCase:   casBackendUseCase,
		CASClientUseCase:    casClientUseCase,
		IntegrationUseCase:  integrationUseCase,
		ReferrerUseCase:     referrerUseCase,
		APITokenUseCase:     apiTokenUseCase,
		OrganizationUseCase: organizationUseCase,
		WorkflowUseCase:     workflowUseCase,
		MembershipUseCase:   membershipUseCase,
		WorkflowSvc:         workflowService,
		AuthSvc:             authService,
		RobotAccountSvc:     robotAccountService,
		WorkflowRunSvc:      workflowRunService,
		AttestationSvc:      attestationService,
		WorkflowContractSvc: workflowContractService,
		ContextSvc:          contextService,
		CASCredsSvc:         casCredentialsService,
		OrgMetricsSvc:       orgMetricsService,
		IntegrationsSvc:     integrationsService,
		OrganizationSvc:     organizationService,
		CASBackendSvc:       casBackendService,
		CASRedirectSvc:      casRedirectService,
		OrgInvitationSvc:    orgInvitationService,
		ReferrerSvc:         referrerService,
		APITokenSvc:         apiTokenService,
		AttestationStateSvc: attestationStateService,
		UserSvc:             userService,
		SigningSvc:          signingService,
		PrometheusSvc:       prometheusService,
		GroupSvc:            groupService,
		ProjectSvc:          projectService,
		Logger:              logger,
		ServerConfig:        confServer,
		AuthConfig:          auth,
		FederatedConfig:     federatedAuthentication,
		BootstrapConfig:     bootstrap,
		Credentials:         readerWriter,
		Enforcer:            enforcer,
		Validator:           validator,
	}
	grpcServer, err := server.NewGRPCServer(opts)
	if err != nil {
		cleanup()
		return nil, nil, err
	}
	httpServer, err := server.NewHTTPServer(opts, grpcServer)
	if err != nil {
		cleanup()
		return nil, nil, err
	}
	httpMetricsServer, err := server.NewHTTPMetricsServer(opts)
	if err != nil {
		cleanup()
		return nil, nil, err
	}
	httpProfilerServer, err := server.NewHTTPProfilerServer(opts)
	if err != nil {
		cleanup()
		return nil, nil, err
	}
	workflowRunExpirerUseCase := biz.NewWorkflowRunExpirerUseCase(workflowRunRepo, prometheusUseCase, logger)
	apiTokenSyncerUseCase := biz.NewAPITokenSyncerUseCase(apiTokenUseCase)
	casBackendChecker := biz.NewCASBackendChecker(logger, casBackendRepo, casBackendUseCase)
	mainApp := newApp(logger, grpcServer, httpServer, httpMetricsServer, httpProfilerServer, workflowRunExpirerUseCase, availablePlugins, apiTokenSyncerUseCase, userAccessSyncerUseCase, casBackendChecker, bootstrap)
	return mainApp, func() {
		cleanup()
	}, nil
}

var (
	_wireValue = []biz.CASClientOpts{}
)

// wire.go:

func authzConfig(conf2 *conf.Bootstrap) *authz.Config {
	return &authz.Config{ManagedResources: authz.ManagedResources, RolesMap: authz.RolesMap, RestrictOrgCreation: conf2.RestrictOrgCreation}
}

func newJWTConfig(conf2 *conf.Auth) *biz.APITokenJWTConfig {
	return &biz.APITokenJWTConfig{
		SymmetricHmacKey: conf2.GeneratedJwsHmacSecret,
	}
}

func newDataConf(in *conf.Data_Database) *v1.DatabaseConfig {
	c := &v1.DatabaseConfig{Driver: in.Driver, Source: in.Source, MinOpenConns: in.MinOpenConns, MaxOpenConns: in.MaxOpenConns}
	if in.MaxConnIdleTime != nil {
		c.MaxConnIdleTime = in.MaxConnIdleTime
	}
	return c
}

func newPolicyProviderConfig(in []*conf.PolicyProvider) []*policies.NewRegistryConfig {
	out := make([]*policies.NewRegistryConfig, 0, len(in))
	for _, p := range in {
		out = append(out, &policies.NewRegistryConfig{Name: p.Name, Host: p.Host, Default: p.Default, URL: p.Url})
	}
	return out
}

func serviceOpts(l log.Logger, enforcer *authz.Enforcer, pUC *biz.ProjectUseCase, gUC *biz.GroupUseCase) []service.NewOpt {
	return []service.NewOpt{service.WithLogger(l), service.WithEnforcer(enforcer), service.WithProjectUseCase(pUC), service.WithGroupUseCase(gUC)}
}

func newCASServerOptions(in *conf.Bootstrap_CASServer) *biz.CASServerDefaultOpts {
	if in == nil {
		return &biz.CASServerDefaultOpts{}
	}
	return &biz.CASServerDefaultOpts{
		DefaultEntryMaxSize: in.GetDefaultEntryMaxSize(),
	}
}

func newAuthAllowList(conf2 *conf.Bootstrap) *v1.AllowList {
	return conf2.Auth.GetAllowList()
}
