/*
 * Copyright 2026, Jaime Terrats.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import { generateSlackWorkflows } from '../generators/workflows/slack.js';
import { consultativeDesign, documentationStandards, generatedPromptsProtocol, interactionPreferences, semanticCommits, } from './shared-sections.js';
export const slackProfile = {
    id: 'slack',
    label: 'Slack Developer (Bolt.js)',
    ruleFile: 'slack-standards.mdc',
    extensions: ['dbaeumer.vscode-eslint', 'esbenp.prettier-vscode', 'humao.rest-client'],
    capabilities: ['deploy', 'code-analysis', 'prompt-registry', 'declare-points'],
    cursorScope: 'auto-attach',
    globs: ['**/slack.json', '**/manifest.json', '**/bolt/**', '**/slack-app/**'],
    detect(cwd) {
        return existsSync(join(cwd, 'slack.json')) || existsSync(join(cwd, 'manifest.json'));
    },
    workflows: generateSlackWorkflows,
    ruleContent() {
        return [
            '---',
            'description: Slack Developer (Bolt.js) Standards',
            'globs:',
            'alwaysApply: true',
            '---',
            '',
            '# Slack Developer (Bolt.js) Standards',
            '',
            '> Role: Slack Developer — Salesforce Professional Services.',
            '',
            ...consultativeDesign('Bolt.js architecture and Salesforce integration decisions'),
            '',
            '## Codebase Contextualization',
            '- Scan existing `listeners/` and `handlers/` directories before writing new ones.',
            '- Check `manifest.json` for existing bot scopes before adding new permissions.',
            '- Identify the Salesforce org alias the app integrates with before writing any org calls.',
            '',
            '## Framework',
            '- Use **Bolt.js** (`@slack/bolt`). Node.js >= 18 required.',
            '- All listener callbacks must be `async`.',
            '- Use `app.start()` for Socket Mode (development) and HTTP mode (production).',
            '- Organize listeners by type: `listeners/commands/`, `listeners/actions/`, `listeners/events/`, `listeners/shortcuts/`.',
            '',
            '## App Manifest',
            '- Always maintain `manifest.json` in version control.',
            '- Bot token scopes must be **minimal** — request only what the feature requires.',
            '- Use **Socket Mode** for local development. Switch to HTTP for production deployments.',
            '- After any manifest change, reinstall the app to the workspace.',
            '',
            '## Event Listeners',
            '- Register listeners with: `app.message()`, `app.action()`, `app.shortcut()`, `app.command()`.',
            '- **Always call `ack()` within 3 seconds** — Slack will retry after 3 seconds if no acknowledgement.',
            '- Perform long-running work asynchronously after `ack()`. Use `respond()` or `say()` for deferred responses.',
            '- Never block the event loop in a listener body.',
            '',
            '## Salesforce Integration',
            '- Use `jsforce` or the SF CLI session for org access from the Slack app.',
            '- **Never hardcode org credentials** — use environment variables or a Secrets Manager.',
            '- Authenticate to Salesforce using JWT Bearer Flow or Named Credentials for production.',
            '- Store the org alias or instance URL in `process.env.SF_TARGET_ORG` or equivalent.',
            '',
            '## Security',
            '- Verify Slack request signatures on every webhook endpoint.',
            '- Use `SLACK_SIGNING_SECRET` environment variable — Bolt validates signatures automatically.',
            '- **Never log token values** — redact all `xoxb-`, `xoxp-`, and `xapp-` prefixed strings.',
            '- Rotate tokens if accidental exposure occurs. Audit token usage quarterly.',
            '',
            '## Error Handling',
            '- Wrap all listener bodies in `try/catch`.',
            '- Use `logger.error()` for structured error logging.',
            '- Post a user-friendly error message to the channel or as an ephemeral message — never expose stack traces.',
            '- Handle Slack API rate limits: respect `Retry-After` headers and implement exponential backoff.',
            '',
            '## State Management',
            "- Use Bolt's built-in `installationStore` for multi-workspace apps.",
            '- For single-workspace apps, environment variables are sufficient.',
            '- Never store user tokens in plaintext databases — encrypt at rest.',
            '',
            '## Modals & Block Kit',
            '- Use **Block Kit Builder** (https://api.slack.com/tools/block-kit-builder) for UI design.',
            '- Validate all modal form submissions server-side — never trust client-submitted values.',
            '- Use `view.update()` to show a loading state while processing long-running modal submissions.',
            '- Keep modals focused: one task per modal. Avoid nested modals.',
            '',
            '## Agentforce in Slack',
            '- **Agentforce agents are first-class Slack citizens** — design agents to render natively in Slack via the Agentforce Experience Layer.',
            '- Custom Agentforce agents in Slack grew **300% since January 2026** — this is the primary Engagement layer of Headless 360.',
            '- **Two integration patterns:**',
            '  - **Experience Layer (recommended):** agent behavior defined in Agentforce, rendered in Slack via the Experience Layer — no Bolt.js code needed for rendering.',
            '  - **Bolt.js + Agentforce API:** invoke Agentforce agents programmatically from a Bolt.js listener when custom pre/post-processing is required.',
            '- For new Agentforce-in-Slack implementations: use the Experience Layer pattern first. Only fall back to Bolt.js integration when the Experience Layer cannot meet the requirement.',
            '- Agent responses in Slack must follow Block Kit structure — use cards and decision tiles for structured outputs.',
            '- **Human escalation:** when an Agentforce agent in Slack cannot resolve a request, route to a human via Slack DM or a designated support channel — never silently drop the conversation.',
            '- Test Agentforce-in-Slack flows in a dedicated Slack test workspace. Verify `ack()` is called before any agent invocation.',
            '',
            '## Testing',
            '- Use **Jest** with `@slack/bolt` test helpers.',
            '- Mock the Bolt `App` client for unit tests — do not make real Slack API calls in tests.',
            '- Use a Slack test workspace for integration tests.',
            '- Test that `ack()` is always called within listener handlers.',
            '',
            ...generatedPromptsProtocol(),
            '',
            ...documentationStandards(),
            '',
            ...semanticCommits(),
            '',
            ...interactionPreferences('Bolt.js and Salesforce integration'),
            '',
            '## Sub-agent Handover',
            '- Pass: Bolt app file path, Salesforce org alias, Block Kit payload, and Slack event type.',
            '- When the task touches business object design (SLAs, approvals, milestones, routing rules,',
            '  entitlements), delegate evaluation to **Architect** or **BA** before proposing a custom',
            '  Slack workflow or canvas that duplicates native Salesforce approval or case management.',
        ].join('\n');
    },
};
//# sourceMappingURL=slack.js.map