# Templates DB Seed - Application Skill

> Ces templates génèrent les seeds EF Core pour les entités de navigation et leurs traductions.

---

## RÈGLES DE GÉNÉRATION GUID

```csharp
// JAMAIS de GUIDs séquentiels générés par NewGuid()
// TOUJOURS utiliser la méthode déterministe

private static Guid GenerateGuid(int index)
{
    // Format: 11111111-1111-1111-1111-{index:D12}
    return Guid.Parse($"11111111-1111-1111-1111-{index:D12}");
}

// Pour les seeds existants, continuer la séquence
// Vérifier le dernier index utilisé dans NavigationTranslationConfiguration.cs
```

---

## TEMPLATE: CONTEXT

### Navigation Entity

```csharp
// Dans NavigationContextConfiguration.cs - HasData()
new {
    Id = Guid.Parse("$CONTEXT_GUID"),
    Code = "$CODE",           // ex: "erp"
    Label = "$LABEL_FR",      // ex: "ERP"
    Description = "$DESC_FR", // ex: "Gestion d'entreprise"
    Icon = "$ICON",           // ex: "Building2"
    IconType = IconType.Lucide,
    Route = "/$CODE",         // ex: "/erp"
    DisplayOrder = $ORDER,    // ex: 4
    IsActive = true,
    CreatedAt = seedDate
}
```

### Translations (4 langues)

```csharp
// Dans NavigationTranslationConfiguration.cs - GetSeedData()
// Context translations
translations.Add(new {
    Id = GenerateGuid(index++),
    EntityType = NavigationEntityType.Context,
    EntityId = $CONTEXT_GUID,
    LanguageCode = "fr",
    Label = "$LABEL_FR",
    Description = "$DESC_FR",
    CreatedAt = seedDate
});
translations.Add(new {
    Id = GenerateGuid(index++),
    EntityType = NavigationEntityType.Context,
    EntityId = $CONTEXT_GUID,
    LanguageCode = "en",
    Label = "$LABEL_EN",
    Description = "$DESC_EN",
    CreatedAt = seedDate
});
translations.Add(new {
    Id = GenerateGuid(index++),
    EntityType = NavigationEntityType.Context,
    EntityId = $CONTEXT_GUID,
    LanguageCode = "it",
    Label = "$LABEL_IT",
    Description = "$DESC_IT",
    CreatedAt = seedDate
});
translations.Add(new {
    Id = GenerateGuid(index++),
    EntityType = NavigationEntityType.Context,
    EntityId = $CONTEXT_GUID,
    LanguageCode = "de",
    Label = "$LABEL_DE",
    Description = "$DESC_DE",
    CreatedAt = seedDate
});
```

---

## TEMPLATE: APPLICATION

### Navigation Entity

```csharp
// Dans NavigationApplicationConfiguration.cs - HasData()
new {
    Id = Guid.Parse("$APP_GUID"),
    ContextId = Guid.Parse("$CONTEXT_GUID"),
    Code = "$CODE",           // ex: "sales"
    Label = "$LABEL_FR",      // ex: "Ventes"
    Description = "$DESC_FR", // ex: "Gestion des ventes"
    Icon = "$ICON",           // ex: "TrendingUp"
    IconType = IconType.Lucide,
    Route = "/$CONTEXT/$CODE", // ex: "/erp/sales"
    DisplayOrder = $ORDER,
    IsActive = true,
    CreatedAt = seedDate
}
```

### Translations (4 langues)

```csharp
// Application translations
translations.Add(new {
    Id = GenerateGuid(index++),
    EntityType = NavigationEntityType.Application,
    EntityId = $APP_GUID,
    LanguageCode = "fr",
    Label = "$LABEL_FR",
    Description = "$DESC_FR",
    CreatedAt = seedDate
});
translations.Add(new {
    Id = GenerateGuid(index++),
    EntityType = NavigationEntityType.Application,
    EntityId = $APP_GUID,
    LanguageCode = "en",
    Label = "$LABEL_EN",
    Description = "$DESC_EN",
    CreatedAt = seedDate
});
translations.Add(new {
    Id = GenerateGuid(index++),
    EntityType = NavigationEntityType.Application,
    EntityId = $APP_GUID,
    LanguageCode = "it",
    Label = "$LABEL_IT",
    Description = "$DESC_IT",
    CreatedAt = seedDate
});
translations.Add(new {
    Id = GenerateGuid(index++),
    EntityType = NavigationEntityType.Application,
    EntityId = $APP_GUID,
    LanguageCode = "de",
    Label = "$LABEL_DE",
    Description = "$DESC_DE",
    CreatedAt = seedDate
});
```

