/*
 * 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 { workflow } from '../workflow-generator.js';
export function generatePublicSectorWorkflows(version) {
    return {
        'licensing-permitting-setup.md': workflow(version, 'Licensing & Permitting Setup', `
## 1. Gather Requirements

Ask:
- **License types:** What types of licenses or permits does the agency issue? (Business License, Occupational Permit, Alcohol License, Building Permit, etc.)
- **Application intake:** How do applicants submit? (Portal via Experience Cloud, OmniScript guided form, paper → manual entry)
- **Review process:** Single reviewer, multi-step approval, or committee-based?
- **Fee structure:** Flat fees, tiered by license type, or calculated based on application attributes (square footage, employee count)?
- **Renewal model:** Auto-renewal with payment, manual reapplication, or lapse-and-reapply?
- **Integration needs:** Payment gateway for fees? GIS for location validation? Legacy licensing system migration?

Check \`force-app/main/default/objects/BusinessLicenseApplication/\` for existing field customizations.

## 2. Configure Application Intake

### OmniScript Guided Application
1. Create an OmniScript for each license type (e.g., \`BusinessLicenseApp_Intake\`).
2. Steps: Applicant Info → Business Details → Location → Document Upload → Fee Preview → Submit.
3. Use DataRaptor to transform OmniScript output into \`BusinessLicenseApplication\` record.
4. Attach uploaded documents via ContentDocumentLink.

### Experience Cloud Portal
1. Configure the Public Sector portal template in Experience Builder.
2. Add the OmniScript component for guided intake.
3. Ensure guest user access is restricted — applicants must authenticate.
4. Enable document upload with file type and size restrictions.

### Application Record Structure
- \`BusinessLicenseApplication\` — the application record (Status: Draft → Submitted → Under Review → Approved → Issued → Denied)
- \`BusinessLicense\` — the issued license (created upon approval)
- \`RegulatoryTrxnFee\` — fees associated with the application

## 3. Configure Review Process

### Approval Workflow
1. Define Approval Process on \`BusinessLicenseApplication\`:
   - Entry criteria: Status = "Submitted"
   - Approver: Queue-based (License Review Queue) or role-based
   - Approval actions: Status → "Approved", trigger license issuance
   - Rejection actions: Status → "Denied", send rejection notification
2. For multi-step reviews: use sequential approval steps (e.g., Initial Review → Compliance Check → Final Approval).
3. For committee reviews: use unanimous approval with multiple approvers per step.

### Automated Eligibility Checks
- Use Flow to validate application completeness before routing to review queue.
- Check: all required documents attached, fees paid, no outstanding violations on applicant.

## 4. Configure Fee Schedules

### Fee Calculation
1. Create Custom Metadata Type \`LicenseFeeSchedule__mdt\` with fields:
   - \`LicenseType__c\` (Text) — maps to license Record Type
   - \`FeeAmount__c\` (Currency) — base fee amount
   - \`RenewalFeeAmount__c\` (Currency) — renewal fee
   - \`LatePenaltyPercent__c\` (Percent) — late renewal penalty
2. Create \`RegulatoryTrxnFee\` records via Flow when application is submitted.
3. Integrate payment gateway (Stripe, PayPal Gov, US Bank) via Named Credential callout.

### Fee Payment Tracking
- \`RegulatoryTrxnFee.Status\` tracks: Assessed → Paid → Waived → Refunded
- Block approval progression until all fees are in Paid or Waived status.

## 5. Configure License Issuance

### Issuance Flow (After Approval)
1. Trigger: \`BusinessLicenseApplication.Status\` = "Approved"
2. Create \`BusinessLicense\` record:
   - Copy applicant info, license type, effective dates
   - Set \`ExpirationDate\` based on license type duration
   - Set \`Status\` = "Active"
3. Generate PDF permit document (use Flow + Document Generation template).
4. Attach PDF to the \`BusinessLicense\` record via ContentDocumentLink.
5. Send issuance notification to applicant (email + portal notification).

### Renewal Automation
1. Scheduled Flow: 60 days before \`BusinessLicense.ExpirationDate\`, send renewal notice.
2. Create a new \`BusinessLicenseApplication\` with \`ApplicationType\` = "Renewal" and link to prior license.
3. Pre-populate renewal application with prior license data.
4. If not renewed by expiration: set \`BusinessLicense.Status\` = "Expired".

## 6. Deploy Licensing Configuration

\`\`\`bash
sf project deploy start \\
  --source-dir force-app/main/default/objects/BusinessLicenseApplication \\
  --source-dir force-app/main/default/objects/BusinessLicense \\
  --source-dir force-app/main/default/objects/RegulatoryTrxnFee \\
  --source-dir force-app/main/default/customMetadata \\
  --source-dir force-app/main/default/flows \\
  --target-org <alias>
\`\`\`

Verify:
- Application intake OmniScript renders correctly in the portal
- Fee calculation creates correct \`RegulatoryTrxnFee\` records
- Approval process routes to correct queue
- License issuance creates \`BusinessLicense\` with PDF attachment
- Renewal notices fire 60 days before expiration
`),
        'inspection-management-setup.md': workflow(version, 'Inspection Management Setup', `
## 1. Gather Requirements

Ask:
- **Inspection types:** What kinds of inspections does the agency perform? (Health, Fire, Building, Environmental, Food Safety)
- **Scheduling model:** Inspector-initiated, scheduled by dispatcher, or triggered by license milestones?
- **Checklist structure:** Standard checklist per inspection type, or dynamic based on facility attributes?
- **Violation tracking:** How are violations categorized? (Critical, Major, Minor) What is the corrective action timeline?
- **Mobile requirements:** Do inspectors use mobile devices in the field? Offline capability needed?
- **Integration needs:** GIS for route optimization? Legacy inspection database migration?

Check \`force-app/main/default/objects/Inspection/\` and \`force-app/main/default/objects/Visit/\` for existing configurations.

## 2. Configure Inspection Types

### InspectionType Setup
1. Create \`InspectionType\` records for each category:
   - Health Inspection, Fire Safety Inspection, Building Code Inspection, Environmental Inspection
2. Set inspection frequency per type (annual, semi-annual, complaint-triggered).
3. Link inspection types to \`RegulatoryCode\` records that define applicable regulations.

### Checklist Templates
1. Create checklist item templates per inspection type using \`InspectionType\` related lists.
2. Each checklist item maps to a specific \`RegulatoryCode\` section.
3. Define pass/fail/N-A options per item.
4. Include required photo evidence fields for critical items.

## 3. Configure Scheduling

### Dispatcher-Based Scheduling
1. Create \`Visit\` records linked to the facility (\`Account\` or \`BusinessLicense\`).
2. Assign inspector via \`Visit.OwnerId\` or a custom \`Inspector__c\` lookup.
3. Set \`ScheduledStartTime\` and \`ScheduledEndTime\`.
4. Use Omni-Channel or queue-based assignment for workload balancing.

### Milestone-Triggered Scheduling
1. Flow: When \`BusinessLicense\` is issued, create a \`Visit\` record for initial inspection.
2. Set inspection due date based on license type (e.g., food establishment → 30 days after issuance).
3. Recurrence: After each completed inspection, schedule the next based on frequency rules.

### GIS Integration (Optional)
- Use Named Credential callout to GIS service for route optimization.
- Store facility coordinates on Account (Geolocation field).
- Batch inspector routes by geographic cluster.

## 4. Configure Inspection Execution

### Inspection Record
1. Create \`Inspection\` record when inspector begins the visit:
   - Link to \`Visit\`, \`BusinessLicense\`, and \`InspectionType\`
   - Status: Scheduled → In Progress → Completed → Follow-Up Required
2. Populate checklist items from the \`InspectionType\` template.
3. Inspector marks each item: Pass, Fail, N/A with optional notes and photo.

### Mobile Configuration
1. Configure Salesforce Mobile App or Field Service Mobile for offline inspection.
2. Enable offline sync for \`Inspection\`, \`Visit\`, and checklist objects.
3. Configure photo capture fields for evidence documentation.
4. Set up mobile action bar: Complete Inspection, Log Violation, Request Follow-Up.

## 5. Configure Violation Tracking

### Violation Creation
1. When a checklist item is marked "Fail":
   - Auto-create \`RegulatoryCodeViolation\` record via Flow.
   - Link to \`Inspection\`, \`RegulatoryCode\`, and facility \`Account\`.
   - Set severity: Critical, Major, Minor (based on \`RegulatoryCode\` classification).
2. Set corrective action deadline based on severity:
   - Critical: 24 hours
   - Major: 7 days
   - Minor: 30 days

### Corrective Action Plans
1. Create corrective action Task records linked to the \`RegulatoryCodeViolation\`.
2. Assign to facility owner/contact with deadline.
3. Status tracking: Open → In Progress → Submitted for Review → Verified → Closed.
4. Re-inspection scheduling: When corrective action is "Submitted for Review", create a follow-up \`Visit\`.

### Penalty Assessment
1. For unresolved violations past deadline:
   - Create \`RegulatoryTrxnFee\` with \`FeeType\` = "Penalty"
   - Calculate penalty amount based on \`RegulatoryCode\` and severity
   - Escalation: repeated violations → license suspension workflow
2. Track penalty payment status on \`RegulatoryTrxnFee.Status\`.

## 6. Deploy Inspection Configuration

\`\`\`bash
sf project deploy start \\
  --source-dir force-app/main/default/objects/Inspection \\
  --source-dir force-app/main/default/objects/InspectionType \\
  --source-dir force-app/main/default/objects/Visit \\
  --source-dir force-app/main/default/objects/RegulatoryCodeViolation \\
  --source-dir force-app/main/default/objects/RegulatoryCode \\
  --source-dir force-app/main/default/flows \\
  --target-org <alias>
\`\`\`

Verify:
- Inspection types have correct checklist templates
- Scheduling creates \`Visit\` records with correct assignment
- Checklist items populate from template on inspection start
- Failed items auto-create \`RegulatoryCodeViolation\` records
- Corrective action deadlines calculate correctly by severity
- Penalty fees generate for unresolved violations past deadline
- Re-inspection visits schedule when corrective action is submitted
`),
    };
}
//# sourceMappingURL=public-sector.js.map