package organization

import (
	"context"
	"errors"
	"fmt"
	"runtime/debug"
	"strings"
	"time"

	"github.com/tidbcloud/AgentTeam/internal/coredb"
	"github.com/tidbcloud/AgentTeam/internal/store/model"
)

func (store *Store) AcceptOrganizationInviteByTokenIfExists(ctx context.Context, user model.User, token string) (arg0 OrganizationInviteAcceptance, arg1 bool, arg2 error) {
	var out0 OrganizationInviteAcceptance
	var out1 bool
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, out1, err = store.InTx(tx).AcceptOrganizationInviteByTokenIfExists(ctx, user, token)
		return err
	})
	if err != nil {
		return out0, out1, err
	}
	return out0, out1, nil
}

func (store *Store) AddOrganizationTeamMemberForUser(ctx context.Context, userID string, organizationID string, teamID string, params AddOrganizationTeamMemberParams) (arg0 OrganizationTeamMemberView, arg1 error) {
	var out0 OrganizationTeamMemberView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).AddOrganizationTeamMemberForUser(ctx, userID, organizationID, teamID, params)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) AddOrganizationUnitMemberForUser(ctx context.Context, userID string, organizationID string, unitID string, params AddOrganizationUnitMemberParams) (arg0 OrganizationUnitMemberView, arg1 error) {
	var out0 OrganizationUnitMemberView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).AddOrganizationUnitMemberForUser(ctx, userID, organizationID, unitID, params)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) AuthenticateFeishuLogin(ctx context.Context, code string, state string, redirectURI string) (arg0 model.User, arg1 error) {
	var out0 model.User
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).AuthenticateFeishuLogin(ctx, code, state, redirectURI)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) AuthenticateOrganizationLLMProxyKey(ctx context.Context, token string) (arg0 OrganizationProviderProxyAccess, arg1 error) {
	var out0 OrganizationProviderProxyAccess
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).AuthenticateOrganizationLLMProxyKey(ctx, token)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) AuthenticatePersistentOrganizationLLMProxyKey(ctx context.Context, token string) (arg0 OrganizationProviderProxyAccess, arg1 error) {
	var out0 OrganizationProviderProxyAccess
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).AuthenticatePersistentOrganizationLLMProxyKey(ctx, token)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) AuthenticateSignedWorkspaceLLMProxyKey(ctx context.Context, token string) (arg0 OrganizationProviderProxyAccess, arg1 error) {
	var out0 OrganizationProviderProxyAccess
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).AuthenticateSignedWorkspaceLLMProxyKey(ctx, token)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) AutoJoinOrganizationsForVerifiedEmail(ctx context.Context, user model.User) (arg0 error) {
	return coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		return store.InTx(tx).AutoJoinOrganizationsForVerifiedEmail(ctx, user)
	})
}

func (store *Store) AutoJoinOrganizationsForVerifiedMobile(ctx context.Context, user model.User) (arg0 error) {
	return coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		return store.InTx(tx).AutoJoinOrganizationsForVerifiedMobile(ctx, user)
	})
}

func (store *Store) CreateOrganizationWorkspaceForUser(ctx context.Context, userID string, organizationID string, name string, slug string) (arg0 OrganizationWorkspaceView, arg1 error) {
	var out0 OrganizationWorkspaceView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).CreateOrganizationWorkspaceForUser(ctx, userID, organizationID, name, slug)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) EnsureDefaultWorkspaceForUser(ctx context.Context, user model.User) (arg0 error) {
	_, _, err := store.EnsureDefaultWorkspaceForUserAndReport(ctx, user)
	return err
}

func (store *Store) EnsureDefaultWorkspaceForUserAndReport(ctx context.Context, user model.User) (arg0 string, arg1 bool, arg2 error) {
	var out0 string
	var out1 bool
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, out1, err = store.InTx(tx).ensureDefaultWorkspaceForUserAndReportInTx(ctx, user)
		return err
	})
	if err != nil {
		return out0, out1, err
	}
	return out0, out1, nil
}

func (store *Store) MarkDefaultWorkspaceBootstrapCompleted(ctx context.Context, userID string, organizationID string) (arg0 error) {
	return coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		return store.InTx(tx).MarkDefaultWorkspaceBootstrapCompleted(ctx, userID, organizationID)
	})
}

