# Micro-SaaS Example

Exemplo completo de um Micro-SaaS com Blazor Server + Asaas para billing.

## Stack

| Camada | Tecnologia |
|--------|------------|
| **Frontend** | Blazor Server |
| **Backend** | .NET 8 Minimal APIs |
| **Database** | SQL Server / PostgreSQL |
| **Auth** | Clerk ou Azure Identity |
| **Billing** | Asaas (PIX, Boleto, Cartão) |
| **Infra** | Azure Container Apps |

## Features

- Multi-tenancy com isolamento por tenant
- Subscription management com Asaas
- Dashboard administrativo
- User management por tenant
- Webhooks para eventos de pagamento

## Estrutura

```
src/
├── MicroSaaS.Domain/
│   ├── Entities/
│   │   ├── Tenant.cs
│   │   ├── TenantUser.cs
│   │   ├── Subscription.cs
│   │   └── Plan.cs
│   └── Interfaces/
├── MicroSaaS.Infrastructure/
│   ├── Data/
│   ├── Repositories/
│   └── Services/
│       └── AsaasService.cs
├── MicroSaaS.Application/
│   ├── Commands/
│   ├── Queries/
│   └── Services/
└── MicroSaaS.Web/
    ├── Components/
    │   ├── Layout/
    │   ├── Pages/
    │   └── Shared/
    └── Program.cs
```

## Quick Start

```bash
# 1. Clone e configure
dotnet new blazor -n MicroSaaS.Web -o src/MicroSaaS.Web
cd src/MicroSaaS.Web

# 2. Adicione packages
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer

# 3. Configure appsettings.json
# Veja exemplo em appsettings.example.json

# 4. Run migrations
dotnet ef migrations add Initial
dotnet ef database update

# 5. Start
dotnet run
```

## Fluxo de Onboarding

```mermaid
sequenceDiagram
    participant User
    participant App
    participant Clerk
    participant Asaas

    User->>App: Acessa /signup
    App->>Clerk: Cria usuário
    Clerk-->>App: User ID
    App->>App: Cria Tenant
    App->>Asaas: Cria Customer
    Asaas-->>App: Customer ID
    App->>App: Associa User ao Tenant
    App-->>User: Redirect /dashboard
```

## Fluxo de Subscription

```mermaid
sequenceDiagram
    participant User
    participant App
    participant Asaas

    User->>App: Seleciona plano
    App->>Asaas: Cria Subscription
    Asaas-->>App: Subscription ID
    Asaas->>App: Webhook PAYMENT_CREATED
    App->>App: Registra pagamento pendente
    User->>Asaas: Realiza pagamento
    Asaas->>App: Webhook PAYMENT_RECEIVED
    App->>App: Ativa subscription
    App-->>User: Acesso liberado
```

## Custos Estimados

| Recurso | Tier | Custo/mês |
|---------|------|-----------|
| Container Apps | Consumption | ~$0 (scale-to-zero) |
| SQL Database | Free 32GB | $0 |
| Storage | LRS | ~$0.50 |
| Asaas | Por transação | 2.99% + R$0.49 |

**Total estimado**: < $5/mês para baixo volume

---

*MORPH-SPEC by Polymorphism Tech*
