'use server';
import { resolve } from '../../lib/server-container.js';
import { FeatureFlagDisabledError, requireFeatureFlag } from '../../lib/feature-flags.js';
import { ApplicationNotFoundError } from '../../../../../packages/core/src/domain/errors/application-not-found.error.js';
import { BedrockBinaryMissingError } from '../../../../../packages/core/src/domain/errors/bedrock-binary-missing.error.js';
import { BedrockNotEnabledError } from '../../../../../packages/core/src/domain/errors/bedrock-not-enabled.error.js';
import { PipxNotInstalledError } from '../../../../../packages/core/src/domain/errors/pipx-not-installed.error.js';
export async function enableBedrock(applicationId) {
    if (!applicationId?.trim()) {
        return { ok: false, code: 'INVALID_INPUT', remediation: 'applicationId is required' };
    }
    try {
        requireFeatureFlag('bedrockIntegration');
        const useCase = resolve('EnableBedrockForApplicationUseCase');
        await useCase.execute({ applicationId });
        return { ok: true, bedrockEnabled: true };
    }
    catch (error) {
        if (error instanceof FeatureFlagDisabledError) {
            return {
                ok: false,
                code: 'FEATURE_FLAG_DISABLED',
                remediation: 'Enable the "Bedrock memory" feature flag in Settings → Feature Flags.',
            };
        }
        if (error instanceof PipxNotInstalledError ||
            error instanceof BedrockBinaryMissingError ||
            error instanceof BedrockNotEnabledError) {
            return { ok: false, code: error.code, remediation: error.remediation };
        }
        if (error instanceof ApplicationNotFoundError) {
            return {
                ok: false,
                code: 'APPLICATION_NOT_FOUND',
                remediation: `Application ${applicationId} no longer exists. Reload the page and try again.`,
            };
        }
        const message = error instanceof Error ? error.message : 'Failed to enable bedrock';
        return { ok: false, code: 'UNKNOWN', remediation: message };
    }
}