func (store *Store) BuildFeishuLoginURL(ctx context.Context, organizationSlug string, redirectURI string) (arg0 string, arg1 error) {
	var out0 string
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).BuildFeishuLoginURL(ctx, organizationSlug, redirectURI)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) CheckOrganizationLLMQuota(ctx context.Context, access OrganizationProviderProxyAccess, model string) (arg0 OrganizationLLMQuotaDecision, arg1 error) {
	var out0 OrganizationLLMQuotaDecision
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).CheckOrganizationLLMQuota(ctx, access, model)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) CreateInternalWorkspaceProxyKey(ctx context.Context, workspaceID string, expiresAt time.Time) (arg0 string, arg1 error) {
	var out0 string
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).CreateInternalWorkspaceProxyKey(ctx, workspaceID, expiresAt)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) CreateOrganizationMemberForUser(ctx context.Context, userID string, organizationID string, teamID string, email string, defaultWorkspaceRole string) (arg0 OrganizationMemberView, arg1 error) {
	var out0 OrganizationMemberView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).CreateOrganizationMemberForUser(ctx, userID, organizationID, teamID, email, defaultWorkspaceRole)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) CreateOrganizationForUser(ctx context.Context, user model.User, name string, slug string) (arg0 OrganizationSummary, arg1 error) {
	var out0 OrganizationSummary
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).CreateOrganizationForUser(ctx, user, name, slug)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) CreateOrganizationInviteForUser(ctx context.Context, userID string, organizationID string, params CreateOrganizationInviteParams, tokenHash string, expiresAt time.Time) (arg0 OrganizationInviteView, arg1 error) {
	var out0 OrganizationInviteView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).CreateOrganizationInviteForUser(ctx, userID, organizationID, params, tokenHash, expiresAt)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) CreateOrganizationLLMProxyKeyForUser(ctx context.Context, userID string, organizationID string, params CreateOrganizationLLMProxyKeyParams) (arg0 OrganizationLLMProxyKeyCreateResult, arg1 error) {
	var out0 OrganizationLLMProxyKeyCreateResult
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).CreateOrganizationLLMProxyKeyForUser(ctx, userID, organizationID, params)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) CreateOrganizationProviderForUser(ctx context.Context, userID string, organizationID string, params CreateOrganizationProviderParams) (arg0 OrganizationProviderView, arg1 error) {
	var out0 OrganizationProviderView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).CreateOrganizationProviderForUser(ctx, userID, organizationID, params)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) CreateOrganizationProviderRouteForUser(ctx context.Context, userID string, organizationID string, params CreateOrganizationProviderRouteParams) (arg0 OrganizationProviderRouteView, arg1 error) {
	var out0 OrganizationProviderRouteView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).CreateOrganizationProviderRouteForUser(ctx, userID, organizationID, params)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) CreateOrganizationProviderRouteGroupForUser(ctx context.Context, userID string, organizationID string, routeID string, params CreateOrganizationProviderRouteGroupParams) (arg0 OrganizationProviderRouteGroupView, arg1 error) {
	var out0 OrganizationProviderRouteGroupView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).CreateOrganizationProviderRouteGroupForUser(ctx, userID, organizationID, routeID, params)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) CreateOrganizationProviderTokenForUser(ctx context.Context, userID string, organizationID string, providerID string, params CreateOrganizationProviderTokenParams) (arg0 OrganizationProviderTokenView, arg1 error) {
	var out0 OrganizationProviderTokenView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).CreateOrganizationProviderTokenForUser(ctx, userID, organizationID, providerID, params)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) CreateOrganizationTeamForUser(ctx context.Context, userID string, organizationID string, name string, slug string, description string) (arg0 OrganizationTeamView, arg1 error) {
	var out0 OrganizationTeamView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).CreateOrganizationTeamForUser(ctx, userID, organizationID, name, slug, description)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) CreateOrganizationUnitForUser(ctx context.Context, userID string, organizationID string, params CreateOrganizationUnitParams) (arg0 OrganizationUnitView, arg1 error) {
	var out0 OrganizationUnitView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).CreateOrganizationUnitForUser(ctx, userID, organizationID, params)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) DeleteOrganizationEnterpriseFeishuCredentialsForUser(ctx context.Context, userID string, organizationID string) (arg0 OrganizationEnterpriseFeishuView, arg1 error) {
	var out0 OrganizationEnterpriseFeishuView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).DeleteOrganizationEnterpriseFeishuCredentialsForUser(ctx, userID, organizationID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) DeleteOrganizationLLMProxyKeyForUser(ctx context.Context, userID string, organizationID string, keyID string) (arg0 error) {
	return coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		return store.InTx(tx).DeleteOrganizationLLMProxyKeyForUser(ctx, userID, organizationID, keyID)
	})
}

func (store *Store) DeleteOrganizationLLMQuotaPolicyForUser(ctx context.Context, userID string, organizationID string, subjectType string, subjectID string) (arg0 error) {
	return coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		return store.InTx(tx).DeleteOrganizationLLMQuotaPolicyForUser(ctx, userID, organizationID, subjectType, subjectID)
	})
}

func (store *Store) DeleteOrganizationProviderForUser(ctx context.Context, userID string, organizationID string, providerID string) (arg0 error) {
	return coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		return store.InTx(tx).DeleteOrganizationProviderForUser(ctx, userID, organizationID, providerID)
	})
}

func (store *Store) DeleteOrganizationProviderRouteForUser(ctx context.Context, userID string, organizationID string, routeID string) (arg0 error) {
	return coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		return store.InTx(tx).DeleteOrganizationProviderRouteForUser(ctx, userID, organizationID, routeID)
	})
}

func (store *Store) DeleteOrganizationProviderRouteGroupForUser(ctx context.Context, userID string, organizationID string, routeID string, groupID string) (arg0 error) {
	return coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		return store.InTx(tx).DeleteOrganizationProviderRouteGroupForUser(ctx, userID, organizationID, routeID, groupID)
	})
}

