# Pre-Flight Check for Azure Deployment

Validates project readiness before deploying to Azure.

## Uso

```
/morph-preflight azure
```

## Objetivo

Detectar problemas **antes** do deploy que causariam falhas em produção. Economiza tempo e evita debugging em ambiente Azure.

---

## Validações Executadas

### 1. Package Version Conflicts

**O que verifica:**
- `Azure.Identity` especificado explicitamente
- Sem conflitos de versão (NU1605, NU1608)
- Packages críticos com versões compatíveis

**Como verificar:**
```bash
dotnet restore --verbosity normal 2>&1 | grep -E "(NU1605|NU1608|warning)"
```

**Fix comum:**
```xml
<PackageReference Include="Azure.Identity" Version="1.14.2" />
```

---

### 2. EF Core Migrations

**O que verifica:**
- Sem pending model changes
- Migrations aplicáveis existem
- Migration scripts são válidos

**Como verificar:**
```bash
dotnet ef migrations has-pending-model-changes \
  --project src/Infrastructure \
  --startup-project src/Web
```

**Fix comum:**
```bash
dotnet ef migrations add <MigrationName> \
  --project src/Infrastructure \
  --startup-project src/Web
```

---

### 3. Dockerfile Validation (se Container Apps)

**O que verifica:**
- Dockerfile existe
- Base image é válida
- Multi-stage build configurado
- EXPOSE ports corretos
- ENTRYPOINT definido

**Como verificar:**
```bash
docker build --check .
# Ou validação manual do Dockerfile
```

---

### 4. Blazor .NET 10 Assets

**O que verifica:**
- `RequiresAspNetWebAssets` está `true` (se Blazor + .NET 10)
- Static web assets configurados corretamente

**Como verificar:**
```bash
grep -r "RequiresAspNetWebAssets" *.csproj
```

**Fix:**
```xml
<PropertyGroup>
  <RequiresAspNetWebAssets>true</RequiresAspNetWebAssets>
</PropertyGroup>
```

---

### 5. Bicep Syntax Validation

**O que verifica:**
- Arquivos `.bicep` sem erros de sintaxe
- Parâmetros obrigatórios definidos
- Resources com nomes válidos

**Como verificar:**
```bash
az bicep build --file infra/main.bicep --stdout > /dev/null
```

---

### 6. Key Vault Configuration

**O que verifica:**
- Key Vault URI configurado em `appsettings.json` ou environment
- Não condiciona Key Vault ao ambiente (bug comum)
- Managed Identity habilitada

**Como verificar:**
```bash
grep -r "KeyVaultUri\|AzureKeyVault" appsettings*.json
grep -r "IsDevelopment()" Program.cs | grep -i keyvault
```

**Anti-pattern a detectar:**
```csharp
// ❌ ERRADO - Não carrega Key Vault em dev!
if (!builder.Environment.IsDevelopment())
{
    builder.Configuration.AddAzureKeyVault(...);
}
```

---

### 7. Connection Strings Security

**O que verifica:**
- Connection strings não hardcoded em código
- Secrets em Key Vault ou User Secrets
- Sem credentials em `appsettings.json` (exceto Development)

**Como verificar:**
```bash
# Detectar passwords em appsettings
grep -rE "(Password=|Pwd=|Secret=)" appsettings*.json | grep -v Development
```

---

## Output Exemplo

```
🔍 MORPH Pre-Flight Check: Azure Deployment
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✅ Packages: No version conflicts detected
✅ Migrations: No pending model changes
✅ Dockerfile: Valid multi-stage build
✅ Blazor Assets: RequiresAspNetWebAssets=true
✅ Bicep: Syntax valid
❌ Key Vault: URI not configured in appsettings.Production.json
⚠️ Connection Strings: Found password in appsettings.Staging.json

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

📋 Summary: 5 passed, 1 failed, 1 warning

❌ BLOCKED: Fix Key Vault configuration before deploy!

Recommended fixes:
1. Add KeyVaultUri to appsettings.Production.json
2. Move password from appsettings.Staging.json to Key Vault
```

---

## Workflow de Uso

### Antes de criar PR para produção

```bash
/morph-preflight azure
```

### No pipeline CI/CD

```yaml
- script: |
    # Package check
    dotnet restore 2>&1 | grep -E "NU1605|NU1608" && exit 1

    # Migration check
    dotnet ef migrations has-pending-model-changes \
      --project src/Infrastructure \
      --startup-project src/Web
    if [ $? -ne 0 ]; then exit 1; fi

    # Bicep check
    az bicep build --file infra/main.bicep --stdout > /dev/null
    if [ $? -ne 0 ]; then exit 1; fi

    echo "Pre-flight checks passed!"
  displayName: 'Pre-flight validation'
```

---

## Validações Futuras (Roadmap)

- [ ] Cost estimation validation (dentro do budget)
- [ ] RBAC permissions check
- [ ] Network security rules validation
- [ ] Health probe endpoint exists
- [ ] Application Insights configured

---

## Referências

- [azure.md](../../standards/azure.md) - Padrões Azure
- [dotnet10-migration.md](../../standards/dotnet10-migration.md) - Breaking changes .NET 10
- [blazor-efcore.md](../../../../framework/standards/blazor-efcore.md) - EF Core patterns

---

*MORPH-SPEC by Polymorphism Tech*