---

## TEMPLATE: MODULE

### Navigation Entity

```csharp
// Dans NavigationModuleConfiguration.cs - HasData()
new {
    Id = Guid.Parse("$MODULE_GUID"),
    ApplicationId = Guid.Parse("$APP_GUID"),
    Code = "$CODE",                    // ex: "products"
    Label = "$LABEL_FR",               // ex: "Produits"
    Description = "$DESC_FR",          // ex: "Gestion des produits"
    Icon = "$ICON",                    // ex: "Package"
    IconType = IconType.Lucide,
    Route = "/$CONTEXT/$APP/$CODE",    // ex: "/erp/sales/products"
    DisplayOrder = $ORDER,
    IsActive = true,
    CreatedAt = seedDate
}
```

### Translations (4 langues)

```csharp
// Module translations
translations.Add(new {
    Id = GenerateGuid(index++),
    EntityType = NavigationEntityType.Module,
    EntityId = $MODULE_GUID,
    LanguageCode = "fr",
    Label = "$LABEL_FR",
    Description = "$DESC_FR",
    CreatedAt = seedDate
});
translations.Add(new {
    Id = GenerateGuid(index++),
    EntityType = NavigationEntityType.Module,
    EntityId = $MODULE_GUID,
    LanguageCode = "en",
    Label = "$LABEL_EN",
    Description = "$DESC_EN",
    CreatedAt = seedDate
});
translations.Add(new {
    Id = GenerateGuid(index++),
    EntityType = NavigationEntityType.Module,
    EntityId = $MODULE_GUID,
    LanguageCode = "it",
    Label = "$LABEL_IT",
    Description = "$DESC_IT",
    CreatedAt = seedDate
});
translations.Add(new {
    Id = GenerateGuid(index++),
    EntityType = NavigationEntityType.Module,
    EntityId = $MODULE_GUID,
    LanguageCode = "de",
    Label = "$LABEL_DE",
    Description = "$DESC_DE",
    CreatedAt = seedDate
});
```

---

## TEMPLATE: SECTION

### Navigation Entity

```csharp
// Dans NavigationSectionConfiguration.cs - HasData()
new {
    Id = Guid.Parse("$SECTION_GUID"),
    ModuleId = Guid.Parse("$MODULE_GUID"),
    Code = "$CODE",                           // ex: "inventory"
    Label = "$LABEL_FR",                      // ex: "Inventaire"
    Description = "$DESC_FR",                 // ex: "Gestion des stocks"
    Icon = "$ICON",                           // ex: "Warehouse"
    IconType = IconType.Lucide,
    Route = "/$CONTEXT/$APP/$MODULE/$CODE",   // ex: "/erp/sales/products/inventory"
    DisplayOrder = $ORDER,
    IsActive = true,
    CreatedAt = seedDate
}
```

### Translations (4 langues)

```csharp
// Section translations
translations.Add(new {
    Id = GenerateGuid(index++),
    EntityType = NavigationEntityType.Section,
    EntityId = $SECTION_GUID,
    LanguageCode = "fr",
    Label = "$LABEL_FR",
    Description = "$DESC_FR",
    CreatedAt = seedDate
});
translations.Add(new {
    Id = GenerateGuid(index++),
    EntityType = NavigationEntityType.Section,
    EntityId = $SECTION_GUID,
    LanguageCode = "en",
    Label = "$LABEL_EN",
    Description = "$DESC_EN",
    CreatedAt = seedDate
});
translations.Add(new {
    Id = GenerateGuid(index++),
    EntityType = NavigationEntityType.Section,
    EntityId = $SECTION_GUID,
    LanguageCode = "it",
    Label = "$LABEL_IT",
    Description = "$DESC_IT",
    CreatedAt = seedDate
});
translations.Add(new {
    Id = GenerateGuid(index++),
    EntityType = NavigationEntityType.Section,
    EntityId = $SECTION_GUID,
    LanguageCode = "de",
    Label = "$LABEL_DE",
    Description = "$DESC_DE",
    CreatedAt = seedDate
});
```

---

## TEMPLATE: PERMISSIONS SEED

### Permission Entity

