export class UnsafeCodeStrategyError extends Error {
    statusCode = 400;

    constructor() {
        super('Code strategy backtests are not supported until a sandboxed evaluator is available. Use rule-based strategyConfig instead.');
        this.name = 'UnsafeCodeStrategyError';
    }
}

export const isUnsafeCodeStrategyConfig = (strategyConfig: unknown): boolean => {
    if (!strategyConfig || typeof strategyConfig !== 'object') {
        return false;
    }

    const candidate = strategyConfig as Record<string, unknown>;
    return String(candidate.type || '').trim().toLowerCase() === 'code';
};

export const assertBacktestStrategyConfigSafe = (strategyConfig: unknown): void => {
    if (isUnsafeCodeStrategyConfig(strategyConfig)) {
        throw new UnsafeCodeStrategyError();
    }
};

export const assertBacktestBodyDoesNotContainInlineCode = (body: unknown): void => {
    if (!body || typeof body !== 'object') {
        return;
    }

    const candidate = body as Record<string, unknown>;
    if (typeof candidate.strategyCode === 'string' && candidate.strategyCode.trim()) {
        throw new UnsafeCodeStrategyError();
    }
};