func (store *Store) DeleteOrganizationProviderTokenForUser(ctx context.Context, userID string, organizationID string, providerID string, tokenID string) (arg0 error) {
	return coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		return store.InTx(tx).DeleteOrganizationProviderTokenForUser(ctx, userID, organizationID, providerID, tokenID)
	})
}

func (store *Store) EnsureOrganizationUnitBackedTeamForUser(ctx context.Context, userID string, organizationID string, unitID string) (arg0 OrganizationTeamView, arg1 error) {
	var out0 OrganizationTeamView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).EnsureOrganizationUnitBackedTeamForUser(ctx, userID, organizationID, unitID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) FindOrganizationAuthBinding(ctx context.Context, organizationID string, authProvider string, subjectKey string, subjectValue string) (arg0 OrganizationAuthBindingView, arg1 bool, arg2 error) {
	var out0 OrganizationAuthBindingView
	var out1 bool
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, out1, err = store.InTx(tx).FindOrganizationAuthBinding(ctx, organizationID, authProvider, subjectKey, subjectValue)
		return err
	})
	if err != nil {
		return out0, out1, err
	}
	return out0, out1, nil
}

func (store *Store) FindOrganizationInviteInfoByToken(ctx context.Context, token string) (arg0 OrganizationInviteInfo, arg1 bool, arg2 error) {
	var out0 OrganizationInviteInfo
	var out1 bool
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, out1, err = store.InTx(tx).FindOrganizationInviteInfoByToken(ctx, token)
		return err
	})
	if err != nil {
		return out0, out1, err
	}
	return out0, out1, nil
}

