/*
 * 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 generateAutomotiveWorkflows(version) {
    return {
        'vehicle-lifecycle-setup.md': workflow(version, 'Vehicle Lifecycle Setup', `
## 1. Gather Requirements

Ask:
- **Vehicle catalog:** How are vehicle definitions structured? (Make, Model, Year, Trim, Engine, Transmission)
- **Inventory model:** How do physical vehicles enter the system? (OEM feed, manual entry, DMS sync)
- **VIN management:** Is VIN the primary identifier? What validation rules apply?
- **Sale tracking:** What stages does a vehicle go through from inventory to sold? (Available, Reserved, In Transit, Sold, Delivered)
- **Service lifecycle:** After sale, how are service appointments and warranty tracked?
- **Trade-in flow:** How are trade-in valuations captured and linked to new sales?

Check \`force-app/main/default/objects/\` for existing \`Vehicle__c\` or \`VehicleDefinition__c\` customizations before adding new fields.

## 2. Configure the Vehicle Catalog (VehicleDefinition__c)

The vehicle catalog represents the abstract product definition — not a physical unit.

### Key Fields
| Field | Purpose |
|-------|---------|
| Make | Manufacturer (e.g., Toyota, Ford, BMW) |
| Model | Model name (e.g., Camry, F-150, X5) |
| Year | Model year |
| Trim | Trim level (e.g., SE, XLE, Limited) |
| Engine Type | Engine specification (e.g., 2.5L I4, 3.5L V6, Electric) |
| Transmission | Transmission type (Automatic, Manual, CVT) |
| MSRP | Manufacturer Suggested Retail Price |
| Segment | Vehicle segment (Sedan, SUV, Truck, EV) |

### Configuration Steps
1. Verify \`VehicleDefinition__c\` object exists in the org.
2. Map business catalog fields to existing object fields.
3. Create Record Types by segment if page layouts differ significantly.
4. Set up validation rules for required catalog fields (Make + Model + Year minimum).

## 3. Configure Vehicle Inventory (Vehicle__c)

Each \`Vehicle__c\` record represents a physical unit identified by VIN.

### Key Fields
| Field | Purpose |
|-------|---------|
| VIN | Vehicle Identification Number (17 chars, unique) |
| Vehicle Definition | Lookup to \`VehicleDefinition__c\` |
| Status | Picklist: In Transit, In Stock, Reserved, Sold, Delivered |
| Stock Number | Dealer-assigned stock number |
| Color (Exterior) | Exterior color |
| Color (Interior) | Interior color |
| Dealer | Lookup to dealer Account |
| List Price | Dealer asking price |
| Arrival Date | Expected or actual arrival at dealership |

### Inventory Status Flow
\`\`\`
In Transit → In Stock → Reserved → Sold → Delivered
                ↓
           Trade-In (used vehicle re-enters as In Stock)
\`\`\`

### VIN Validation
- Implement a validation rule: VIN must be exactly 17 alphanumeric characters (excluding I, O, Q).
- Use a formula or Apex trigger handler to compute the VIN check digit (position 9).

## 4. Configure Sale Tracking

Link vehicle sales to standard Opportunity:
1. Add a \`Vehicle__c\` lookup on Opportunity (or use the Automotive Cloud managed relationship).
2. Define Opportunity stages that mirror the vehicle sale lifecycle:
   - Inquiry → Test Drive → Negotiation → Finance Approval → Closed Won → Delivered
3. Set up a Record-Triggered Flow: when Opportunity = Closed Won, update \`Vehicle__c.Status\` to "Sold".
4. Track trade-in vehicles via a related \`TradeIn\` record linked to the Opportunity.

## 5. Deploy Vehicle Lifecycle Metadata

\`\`\`bash
sf project deploy start \\
  --source-dir force-app/main/default/objects/VehicleDefinition__c \\
  --source-dir force-app/main/default/objects/Vehicle__c \\
  --source-dir force-app/main/default/flows \\
  --target-org <alias>
\`\`\`

Verify in the target org:
- Vehicle Definition records can be created with all catalog fields
- Vehicle records enforce VIN uniqueness and validation
- Opportunity closure updates Vehicle status automatically
- Trade-in records link correctly to both the old vehicle and the new sale
`),
        'dealer-management-setup.md': workflow(version, 'Dealer Management Setup', `
## 1. Gather Requirements

Ask:
- **Dealer structure:** How are dealers organized? (Single-location, multi-rooftop, dealer groups)
- **Performance metrics:** What KPIs are tracked? (Units sold, gross profit, CSI scores, service revenue)
- **Territory model:** How are territories assigned? (Geographic, brand, segment)
- **Incentive programs:** What manufacturer or group incentive programs exist? (Volume bonuses, stair-step, CSI-based)
- **Reporting cadence:** How often are dealer scorecards generated? (Daily, weekly, monthly)
- **Integration:** Does performance data come from a DMS (Dealer Management System)? Which one?

## 2. Configure Dealer Accounts

Dealers are modeled as Account records with a dedicated Record Type.

### Account Record Type Setup
1. Create Record Type: \`Dealer\` on Account object.
2. Key fields:
   - Dealer Code (unique external ID for DMS integration)
   - Brand Affiliation (multi-select picklist or junction object for multi-brand dealers)
   - Territory (lookup to Territory or picklist)
   - Dealer Tier (Gold, Silver, Bronze — based on performance)
   - Primary Contact (lookup to the dealer principal or GM)
3. For multi-rooftop dealers: use Account hierarchy (parent = dealer group, children = locations).

## 3. Configure Performance Tracking (DealerPerformance)

### DealerPerformance Object Setup
| Field | Purpose |
|-------|---------|
| Dealer | Lookup to dealer Account |
| Period | Date field or picklist (Monthly, Quarterly) |
| Units Sold (New) | Number of new vehicle sales |
| Units Sold (Used) | Number of used vehicle sales |
| Gross Profit | Total gross profit for the period |
| CSI Score | Customer Satisfaction Index score |
| Service Revenue | Total service department revenue |
| Parts Revenue | Total parts department revenue |
| Tier Achieved | Computed tier based on metrics |

### Automation
1. Record-Triggered Flow: On DealerPerformance insert/update, recalculate Tier Achieved.
2. Scheduled Flow (monthly): Generate DealerPerformance records from aggregated Opportunity data.
3. Platform Event or Apex Batch: Import performance data from DMS on a schedule.

## 4. Configure Territory Assignment

### Territory Model Options
- **Option A — Territory2 (Enterprise Territory Management):**
  Assign dealers to territories using Territory2 and ObjectTerritory2Association.
  Best for complex, overlapping territory rules.
- **Option B — Custom Territory Object:**
  Simpler model with a custom \`Territory__c\` object and a lookup on Account.
  Best for straightforward geographic assignments.

### Steps (Option A)
1. Enable Territory Management in Setup.
2. Define territory hierarchy: Region → District → Territory.
3. Create assignment rules based on dealer postal code or state.
4. Assign dealer Accounts to territories.

### Steps (Option B)
1. Create \`Territory__c\` object with fields: Name, Region, District, Manager (User lookup).
2. Add \`Territory__c\` lookup on the Dealer Account Record Type page layout.
3. Create sharing rules based on Territory ownership for dealer-level access.

## 5. Configure Incentive Programs

### Incentive Program Object
| Field | Purpose |
|-------|---------|
| Program Name | Name of the incentive program |
| Type | Volume Bonus, Stair-Step, CSI-Based, Mixed |
| Start Date / End Date | Program validity period |
| Threshold | Minimum metric value to qualify |
| Reward | Payout per unit or flat bonus |
| Eligible Tiers | Which dealer tiers qualify |

### Incentive Calculation
1. Scheduled Apex or Flow: At period end, evaluate each dealer against program thresholds.
2. Generate \`IncentivePayout\` records for qualifying dealers.
3. Surface payouts on the dealer Account page via a related list.

## 6. Deploy Dealer Management Metadata

\`\`\`bash
sf project deploy start \\
  --source-dir force-app/main/default/objects/DealerPerformance \\
  --source-dir force-app/main/default/objects/Account \\
  --source-dir force-app/main/default/flows \\
  --target-org <alias>
\`\`\`

Verify in the target org:
- Dealer Account Record Type with all required fields
- DealerPerformance records track metrics correctly
- Territory assignments reflect in sharing and reports
- Incentive program evaluation produces correct payouts
`),
    };
}
//# sourceMappingURL=automotive.js.map