```csharp
// Dans PermissionConfiguration.cs - HasData()
// Pour chaque action CRUD + assign + execute

// Read
new {
    Id = Guid.Parse("$PERM_READ_GUID"),
    Code = "$CONTEXT.$APP.$MODULE.read",
    Name = "View $MODULE_LABEL",
    Description = "View $MODULE_LABEL list and details",
    Category = "$CONTEXT.$APP.$MODULE",
    CreatedAt = seedDate
},
// Create
new {
    Id = Guid.Parse("$PERM_CREATE_GUID"),
    Code = "$CONTEXT.$APP.$MODULE.create",
    Name = "Create $MODULE_LABEL",
    Description = "Create new $MODULE_LABEL",
    Category = "$CONTEXT.$APP.$MODULE",
    CreatedAt = seedDate
},
// Update
new {
    Id = Guid.Parse("$PERM_UPDATE_GUID"),
    Code = "$CONTEXT.$APP.$MODULE.update",
    Name = "Update $MODULE_LABEL",
    Description = "Modify existing $MODULE_LABEL",
    Category = "$CONTEXT.$APP.$MODULE",
    CreatedAt = seedDate
},
// Delete
new {
    Id = Guid.Parse("$PERM_DELETE_GUID"),
    Code = "$CONTEXT.$APP.$MODULE.delete",
    Name = "Delete $MODULE_LABEL",
    Description = "Remove $MODULE_LABEL",
    Category = "$CONTEXT.$APP.$MODULE",
    CreatedAt = seedDate
}
```

### RolePermission Seed (Default Roles)

```csharp
// Dans RolePermissionConfiguration.cs - HasData()
// SuperAdmin gets all permissions automatically (wildcard)

// PlatformAdmin - if platform context
new { RoleId = Guid.Parse("...PlatformAdminRoleId..."), PermissionId = $PERM_READ_GUID },
new { RoleId = Guid.Parse("...PlatformAdminRoleId..."), PermissionId = $PERM_CREATE_GUID },
new { RoleId = Guid.Parse("...PlatformAdminRoleId..."), PermissionId = $PERM_UPDATE_GUID },
new { RoleId = Guid.Parse("...PlatformAdminRoleId..."), PermissionId = $PERM_DELETE_GUID },

// StandardUser - read only by default
new { RoleId = Guid.Parse("...StandardUserRoleId..."), PermissionId = $PERM_READ_GUID }
```

---

## TEMPLATE: SECTION PERMISSIONS SEED (Level 4)

> **Usage:** Quand un Module a des sous-pages avec permissions différentes

### Permission Entity - Section Level

```csharp
// Dans PermissionConfiguration.cs - HasData()
// Pattern: {context}.{application}.{module}.{section}.{action}

// Déclarer le SectionId (doit correspondre à NavigationSectionConfiguration.cs)
var {section}SectionId = Guid.Parse("$SECTION_GUID");

// Wildcard Section
new {
    Id = Guid.Parse("$PERM_SECTION_WILDCARD_GUID"),
    Path = "$CONTEXT.$APP.$MODULE.$SECTION.*",
    Level = PermissionLevel.Section,
    IsWildcard = true,
    SectionId = {section}SectionId,
    Description = "Full $SECTION_LABEL access",
    CreatedAt = seedDate
},
// Read
new {
    Id = Guid.Parse("$PERM_SECTION_READ_GUID"),
    Path = "$CONTEXT.$APP.$MODULE.$SECTION.read",
    Level = PermissionLevel.Section,
    Action = PermissionAction.Read,
    IsWildcard = false,
    SectionId = {section}SectionId,
    Description = "View $SECTION_LABEL",
    CreatedAt = seedDate
},
// Create
new {
    Id = Guid.Parse("$PERM_SECTION_CREATE_GUID"),
    Path = "$CONTEXT.$APP.$MODULE.$SECTION.create",
    Level = PermissionLevel.Section,
    Action = PermissionAction.Create,
    IsWildcard = false,
    SectionId = {section}SectionId,
    Description = "Create in $SECTION_LABEL",
    CreatedAt = seedDate
},
// Update
new {
    Id = Guid.Parse("$PERM_SECTION_UPDATE_GUID"),
    Path = "$CONTEXT.$APP.$MODULE.$SECTION.update",
    Level = PermissionLevel.Section,
    Action = PermissionAction.Update,
    IsWildcard = false,
    SectionId = {section}SectionId,
    Description = "Update in $SECTION_LABEL",
    CreatedAt = seedDate
},
// Delete
new {
    Id = Guid.Parse("$PERM_SECTION_DELETE_GUID"),
    Path = "$CONTEXT.$APP.$MODULE.$SECTION.delete",
    Level = PermissionLevel.Section,
    Action = PermissionAction.Delete,
    IsWildcard = false,
    SectionId = {section}SectionId,
    Description = "Delete in $SECTION_LABEL",
    CreatedAt = seedDate
},
// Execute (optionnel)
new {
    Id = Guid.Parse("$PERM_SECTION_EXECUTE_GUID"),
    Path = "$CONTEXT.$APP.$MODULE.$SECTION.execute",
    Level = PermissionLevel.Section,
    Action = PermissionAction.Execute,
    IsWildcard = false,
    SectionId = {section}SectionId,
    Description = "Execute actions in $SECTION_LABEL",
    CreatedAt = seedDate
}
```