func (store *Store) GetOrganizationDirectorySyncRunForUser(ctx context.Context, userID string, organizationID string, syncRunID string) (arg0 OrganizationDirectorySyncRunDetailView, arg1 error) {
	var out0 OrganizationDirectorySyncRunDetailView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).GetOrganizationDirectorySyncRunForUser(ctx, userID, organizationID, syncRunID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) GetOrganizationEnterpriseFeishuForUser(ctx context.Context, userID string, organizationID string) (arg0 OrganizationEnterpriseFeishuView, arg1 error) {
	var out0 OrganizationEnterpriseFeishuView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).GetOrganizationEnterpriseFeishuForUser(ctx, userID, organizationID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) GetOrganizationLLMErrorSummaryForUser(ctx context.Context, userID string, organizationID string, query OrganizationLLMStatsQuery) (arg0 OrganizationLLMErrorSummaryView, arg1 error) {
	var out0 OrganizationLLMErrorSummaryView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).GetOrganizationLLMErrorSummaryForUser(ctx, userID, organizationID, query)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) GetOrganizationLLMErrorTimeSeriesForUser(ctx context.Context, userID string, organizationID string, query OrganizationLLMStatsQuery) (arg0 OrganizationLLMErrorTimeSeriesView, arg1 error) {
	var out0 OrganizationLLMErrorTimeSeriesView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).GetOrganizationLLMErrorTimeSeriesForUser(ctx, userID, organizationID, query)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) GetOrganizationLLMStatsTimeSeriesForUser(ctx context.Context, userID string, organizationID string, query OrganizationLLMStatsQuery) (arg0 OrganizationLLMTimeSeriesView, arg1 error) {
	var out0 OrganizationLLMTimeSeriesView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).GetOrganizationLLMStatsTimeSeriesForUser(ctx, userID, organizationID, query)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) GetOrganizationProviderForUser(ctx context.Context, userID string, organizationID string, providerID string) (arg0 OrganizationProviderView, arg1 error) {
	var out0 OrganizationProviderView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).GetOrganizationProviderForUser(ctx, userID, organizationID, providerID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) GetOrganizationSettingsForUser(ctx context.Context, userID string, organizationID string) (arg0 OrganizationSettingsView, arg1 error) {
	var out0 OrganizationSettingsView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).GetOrganizationSettingsForUser(ctx, userID, organizationID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) GetOrganizationHostedModelSettingsForUser(ctx context.Context, userID string, organizationID string) (arg0 OrganizationHostedModelSettingsView, arg1 error) {
	var out0 OrganizationHostedModelSettingsView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).GetOrganizationHostedModelSettingsForUser(ctx, userID, organizationID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) GetOrganizationWorkspaceHostedModelAuthorizationForUser(ctx context.Context, userID string, organizationID string, workspaceID string) (arg0 OrganizationWorkspaceHostedModelAuthorizationView, arg1 error) {
	var out0 OrganizationWorkspaceHostedModelAuthorizationView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).GetOrganizationWorkspaceHostedModelAuthorizationForUser(ctx, userID, organizationID, workspaceID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) GetHostedModelSelectionForWorkspace(ctx context.Context, organizationID string, workspaceID string) (arg0 OrganizationHostedModelSelectionView, arg1 error) {
	var out0 OrganizationHostedModelSelectionView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).GetHostedModelSelectionForWorkspace(ctx, organizationID, workspaceID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) GrantOrganizationMemberWorkspaceAccessForUser(ctx context.Context, userID string, organizationID string, workspaceID string, accountID string, role string) (arg0 OrganizationWorkspaceGrantView, arg1 error) {
	var out0 OrganizationWorkspaceGrantView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).GrantOrganizationMemberWorkspaceAccessForUser(ctx, userID, organizationID, workspaceID, accountID, role)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) GrantOrganizationTeamWorkspaceAccessForUser(ctx context.Context, userID string, organizationID string, workspaceID string, teamID string, role string) (arg0 OrganizationWorkspaceTeamGrantView, arg1 error) {
	var out0 OrganizationWorkspaceTeamGrantView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).GrantOrganizationTeamWorkspaceAccessForUser(ctx, userID, organizationID, workspaceID, teamID, role)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) GrantOrganizationWorkspaceProviderForUser(ctx context.Context, userID string, organizationID string, workspaceID string, providerID string) (arg0 OrganizationWorkspaceProviderGrantView, arg1 error) {
	var out0 OrganizationWorkspaceProviderGrantView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).GrantOrganizationWorkspaceProviderForUser(ctx, userID, organizationID, workspaceID, providerID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) GrantOrganizationWorkspaceRouteForUser(ctx context.Context, userID string, organizationID string, workspaceID string, routeID string) (arg0 OrganizationWorkspaceRouteGrantView, arg1 error) {
	var out0 OrganizationWorkspaceRouteGrantView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).GrantOrganizationWorkspaceRouteForUser(ctx, userID, organizationID, workspaceID, routeID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) HasPendingOrganizationInviteForEmail(ctx context.Context, email string) (arg0 bool, arg1 error) {
	var out0 bool
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).HasPendingOrganizationInviteForEmail(ctx, email)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ImportOrganizationFeishuDirectory(ctx context.Context, organizationID string, client FeishuDirectoryClient) (arg0 OrganizationFeishuDirectoryImportResult, arg1 error) {
	var out0 OrganizationFeishuDirectoryImportResult
	helper := store.preflightTxStore()
	settings, err := helper.loadOrganizationSettingsView(ctx, store.db, organizationID)
	if err != nil {
		return out0, err
	}
	if settings.DirectoryProvider != organizationProviderFeishu {
		return out0, badRequest("organization directory provider is not feishu")
	}
	preparedCtx, err := store.prepareFeishuDirectoryImport(ctx, settings, "", client)
	if err != nil {
		return out0, err
	}
	err = coredb.WithTx(preparedCtx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ImportOrganizationFeishuDirectory(preparedCtx, organizationID, client)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListActiveOrganizationProviderRouteGroupsForLLM(ctx context.Context) (arg0 []OrganizationProviderRouteGroupView, arg1 error) {
	var out0 []OrganizationProviderRouteGroupView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListActiveOrganizationProviderRouteGroupsForLLM(ctx)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListActiveOrganizationProviderRoutesForLLM(ctx context.Context) (arg0 []OrganizationProviderRouteView, arg1 error) {
	var out0 []OrganizationProviderRouteView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListActiveOrganizationProviderRoutesForLLM(ctx)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListActiveOrganizationProvidersForLLM(ctx context.Context) (arg0 []OrganizationProviderView, arg1 error) {
	var out0 []OrganizationProviderView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListActiveOrganizationProvidersForLLM(ctx)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListOrganizationMemberAuthBindings(ctx context.Context, organizationID string, accountID string) (arg0 []OrganizationAuthBindingView, arg1 error) {
	var out0 []OrganizationAuthBindingView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListOrganizationMemberAuthBindings(ctx, organizationID, accountID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListOrganizationMemberUnitsForUser(ctx context.Context, userID string, organizationID string, accountID string) (arg0 []OrganizationMemberUnitView, arg1 error) {
	var out0 []OrganizationMemberUnitView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListOrganizationMemberUnitsForUser(ctx, userID, organizationID, accountID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListOrganizationMembersForUser(ctx context.Context, userID string, organizationID string) (arg0 []OrganizationMemberView, arg1 error) {
	var out0 []OrganizationMemberView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListOrganizationMembersForUser(ctx, userID, organizationID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListOrganizationMembersForUserPage(ctx context.Context, userID string, organizationID string, options OrganizationMemberListOptions) (arg0 OrganizationMemberPage, arg1 error) {
	var out0 OrganizationMemberPage
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListOrganizationMembersForUserPage(ctx, userID, organizationID, options)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListOrganizationInvitesForUser(ctx context.Context, userID string, organizationID string) (arg0 []OrganizationInviteView, arg1 error) {
	var out0 []OrganizationInviteView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListOrganizationInvitesForUser(ctx, userID, organizationID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListOrganizationInvitesForUserPage(ctx context.Context, userID string, organizationID string, options OrganizationInviteListOptions) (arg0 OrganizationInvitePage, arg1 error) {
	var out0 OrganizationInvitePage
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListOrganizationInvitesForUserPage(ctx, userID, organizationID, options)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListOrganizationLLMProxyKeysForUser(ctx context.Context, userID string, organizationID string) (arg0 []OrganizationLLMProxyKeyView, arg1 error) {
	var out0 []OrganizationLLMProxyKeyView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListOrganizationLLMProxyKeysForUser(ctx, userID, organizationID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListOrganizationLLMQuotaPoliciesForUser(ctx context.Context, userID string, organizationID string) (arg0 []OrganizationLLMQuotaPolicyView, arg1 error) {
	var out0 []OrganizationLLMQuotaPolicyView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListOrganizationLLMQuotaPoliciesForUser(ctx, userID, organizationID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListOrganizationLLMStatsDetailForUser(ctx context.Context, userID string, organizationID string, query OrganizationLLMStatsQuery) (arg0 []OrganizationLLMStatsDetailView, arg1 error) {
	var out0 []OrganizationLLMStatsDetailView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListOrganizationLLMStatsDetailForUser(ctx, userID, organizationID, query)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListOrganizationLLMStatsSummaryForUser(ctx context.Context, userID string, organizationID string) (arg0 []OrganizationLLMStatsSummaryView, arg1 error) {
	var out0 []OrganizationLLMStatsSummaryView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListOrganizationLLMStatsSummaryForUser(ctx, userID, organizationID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListOrganizationProviderRouteGroupsForUser(ctx context.Context, userID string, organizationID string, routeID string) (arg0 []OrganizationProviderRouteGroupView, arg1 error) {
	var out0 []OrganizationProviderRouteGroupView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListOrganizationProviderRouteGroupsForUser(ctx, userID, organizationID, routeID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListOrganizationProviderRoutesForUser(ctx context.Context, userID string, organizationID string) (arg0 []OrganizationProviderRouteView, arg1 error) {
	var out0 []OrganizationProviderRouteView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListOrganizationProviderRoutesForUser(ctx, userID, organizationID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListOrganizationProviderTokensForUser(ctx context.Context, userID string, organizationID string, providerID string) (arg0 []OrganizationProviderTokenView, arg1 error) {
	var out0 []OrganizationProviderTokenView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListOrganizationProviderTokensForUser(ctx, userID, organizationID, providerID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListOrganizationProvidersForUser(ctx context.Context, userID string, organizationID string) (arg0 []OrganizationProviderView, arg1 error) {
	var out0 []OrganizationProviderView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListOrganizationProvidersForUser(ctx, userID, organizationID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListOrganizationTeamMembersForUser(ctx context.Context, userID string, organizationID string, teamID string) (arg0 []OrganizationTeamMemberView, arg1 error) {
	var out0 []OrganizationTeamMemberView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListOrganizationTeamMembersForUser(ctx, userID, organizationID, teamID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListOrganizationTeamsForUser(ctx context.Context, userID string, organizationID string) (arg0 []OrganizationTeamView, arg1 error) {
	var out0 []OrganizationTeamView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListOrganizationTeamsForUser(ctx, userID, organizationID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListOrganizationUnitMembersForUser(ctx context.Context, userID string, organizationID string, unitID string) (arg0 []OrganizationUnitMemberView, arg1 error) {
	var out0 []OrganizationUnitMemberView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListOrganizationUnitMembersForUser(ctx, userID, organizationID, unitID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListOrganizationUnitsForUser(ctx context.Context, userID string, organizationID string) (arg0 []OrganizationUnitView, arg1 error) {
	var out0 []OrganizationUnitView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListOrganizationUnitsForUser(ctx, userID, organizationID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListOrganizationWorkspaceGrantsForUser(ctx context.Context, userID string, organizationID string, workspaceID string) (arg0 []OrganizationWorkspaceGrantView, arg1 error) {
	var out0 []OrganizationWorkspaceGrantView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListOrganizationWorkspaceGrantsForUser(ctx, userID, organizationID, workspaceID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListOrganizationWorkspaceProviderGrantsForUser(ctx context.Context, userID string, organizationID string, workspaceID string) (arg0 []OrganizationWorkspaceProviderGrantView, arg1 error) {
	var out0 []OrganizationWorkspaceProviderGrantView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListOrganizationWorkspaceProviderGrantsForUser(ctx, userID, organizationID, workspaceID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListOrganizationWorkspaceRouteGrantsForUser(ctx context.Context, userID string, organizationID string, workspaceID string) (arg0 []OrganizationWorkspaceRouteGrantView, arg1 error) {
	var out0 []OrganizationWorkspaceRouteGrantView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListOrganizationWorkspaceRouteGrantsForUser(ctx, userID, organizationID, workspaceID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListOrganizationWorkspaceTeamGrantsForUser(ctx context.Context, userID string, organizationID string, workspaceID string) (arg0 []OrganizationWorkspaceTeamGrantView, arg1 error) {
	var out0 []OrganizationWorkspaceTeamGrantView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListOrganizationWorkspaceTeamGrantsForUser(ctx, userID, organizationID, workspaceID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListOrganizationWorkspacesForUser(ctx context.Context, userID string, organizationID string) (arg0 []OrganizationWorkspaceView, arg1 error) {
	var out0 []OrganizationWorkspaceView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListOrganizationWorkspacesForUser(ctx, userID, organizationID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListOrganizationsForUser(ctx context.Context, userID string) (arg0 []OrganizationSummary, arg1 error) {
	var out0 []OrganizationSummary
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListOrganizationsForUser(ctx, userID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) GetWorkspaceOrganizationForUser(ctx context.Context, userID string, workspaceID string) (arg0 OrganizationSummary, arg1 error) {
	var out0 OrganizationSummary
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).GetWorkspaceOrganizationForUser(ctx, userID, workspaceID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListVisibleOrganizationProvidersForWorkspace(ctx context.Context, workspaceID string) (arg0 []OrganizationProviderView, arg1 error) {
	var out0 []OrganizationProviderView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListVisibleOrganizationProvidersForWorkspace(ctx, workspaceID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListWorkspaceMemberAccessSources(ctx context.Context, workspaceID string) (arg0 []WorkspaceMemberAccessSourceView, arg1 error) {
	var out0 []WorkspaceMemberAccessSourceView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListWorkspaceMemberAccessSources(ctx, workspaceID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) ListWorkspaceMemberAccessSourcesForUsers(ctx context.Context, workspaceID string, userIDs []string) (arg0 []WorkspaceMemberAccessSourceView, arg1 error) {
	var out0 []WorkspaceMemberAccessSourceView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).ListWorkspaceMemberAccessSourcesForUsers(ctx, workspaceID, userIDs)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) LoadActiveOrganizationProviderTokens(ctx context.Context, organizationID string, providerID string) (arg0 []OrganizationProviderTokenSecret, arg1 error) {
	var out0 []OrganizationProviderTokenSecret
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).LoadActiveOrganizationProviderTokens(ctx, organizationID, providerID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) MarkOrganizationMemberSynced(ctx context.Context, organizationID string, accountID string, params MarkOrganizationSyncedParams) (arg0 OrganizationMemberView, arg1 error) {
	var out0 OrganizationMemberView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).MarkOrganizationMemberSynced(ctx, organizationID, accountID, params)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) MarkOrganizationTeamSynced(ctx context.Context, organizationID string, teamID string, params MarkOrganizationSyncedParams) (arg0 OrganizationTeamView, arg1 error) {
	var out0 OrganizationTeamView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).MarkOrganizationTeamSynced(ctx, organizationID, teamID, params)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) MarkOrganizationUnitSynced(ctx context.Context, organizationID string, unitID string, params MarkOrganizationSyncedParams) (arg0 OrganizationUnitView, arg1 error) {
	var out0 OrganizationUnitView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).MarkOrganizationUnitSynced(ctx, organizationID, unitID, params)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) PatchOrganizationSettingsForUser(ctx context.Context, userID string, organizationID string, params UpdateOrganizationSettingsParams) (arg0 OrganizationSettingsView, arg1 error) {
	var out0 OrganizationSettingsView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).PatchOrganizationSettingsForUser(ctx, userID, organizationID, params)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) PatchOrganizationHostedModelSettingsForUser(ctx context.Context, userID string, organizationID string, params UpdateOrganizationHostedModelSettingsParams) (arg0 OrganizationHostedModelSettingsView, arg1 error) {
	var out0 OrganizationHostedModelSettingsView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).PatchOrganizationHostedModelSettingsForUser(ctx, userID, organizationID, params)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) PreviewOrganizationWorkspaceHostedModelAuthorizationForUser(ctx context.Context, userID string, organizationID string, workspaceID string, params UpsertOrganizationWorkspaceHostedModelAuthorizationParams) (arg0 OrganizationWorkspaceHostedModelAuthorizationView, arg1 error) {
	var out0 OrganizationWorkspaceHostedModelAuthorizationView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).PreviewOrganizationWorkspaceHostedModelAuthorizationForUser(ctx, userID, organizationID, workspaceID, params)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) PatchOrganizationForUser(ctx context.Context, userID string, organizationID string, params UpdateOrganizationParams) (arg0 OrganizationSummary, arg1 error) {
	var out0 OrganizationSummary
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).PatchOrganizationForUser(ctx, userID, organizationID, params)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) RecordOrganizationLLMUsage(ctx context.Context, record OrganizationLLMUsageRecord) (arg0 error) {
	return coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		return store.InTx(tx).RecordOrganizationLLMUsage(ctx, record)
	})
}

func (store *Store) RemoveOrganizationTeamMemberForUser(ctx context.Context, userID string, organizationID string, teamID string, accountID string) (arg0 error) {
	return coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		return store.InTx(tx).RemoveOrganizationTeamMemberForUser(ctx, userID, organizationID, teamID, accountID)
	})
}

func (store *Store) RemoveOrganizationUnitMemberForUser(ctx context.Context, userID string, organizationID string, unitID string, accountID string) (arg0 error) {
	return coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		return store.InTx(tx).RemoveOrganizationUnitMemberForUser(ctx, userID, organizationID, unitID, accountID)
	})
}

func (store *Store) RevokeOrganizationMemberWorkspaceAccessForUser(ctx context.Context, userID string, organizationID string, workspaceID string, accountID string) (arg0 error) {
	return coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		return store.InTx(tx).RevokeOrganizationMemberWorkspaceAccessForUser(ctx, userID, organizationID, workspaceID, accountID)
	})
}

func (store *Store) RevokeOrganizationInviteForUser(ctx context.Context, userID string, organizationID string, inviteID string) (arg0 error) {
	return coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		return store.InTx(tx).RevokeOrganizationInviteForUser(ctx, userID, organizationID, inviteID)
	})
}

func (store *Store) RevokeOrganizationTeamWorkspaceAccessForUser(ctx context.Context, userID string, organizationID string, workspaceID string, teamID string) (arg0 error) {
	return coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		return store.InTx(tx).RevokeOrganizationTeamWorkspaceAccessForUser(ctx, userID, organizationID, workspaceID, teamID)
	})
}

func (store *Store) RevokeOrganizationWorkspaceProviderForUser(ctx context.Context, userID string, organizationID string, workspaceID string, providerID string) (arg0 error) {
	return coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		return store.InTx(tx).RevokeOrganizationWorkspaceProviderForUser(ctx, userID, organizationID, workspaceID, providerID)
	})
}

func (store *Store) RevokeOrganizationWorkspaceRouteForUser(ctx context.Context, userID string, organizationID string, workspaceID string, routeID string) (arg0 error) {
	return coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		return store.InTx(tx).RevokeOrganizationWorkspaceRouteForUser(ctx, userID, organizationID, workspaceID, routeID)
	})
}

func (store *Store) SwitchOrganizationMemberPrimaryTeamForUser(ctx context.Context, userID string, organizationID string, accountID string, teamID string) (arg0 OrganizationMemberView, arg1 error) {
	var out0 OrganizationMemberView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).SwitchOrganizationMemberPrimaryTeamForUser(ctx, userID, organizationID, accountID, teamID)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) StartOrganizationFeishuDirectorySyncForUser(ctx context.Context, userID string, organizationID string) (OrganizationDirectorySyncRunSummaryView, bool, error) {
	var out OrganizationDirectorySyncRunSummaryView
	var created bool
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out, created, err = store.InTx(tx).StartOrganizationFeishuDirectorySyncForUser(ctx, userID, organizationID)
		return err
	})
	if err != nil {
		return OrganizationDirectorySyncRunSummaryView{}, false, err
	}
	return out, created, nil
}

func (store *Store) RunOrganizationFeishuDirectorySyncForUser(ctx context.Context, userID string, organizationID string, syncRunID string) (out OrganizationFeishuDirectoryImportResult, err error) {
	defer func() {
		recovered := recover()
		if recovered == nil {
			return
		}
		panicErr := fmt.Errorf("organization feishu directory sync panic: %v\n%s", recovered, debug.Stack())
		err = store.failOrganizationFeishuDirectorySyncRun(organizationID, syncRunID, panicErr)
	}()
	return store.syncOrganizationFeishuDirectoryForUser(ctx, userID, organizationID, syncRunID, true)
}

func (store *Store) SyncOrganizationFeishuDirectoryForUser(ctx context.Context, userID string, organizationID string) (arg0 OrganizationFeishuDirectoryImportResult, arg1 error) {
	return store.syncOrganizationFeishuDirectoryForUser(ctx, userID, organizationID, "", false)
}

func (store *Store) syncOrganizationFeishuDirectoryForUser(ctx context.Context, userID string, organizationID string, syncRunID string, markFailed bool) (arg0 OrganizationFeishuDirectoryImportResult, arg1 error) {
	var out0 OrganizationFeishuDirectoryImportResult
	failRun := func(err error) (OrganizationFeishuDirectoryImportResult, error) {
		if !markFailed {
			return out0, err
		}
		return out0, store.failOrganizationFeishuDirectorySyncRun(organizationID, syncRunID, err)
	}

	helper := store.preflightTxStore()
	if _, err := helper.requireActiveOrganizationAdmin(ctx, store.db, userID, organizationID); err != nil {
		return failRun(err)
	}
	settings, err := helper.loadOrganizationSettingsView(ctx, store.db, organizationID)
	if err != nil {
		return failRun(err)
	}
	if settings.DirectoryProvider != organizationProviderFeishu {
		return failRun(badRequest("organization directory provider is not feishu"))
	}
	credentials, err := helper.resolveOrganizationFeishuCredentials(ctx, store.db, organizationID)
	if err != nil {
		return failRun(err)
	}
	client, err := helper.newFeishuDirectoryClient(credentials)
	if err != nil {
		return failRun(err)
	}
	preparedCtx, err := store.prepareFeishuDirectoryImport(ctx, settings, credentials.AppID, client)
	if err != nil {
		return failRun(err)
	}
	err = coredb.WithTx(preparedCtx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).SyncOrganizationFeishuDirectoryRunForUser(preparedCtx, userID, organizationID, syncRunID)
		return err
	})
	if err != nil {
		return failRun(err)
	}
	return out0, nil
}

func (store *Store) failOrganizationFeishuDirectorySyncRun(organizationID string, syncRunID string, err error) error {
	trimmedSyncRunID := strings.TrimSpace(syncRunID)
	if trimmedSyncRunID == "" {
		return err
	}
	markCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	if markErr := store.markOrganizationDirectorySyncRunFailed(markCtx, organizationID, trimmedSyncRunID); markErr != nil && !errors.Is(markErr, ErrNotFound) {
		return fmt.Errorf("%w; mark sync run failed: %v", err, markErr)
	}
	return err
}

func (store *Store) markOrganizationDirectorySyncRunFailed(ctx context.Context, organizationID string, syncRunID string) error {
	return coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		return store.InTx(tx).failOrganizationDirectorySyncRun(ctx, tx, organizationID, syncRunID, time.Now().UTC())
	})
}

func (store *Store) UpdateOrganizationMemberRoleForUser(ctx context.Context, userID string, organizationID string, accountID string, requestedRole string) (arg0 OrganizationMemberView, arg1 error) {
	var out0 OrganizationMemberView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).UpdateOrganizationMemberRoleForUser(ctx, userID, organizationID, accountID, requestedRole)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) UpdateOrganizationEnterpriseFeishuCredentialsForUser(ctx context.Context, userID string, organizationID string, appID string, appSecret string) (arg0 OrganizationEnterpriseFeishuView, arg1 error) {
	var out0 OrganizationEnterpriseFeishuView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).UpdateOrganizationEnterpriseFeishuCredentialsForUser(ctx, userID, organizationID, appID, appSecret)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) UpdateOrganizationProviderForUser(ctx context.Context, userID string, organizationID string, providerID string, params UpdateOrganizationProviderParams) (arg0 OrganizationProviderView, arg1 error) {
	var out0 OrganizationProviderView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).UpdateOrganizationProviderForUser(ctx, userID, organizationID, providerID, params)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) UpdateOrganizationProviderRouteForUser(ctx context.Context, userID string, organizationID string, routeID string, params UpdateOrganizationProviderRouteParams) (arg0 OrganizationProviderRouteView, arg1 error) {
	var out0 OrganizationProviderRouteView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).UpdateOrganizationProviderRouteForUser(ctx, userID, organizationID, routeID, params)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) UpdateOrganizationProviderRouteGroupForUser(ctx context.Context, userID string, organizationID string, routeID string, groupID string, params UpdateOrganizationProviderRouteGroupParams) (arg0 OrganizationProviderRouteGroupView, arg1 error) {
	var out0 OrganizationProviderRouteGroupView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).UpdateOrganizationProviderRouteGroupForUser(ctx, userID, organizationID, routeID, groupID, params)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) UpdateOrganizationProviderTokenSecret(ctx context.Context, organizationID string, providerID string, tokenID string, params UpdateOrganizationProviderTokenSecretParams) (arg0 error) {
	return coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		return store.InTx(tx).UpdateOrganizationProviderTokenSecret(ctx, organizationID, providerID, tokenID, params)
	})
}

func (store *Store) UpdateOrganizationSettingsForUser(ctx context.Context, userID string, organizationID string, directoryBackend string) (arg0 OrganizationSettingsView, arg1 error) {
	var out0 OrganizationSettingsView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).UpdateOrganizationSettingsForUser(ctx, userID, organizationID, directoryBackend)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) UpsertOrganizationMemberAuthBinding(ctx context.Context, organizationID string, accountID string, params UpsertOrganizationAuthBindingParams) (arg0 OrganizationAuthBindingView, arg1 error) {
	var out0 OrganizationAuthBindingView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).UpsertOrganizationMemberAuthBinding(ctx, organizationID, accountID, params)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) UpsertOrganizationLLMQuotaPolicyForUser(ctx context.Context, userID string, organizationID string, params UpsertOrganizationLLMQuotaPolicyParams) (arg0 OrganizationLLMQuotaPolicyView, arg1 error) {
	var out0 OrganizationLLMQuotaPolicyView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).UpsertOrganizationLLMQuotaPolicyForUser(ctx, userID, organizationID, params)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}

func (store *Store) UpsertOrganizationWorkspaceHostedModelAuthorizationForUser(ctx context.Context, userID string, organizationID string, workspaceID string, params UpsertOrganizationWorkspaceHostedModelAuthorizationParams) (arg0 OrganizationWorkspaceHostedModelAuthorizationView, arg1 error) {
	var out0 OrganizationWorkspaceHostedModelAuthorizationView
	err := coredb.WithTx(ctx, store.db, func(tx coredb.Tx) error {
		var err error
		out0, err = store.InTx(tx).UpsertOrganizationWorkspaceHostedModelAuthorizationForUser(ctx, userID, organizationID, workspaceID, params)
		return err
	})
	if err != nil {
		return out0, err
	}
	return out0, nil
}
