/*
 * 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 { generateServiceWorkflows } from '../generators/workflows/service.js';
import { consultativeDesign, deployment, documentationStandards, generatedPromptsProtocol, getSharedSectionLines, interactionPreferences, salesforceReferences, semanticCommits, } from './shared-sections.js';
export const serviceProfile = {
    id: 'service',
    label: 'Service Cloud',
    ruleFile: 'service-standards.mdc',
    extensions: ['salesforce.salesforcedx-vscode'],
    capabilities: ['deploy', 'code-analysis', 'prompt-registry', 'declare-points'],
    cursorScope: 'description',
    detect(cwd) {
        return (existsSync(join(cwd, 'force-app/main/default/objects/Case')) ||
            existsSync(join(cwd, 'force-app/main/default/entitlementProcesses')) ||
            existsSync(join(cwd, 'force-app/main/default/bots')));
    },
    workflows: generateServiceWorkflows,
    ruleContent() {
        return [
            '---',
            'description: Salesforce Service Cloud Standards',
            'globs:',
            'alwaysApply: true',
            '---',
            '',
            '# Salesforce Service Cloud Standards',
            '',
            '> Role: Service Cloud Consultant — Salesforce Professional Services.',
            '',
            ...consultativeDesign('service case management and entitlement decisions'),
            '',
            '## Case Management',
            '- **Case Assignment Rules:** Define criteria-based rules (origin, subject keywords, record type) before using round-robin queues. Document the rule logic.',
            '- **Escalation Rules:** Set time-based escalation triggers tied to business hours. Escalate to a queue, not a specific user — queues survive re-orgs.',
            '- **Case Teams:** Use predefined Case Team Roles (Internal Agent, Supervisor, Subject Matter Expert). Pre-populate teams via assignment rules.',
            '- **Case Queues:** Name queues by channel + tier: `Email_Tier1`, `Chat_Tier2`. Assign Permission Set to control who can pull from each queue.',
            '- **Email-to-Case:** Use On-Demand Email-to-Case for TLS support. Configure routing addresses per product/region. Set thread-ID format.',
            '- **Web-to-Case:** Validate all input fields. Use a CAPTCHA or rate limit to prevent spam.',
            '- **Case Merge:** Enable Case Merge for duplicate detection. Define the master record selection rule (most recent activity or highest case number).',
            '- **Case Status Lifecycle:** New → Open → Pending Customer → Escalated → Resolved → Closed. Map transitions to business process, not technical convenience.',
            '',
            '## Entitlement Processes & Milestones',
            '- **Entitlement Processes:** Define one process per SLA tier (Standard, Premium, Enterprise). Attach to Accounts and Assets.',
            '- **Business Hours:** Create dedicated Business Hours records per region/timezone. Reference in entitlement processes and escalation rules.',
            '- **Milestones:** Define 2–4 milestones per process: First Response, Assignment, Resolution. Set time-based criteria in business hours.',
            '- **Milestone Actions:** Use auto-responses (email alerts) at 50%/75%/100% elapsed. Reassign at 90% to escalation queue.',
            '- **SLA Clock Behavior:** Pause-and-resume requires Entitlement Milestone Stops (set status = "Pending Customer" to pause). Document pause rules explicitly.',
            '- **Entitlement Verification in Apex:** Use `EntitlementProcess` and `Milestone` objects when querying SLA data. Never hardcode milestone names.',
            '- **Before proposing any custom SLA or milestone object:** verify that Entitlements,',
            '  Milestones, Milestone Actions, and Escalation Rules cover the requirement.',
            '  Reference: https://help.salesforce.com/s/articleView?id=sf.entitlements_overview.htm',
            '  If native configuration is insufficient, document the gap in an ADR before designing a custom object.',
            '',
            '## Knowledge Management',
            '- **Article Types / Record Types:** Use record types to separate FAQ, How-To, and Reference articles. Each type should have a dedicated page layout.',
            '- **Data Categories:** Design a 2-level hierarchy (top: product/domain, child: sub-topic). Categories drive search and visibility rules.',
            '- **Publishing Workflow:** Draft → In Review → Published. Use Approval Processes for external articles. Assign review to a dedicated Knowledge team queue.',
            '- **Article Versioning:** Every edit creates a new version. Archive instead of delete. Set a review reminder every 6 months using a scheduled flow.',
            '- **Knowledge Search Tuning:** Promote articles with high deflection scores. Use keyword synonyms. Surface articles in Lightning Service Console sidebar.',
            '- **Smart Links:** Use Knowledge Article Version IDs for stable links. Never link to article URLs that contain record IDs — they change with drafts.',
            '- **Case Deflection Metrics:** Track article views from case, article attach rate, and CSAT correlation per article.',
            '',
            '## Omni-Channel Routing',
            '- **Queue-Based Routing:** Assign work items to queues; agents pull manually. Simpler to configure; use for low-volume or unstructured work.',
            '- **Skills-Based Routing:** Route work items to agents with matching skills and capacity. Requires Skill definitions and agent skill assignments.',
            '- **Capacity Model:** Define capacity units per channel (chat = 3 units, voice = 5, email = 1). Total agent capacity = sum across all active work.',
            '- **Presence Configuration:** Create Presence Statuses per channel (Available for Chat, Available for Email). Control which statuses allow which work types.',
            '- **Routing Priority:** Higher priority work items pre-empt lower priority. Set priority at the queue level. Use flow to set priority dynamically.',
            '- **Supervisor Panel:** Use Omni-Channel Supervisor in Service Console. Configure real-time monitoring for queue depth, handle time, and agent availability.',
            '',
            '## Agentforce for Service (Einstein Service Agent)',
            '- **Einstein Service Agent is the strategic direction** for new service bot implementations. Einstein Bots (NLU-based) remain supported but are not the recommended path for new projects.',
            '- Einstein Service Agent uses LLM + grounding (Knowledge, Data Cloud, CRM records) — no intent training required.',
            '- **When to use Einstein Service Agent:** new deflection use cases, knowledge-intensive interactions, accounts already on Data Cloud.',
            '- **When to retain Einstein Bots:** existing production bots with mature NLU models, strict latency SLAs, or no LLM budget.',
            '- **Topic design:** define topics with clear scope boundaries. Each topic maps to a set of actions (Flows, Apex, Knowledge search).',
            '- **Handoff to human:** configure the Transfer to Agent topic. Pre-populate case summary fields before transfer using context variables.',
            '- **Guardrails:** always define out-of-scope topics explicitly. Never allow the agent to execute DML or financial actions without human confirmation.',
            '- Monitor: containment rate, deflection rate, handoff rate, and CSAT correlation.',
            '',
            '## Einstein Bots (Legacy NLU)',
            '- **Dialog Design:** Start with: Greeting → Intent Recognition → Main Menu → Task-specific dialogs → Handoff/Close.',
            '- **NLU Intent Training:** Provide 20+ training utterances per intent. Use the Intent Model accuracy dashboard to track precision and recall.',
            '- **Entity Extraction:** Define entities for key data (case number, order ID, product name). Use system entities for dates and numbers.',
            '- **Bot-to-Agent Handoff:** Use the Transfer to Agent dialog with context variables. Pre-populate case fields from bot conversation before transfer.',
            '- **Bot Analytics:** Monitor deflection rate, containment rate, and transfer rate. Set alerts for high transfer rates (>60% may indicate poor intent coverage).',
            '- **Multi-Language:** Configure language-specific intent models per locale. Use Custom Labels for bot response text.',
            '',
            '## Messaging for In-App and Web (MIAW)',
            '- **MIAW is the current Digital Engagement channel** for web and in-app chat. Legacy Live Agent / Snap-ins are in maintenance mode.',
            '- Setup: Create an Embedded Service Deployment (Messaging for Web) → configure the Chat Button → publish to Lightning pages or external sites via the snippet.',
            '- **Session routing:** MIAW sessions route via Omni-Channel using the same queue/skills model as other channels.',
            '- **Session lifecycle:** sessions are persistent (user can return and resume). Design conversation context accordingly.',
            '- **Link to Case:** create a Flow that generates a Case on first inbound message and associates the Messaging Session to the Case.',
            '- **Consent:** configure consent settings at the Embedded Service level. Respect opt-in/opt-out for proactive messaging.',
            '- **Einstein Bot + MIAW:** pair an Einstein Service Agent (or Bot) as the first-contact handler before routing to a human.',
            '- Test MIAW on mobile viewport — the widget must be functional at 320px width.',
            '',
            '## Macros & Quick Text',
            '- **Macro Structure:** Header (set subject/status) → Instructions (body text) → Close action (set status = Closed). Keep macros atomic.',
            '- **Quick Text:** Organize by category (Greeting, Troubleshooting, Escalation, Closing). Grant access via Permission Set.',
            '- **Permission:** Create a Permission Set `ServiceAgent_Macros` — assign only to agents who need each macro. Avoid broad profile-level access.',
            '',
            '## Messaging Channels',
            '- **Channel Setup:** Configure SMS/WhatsApp/Facebook via Messaging in Setup. Each channel = one Messaging Channel record + one Routing Config.',
            '- **Messaging Sessions:** Sessions are separate from Cases. Link via a flow that creates a case on first inbound message and associates subsequent messages.',
            '- **Consent Management:** WhatsApp requires opt-in. Track consent in a custom field on Contact. Block outbound if `MessagingConsentStatus != OptedIn`.',
            '- **Session Routing:** Route messaging sessions via Omni-Channel using the same skills/queue model as other channels.',
            '',
            '## Service Cloud Voice',
            '- **Service Cloud Voice** integrates telephony directly into the Service Console via Amazon Connect (or partner telephony).',
            '- Setup: enable Voice in Setup → configure an Amazon Connect instance → assign Voice permission sets to agents.',
            '- **Real-Time Transcription:** voice calls are transcribed live in the console. Use transcripts to trigger Knowledge suggestions and next-best-action recommendations.',
            '- **Einstein Real-Time Coaching:** surfaces recommended responses and article suggestions to agents during the call based on live transcript keywords.',
            '- **Omni-Channel routing for voice:** voice work items route through the same Omni-Channel model as chat and email — configure capacity and presence statuses accordingly.',
            '- **Wrap-Up:** configure post-call wrap-up time in Omni-Channel settings. Use a Flow to auto-populate case fields from the call transcript on wrap-up.',
            '- **Recording:** enable call recording at the Amazon Connect level. Ensure compliance with local recording consent laws before enabling.',
            '- **Testing:** test voice flows in a sandbox with a test Amazon Connect instance. Never use production telephony for development testing.',
            '',
            '## Case vs Work Order Decision',
            '- **Use Case when:** the customer needs support, resolution involves knowledge/troubleshooting, SLA tracking via Milestones is required, and the resolution is primarily digital (email, chat, phone).',
            '- **Use Work Order when:** resolution requires a physical dispatch, parts/inventory are involved, scheduling via the FSL engine is needed, or mobile technician offline access is required.',
            '- **Mixed scenarios:** a Case can trigger a Work Order — use a Record-Triggered Flow (After Save on Case status change) to auto-create a Work Order when field dispatch is determined.',
            '- **Never replicate data** between Case and Work Order — link via the `Case__c` lookup on Work Order and surface Case context on the mobile layout.',
            '- Document the Case → Work Order trigger criteria in the process design spec before implementation.',
            '',
            ...generatedPromptsProtocol(),
            '',
            ...documentationStandards(),
            '',
            ...deployment(),
            '',
            ...semanticCommits(),
            '',
            ...salesforceReferences(),
            ...getSharedSectionLines('service'),
            '',
            ...interactionPreferences('service configuration'),
        ].join('\n');
    },
};
//# sourceMappingURL=service.js.map