---

## TEMPLATE: RESOURCE PERMISSIONS SEED (Level 5)

> **Usage:** Niveau le plus fin - sous-ressources avec permissions distinctes (ex: Prompts → Blocks)

### Permission Entity - Resource Level

```csharp
// Dans PermissionConfiguration.cs - HasData()
// Pattern: {context}.{application}.{module}.{section}.{resource}.{action}

// Déclarer le ResourceId (doit correspondre à NavigationResourceConfiguration.cs)
var {resource}ResourceId = Guid.Parse("$RESOURCE_GUID");

// Wildcard Resource
new {
    Id = Guid.Parse("$PERM_RESOURCE_WILDCARD_GUID"),
    Path = "$CONTEXT.$APP.$MODULE.$SECTION.$RESOURCE.*",
    Level = PermissionLevel.Resource,
    IsWildcard = true,
    ResourceId = {resource}ResourceId,
    Description = "Full $RESOURCE_LABEL access",
    CreatedAt = seedDate
},
// Read
new {
    Id = Guid.Parse("$PERM_RESOURCE_READ_GUID"),
    Path = "$CONTEXT.$APP.$MODULE.$SECTION.$RESOURCE.read",
    Level = PermissionLevel.Resource,
    Action = PermissionAction.Read,
    IsWildcard = false,
    ResourceId = {resource}ResourceId,
    Description = "View $RESOURCE_LABEL",
    CreatedAt = seedDate
},
// Create
new {
    Id = Guid.Parse("$PERM_RESOURCE_CREATE_GUID"),
    Path = "$CONTEXT.$APP.$MODULE.$SECTION.$RESOURCE.create",
    Level = PermissionLevel.Resource,
    Action = PermissionAction.Create,
    IsWildcard = false,
    ResourceId = {resource}ResourceId,
    Description = "Create $RESOURCE_LABEL",
    CreatedAt = seedDate
},
// Update
new {
    Id = Guid.Parse("$PERM_RESOURCE_UPDATE_GUID"),
    Path = "$CONTEXT.$APP.$MODULE.$SECTION.$RESOURCE.update",
    Level = PermissionLevel.Resource,
    Action = PermissionAction.Update,
    IsWildcard = false,
    ResourceId = {resource}ResourceId,
    Description = "Update $RESOURCE_LABEL",
    CreatedAt = seedDate
},
// Delete
new {
    Id = Guid.Parse("$PERM_RESOURCE_DELETE_GUID"),
    Path = "$CONTEXT.$APP.$MODULE.$SECTION.$RESOURCE.delete",
    Level = PermissionLevel.Resource,
    Action = PermissionAction.Delete,
    IsWildcard = false,
    ResourceId = {resource}ResourceId,
    Description = "Delete $RESOURCE_LABEL",
    CreatedAt = seedDate
}
```

---

## TEMPLATE: BULK OPERATIONS PERMISSIONS

> **OBLIGATOIRE:** Toujours prévoir pour les modules CRUD

### Permission Entity - Bulk Operations

