# Entity Sourcing Presentation (4-Phase Flow)

> **When the analysis detects a referenced entity/concept (e.g., "Projets" referenced by activities, "Clients" referenced by orders), ALWAYS analyze and recommend BEFORE asking the client.**
> This pattern detects potential new modules not mentioned in the original brief.

## PHASE 1 — ULTRATHINK: Silent Architecture Analysis (NO output to client)

> **The AI reasons about the entity BEFORE presenting options. This is internal thinking only.**

```
ULTRATHINK — Entity Architecture Analysis for {entity_name}:

1. DOMAIN COHERENCE (from decision tree in reference)
   - Application domain: {app_description}
   - Entity fit: CORE | SUPPORT | PERIPHERAL
   - Justification: {1 sentence — why this entity is/isn't core to the app}
   - Example: "Projets" in a time management app = CORE (without projects, time entries have no target)
   - Example: "Codes d'imputation" in a time management app = SUPPORT (useful for accounting, but not central)

2. ENTITY SCORING (5 criteria, 0-3 each — from scoring grid in reference)
   - Lifecycle:  {score}/3 — {brief justification}
   - Actors:     {score}/3 — {brief justification}
   - Relations:  {score}/3 — {brief justification}
   - Volume:     {score}/3 — {brief justification}
   - Autonomy:   {score}/3 — {brief justification}
   - TOTAL:      {total}/15 → {classification: config_list | in_app | light_module | full_module}

3. PRE-CLASSIFICATION CHECK (from entity patterns in reference)
   - Does {entity_name} match a known pattern? → {yes: which one | no: use scoring}
   - Override needed? → {yes: why | no}

4. RECOMMENDATION
   - Option: {config_list | in_app_section | new_module | external_system}
   - Confidence: HIGH | MEDIUM | LOW
   - Reasoning: {1-2 sentences}
   - Risk if wrong choice: {what goes wrong if user picks a different option}

5. IF recommendation = new_module → PRE-BUILD ARCHITECTURE
   - Module code: {PascalCase}
   - Section `list`: {main entity grid + create action + navigation to detail}
   - Detail page tabs: [{tab_name: content}] (accessible by clicking a row in list)
   - Additional sections: [{code: justification}] (ONLY functional sections: dashboard, approve, import, rapport...)
   - Dependencies: [{which modules reference/are referenced by this entity}]
   - Topological position: layer {N} (foundation if no dependencies)
```

## PHASE 2 — Present Analysis & Recommendation to Client

**BEFORE the AskUserQuestion, display the analysis as markdown:**

```
### {language == "fr" ? "Analyse" : "Analysis"} : {entity_plural}

{language == "fr"
  ? "Les **{entity_plural}** sont référencés par {parent_module} ({relationship_description})."
  : "**{entity_plural}** are referenced by {parent_module} ({relationship_description})."}

{language == "fr" ? "**Mon analyse :**" : "**My analysis:**"}
- {language == "fr" ? "Cohérence domaine" : "Domain coherence"} : **{CORE|SUPPORT|PERIPHERAL}** — {justification}
- {language == "fr" ? "Score de complexité" : "Complexity score"} : **{score}/15** ({classification})
- {language == "fr" ? "Recommandation" : "Recommendation"} : **{recommended_option}** — {reasoning}
```

**IF recommendation = new_module AND confidence >= MEDIUM, ALSO display:**

```
{language == "fr" ? "**Architecture proposée :**" : "**Proposed architecture:**"}

| {language == "fr" ? "Niveau" : "Level"} | {language == "fr" ? "Valeur" : "Value"} | {language == "fr" ? "Justification" : "Rationale"} |
|--------|--------|---------------|
| Application | `{app_code}` | {app_name} |
| Module | `{ModuleCode}` | {language == "fr" ? "{pourquoi ce découpage}" : "{why this decomposition}"} |

| Section | {language == "fr" ? "Justification" : "Rationale"} | Resources |
|---------|---------------|-----------|
| `list` | {language == "fr" ? "Page principale : grille, création, navigation vers détail" : "Main page: grid, create action, navigate to detail"} | {entity}-grid, {entity}-filters, {entity}-form |
| {additional_section} | {justification} | {resources} |

{language == "fr"
  ? "**Page détail** (onglets accessibles depuis la liste) :"
  : "**Detail page** (tabs accessible from list):"}

| {language == "fr" ? "Onglet" : "Tab"} | {language == "fr" ? "Contenu" : "Content"} |
|--------|---------|
| {language == "fr" ? "Informations" : "Information"} | {language == "fr" ? "Fiche principale" : "Main record"} |
| {tab_name} | {tab_content} |

{language == "fr"
  ? "**Impact :** {ModuleName} deviendra une dépendance de {parent_module} (couche {N} du graphe)."
  : "**Impact:** {ModuleName} will become a dependency of {parent_module} (layer {N} in the graph)."}
```