```csharp
// Dans PermissionConfiguration.cs - HasData()
// Ajouter après les permissions CRUD standard

// Bulk Create
new {
    Id = Guid.Parse("$PERM_BULK_CREATE_GUID"),
    Path = "$CONTEXT.$APP.$MODULE.bulk-create",
    Level = PermissionLevel.Module,
    Action = PermissionAction.Create,
    IsWildcard = false,
    ModuleId = {module}ModuleId,
    Description = "Bulk create $MODULE_LABEL",
    CreatedAt = seedDate
},
// Bulk Update
new {
    Id = Guid.Parse("$PERM_BULK_UPDATE_GUID"),
    Path = "$CONTEXT.$APP.$MODULE.bulk-update",
    Level = PermissionLevel.Module,
    Action = PermissionAction.Update,
    IsWildcard = false,
    ModuleId = {module}ModuleId,
    Description = "Bulk update $MODULE_LABEL",
    CreatedAt = seedDate
},
// Bulk Delete
new {
    Id = Guid.Parse("$PERM_BULK_DELETE_GUID"),
    Path = "$CONTEXT.$APP.$MODULE.bulk-delete",
    Level = PermissionLevel.Module,
    Action = PermissionAction.Delete,
    IsWildcard = false,
    ModuleId = {module}ModuleId,
    Description = "Bulk delete $MODULE_LABEL",
    CreatedAt = seedDate
},
// Export
new {
    Id = Guid.Parse("$PERM_EXPORT_GUID"),
    Path = "$CONTEXT.$APP.$MODULE.export",
    Level = PermissionLevel.Module,
    Action = PermissionAction.Execute,
    IsWildcard = false,
    ModuleId = {module}ModuleId,
    Description = "Export $MODULE_LABEL data",
    CreatedAt = seedDate
},
// Import
new {
    Id = Guid.Parse("$PERM_IMPORT_GUID"),
    Path = "$CONTEXT.$APP.$MODULE.import",
    Level = PermissionLevel.Module,
    Action = PermissionAction.Create,
    IsWildcard = false,
    ModuleId = {module}ModuleId,
    Description = "Import $MODULE_LABEL data",
    CreatedAt = seedDate
}
```

---

## EXEMPLE COMPLET: Module "Products" dans "ERP > Sales"

### Variables

```
$CONTEXT = erp
$CONTEXT_GUID = e1e1e1e1-1111-1111-1111-111111111111
$APP = sales
$APP_GUID = e2e2e2e2-2222-2222-2222-222222222222
$MODULE = products
$MODULE_GUID = e3e3e3e3-3333-3333-3333-333333333333

$LABEL_FR = Produits
$LABEL_EN = Products
$LABEL_IT = Prodotti
$LABEL_DE = Produkte

$DESC_FR = Gestion des produits
$DESC_EN = Product management
$DESC_IT = Gestione prodotti
$DESC_DE = Produktverwaltung

$ICON = Package
$ORDER = 1
```

### Fichiers à modifier

1. `NavigationContextConfiguration.cs` - Ajouter context ERP (si nouveau)
2. `NavigationApplicationConfiguration.cs` - Ajouter application Sales
3. `NavigationModuleConfiguration.cs` - Ajouter module Products
4. `NavigationTranslationConfiguration.cs` - Ajouter 4 traductions × nombre d'entités
5. `PermissionConfiguration.cs` - Ajouter permissions CRUD
6. `RolePermissionConfiguration.cs` - Assigner aux rôles par défaut

---

## CHECKLIST SEED

| Vérification | Status |
|--------------|--------|
| ☐ GUID déterministe (pas NewGuid) | |
| ☐ 4 langues pour chaque entité | |
| ☐ Index translations continue la séquence existante | |
| ☐ Route alignée avec permission path | |
| ☐ DisplayOrder cohérent | |
| ☐ Permissions CRUD créées (Level 3 - Module) | |
| ☐ Permissions Section si sous-pages (Level 4) | |
| ☐ Permissions Resource si sous-ressources (Level 5) | |
| ☐ Permissions Bulk Operations créées | |
| ☐ RolePermissions assignés | |

---

## HIÉRARCHIE DES PERMISSIONS (5 Niveaux)

```
Level 1: CONTEXT
└─ Path: {context}.*
   └─ Ex: platform.* → Accès complet au context

Level 2: APPLICATION
└─ Path: {context}.{application}.*
   └─ Ex: platform.administration.* → Accès complet à l'app

Level 3: MODULE
└─ Path: {context}.{application}.{module}.{action}
   └─ Ex: platform.administration.users.read

Level 4: SECTION
└─ Path: {context}.{application}.{module}.{section}.{action}
   └─ Ex: platform.administration.ai.settings.update

Level 5: RESOURCE (finest granularity)
└─ Path: {context}.{application}.{module}.{section}.{resource}.{action}
   └─ Ex: platform.administration.ai.prompts.blocks.delete
```