## PHASE 3 — Ask with Recommended Option First

**Order the options with the recommendation FIRST, marked "(Recommandé)" / "(Recommended)":**

```
question: "{language == 'fr'
  ? 'Comment souhaitez-vous gérer les {entity_plural} ?'
  : 'How do you want to manage {entity_plural}?'}"
header: "{entity_name}"
options:
  (ORDER: recommended option FIRST, then the 3 remaining options in logical order)

  IF recommendation = new_module:
  - label: "{language == 'fr' ? 'Nouveau module dédié (Recommandé)' : 'New dedicated module (Recommended)'}"
    description: "{language == 'fr'
      ? 'Module {ModuleCode} avec page liste, détail par onglets, permissions dédiées'
      : '{ModuleCode} module with list page, tabbed detail, dedicated permissions'}"
  - label: "{language == 'fr' ? 'Géré dans l\'app' : 'Managed in app'}"
    description: "..."
  - label: "{language == 'fr' ? 'Système externe' : 'External system'}"
    description: "..."
  - label: "{language == 'fr' ? 'Liste simple en config' : 'Simple config list'}"
    description: "..."

  IF recommendation = config_list:
  - label: "{language == 'fr' ? 'Liste simple en config (Recommandé)' : 'Simple config list (Recommended)'}"
    description: "{language == 'fr'
      ? 'Table de référence administrable, pas de module dédié'
      : 'Admin-managed reference table, no dedicated module'}"
  - label: "{language == 'fr' ? 'Géré dans l\'app' : 'Managed in app'}"
    description: "..."
  - label: "{language == 'fr' ? 'Nouveau module dédié' : 'New dedicated module'}"
    description: "..."
  - label: "{language == 'fr' ? 'Système externe' : 'External system'}"
    description: "..."

  IF recommendation = in_app_section:
  - label: "{language == 'fr' ? 'Géré dans l\'app (Recommandé)' : 'Managed in app (Recommended)'}"
    description: "{language == 'fr'
      ? 'Accessible via un onglet dans la page détail de {parent_module}'
      : 'Accessible via a tab in {parent_module} detail page'}"
  - label: "{language == 'fr' ? 'Nouveau module dédié' : 'New dedicated module'}"
    description: "..."
  - label: "{language == 'fr' ? 'Liste simple en config' : 'Simple config list'}"
    description: "..."
  - label: "{language == 'fr' ? 'Système externe' : 'External system'}"
    description: "..."

  IF recommendation = external_system:
  - label: "{language == 'fr' ? 'Système externe (Recommandé)' : 'External system (Recommended)'}"
    description: "{language == 'fr'
      ? 'Les {entity_plural} viennent d\'un autre système (import ou API)'
      : '{entity_plural} come from another system (import or API)'}"
  - label: "{language == 'fr' ? 'Nouveau module dédié' : 'New dedicated module'}"
    description: "..."
  - label: "{language == 'fr' ? 'Géré dans l\'app' : 'Managed in app'}"
    description: "..."
  - label: "{language == 'fr' ? 'Liste simple en config' : 'Simple config list'}"
    description: "..."

  IF confidence = LOW (no clear recommendation):
  (all 4 options in neutral order, NO "(Recommandé)" tag)
```

## PHASE 4 — Process Answer

| Choice | Action |
|--------|--------|
| **Géré dans l'app** | Entity becomes a sub-entity of the parent module. Add as a **tab in the detail page** of the referencing module. Note in coverageMatrix with `parentModule` field. |
| **Nouveau module dédié** | **Add new module to `{pre_analysis}.detected_modules[]`** with the proposed architecture (sections, detail tabs, dependencies). Add to `coverageMatrix` as inScope. The `list` section is ALWAYS the main section. Additional sections ONLY for distinct functional sections (dashboard, approve, import...). `create` and `edit` are ACTIONS within `list` and detail pages, NEVER standalone sections. |
| **Système externe** | Flag for integration specification. Add to `coverageMatrix` as integration. Load questionnaire materials on integrations if not already covered. |
| **Liste simple en config** | Entity becomes a lookup/reference table (no dedicated module, no section). Note in coverageMatrix as config data. |

> **CRITICAL:** The intelligence of this step is in the ULTRATHINK analysis. The AI must REASON about domain coherence, score the entity, and recommend — not just present 4 neutral options. The client can always override the recommendation, but they should benefit from expert architectural guidance.
