# TypeDialog Integration

TypeDialog enables interactive form-based configuration from Nickel schemas.

## Status

- **TypeDialog Binary**: Not yet installed (planned: `typedialog` command)
- **Alternative**: FormInquire (Jinja2 templates + interactive forms) - **ACTIVE**
- **Plan**: Full TypeDialog migration when available

## Directory Structure

```
.typedialog/
└── provisioning/platform/
    ├── README.md                    # This file
    ├── forms/                       # Form definitions (to be generated)
    │   ├── orchestrator.form.toml
    │   ├── control-center.form.toml
    │   └── ...
    ├── templates/                   # Jinja2 templates for schema rendering
    │   └── service-form.template.j2
    ├── schemas/                     # Symlink to Nickel schemas
    │   └── platform/schemas/ → ../../../schemas/platform/schemas/
    └── constraints/                 # Validation constraints
        └── constraints.toml         # Shared validation rules
```

## How TypeDialog Would Work

### 1. Form Generation from Schemas

```bash
# Auto-generate form from Nickel schema
typedialog generate-form --schema orchestrator.ncl \
  --output forms/orchestrator.form.toml
```

### 2. Interactive Configuration

```bash
# Run interactive form
typedialog run-form --form forms/orchestrator.form.toml \
  --output orchestrator-configured.ncl
```

### 3. Validation

```bash
# Validate user input against schema
typedialog validate --form forms/orchestrator.form.toml \
  --data user-config.ncl
```

## Current Alternative: FormInquire

While TypeDialog is not yet available, FormInquire provides form-based configuration:

**Location**: `provisioning/core/forminquire/`

**How it works**:
1. Define form in Jinja2 template (`.form.j2`)
2. Use `nu_plugin_tera` to render templates
3. Collect user input via FormInquire CLI
4. Process results with Nushell scripts

**Example**:
```nushell
# Load Jinja2 template and show form
let form_data = forminquire load provisioning/core/forminquire/templates/orchestrator.form.j2

# Process user input
let config = process_form_input $form_data
```

## Integration Plan (When TypeDialog Available)

### Step 1: Install TypeDialog

```bash
cargo install --path /Users/Akasha/Development/typedialog
typedialog --version
```

### Step 2: Generate Forms from Schemas

```bash
# Batch generate all forms
for schema in provisioning/schemas/platform/schemas/*.ncl; do
  service=$(basename $schema .ncl)
  typedialog generate-form \
    --schema $schema \
    --output provisioning/platform/.typedialog/forms/${service}.form.toml
done
```

### Step 3: Create Setup Wizard

```bash
# Unified setup workflow
provisioning setup-platform \
  --mode solo|multiuser|enterprise \
  --provider docker|kubernetes \
  --interactive  # Uses TypeDialog forms
```

### Step 4: Update Platform Setup Script

```bash
# provisioning/platform/scripts/setup-platform-config.sh

if command -v typedialog &> /dev/null; then
  # TypeDialog is installed
  typedialog run-form \
    --form .typedialog/forms/orchestrator.form.toml \
    --output config/runtime/orchestrator.ncl

  # Export to TOML
  nickel export --format toml config/runtime/orchestrator.ncl \
    > config/runtime/generated/orchestrator.solo.toml
else
  # Fallback to FormInquire
  forminquire setup-wizard
fi
```

## Form Definition Example

```toml
# provisioning/platform/.typedialog/forms/orchestrator.form.toml
[metadata]
name = "Orchestrator Configuration"
description = "Configure the Orchestrator service"
version = "1.0.0"
schema = "orchestrator.ncl"

[fields.mode]
type = "enum"
label = "Deployment Mode"
description = "Select deployment mode: solo, multiuser, or enterprise"
options = ["solo", "multiuser", "enterprise"]
default = "solo"
required = true

[fields.server.port]
type = "number"
label = "Server Port"
description = "HTTP server port (1-65535)"
min = 1
max = 65535
default = 8080
required = true

[fields.database.host]
type = "string"
label = "Database Host"
description = "PostgreSQL host"
default = "localhost"
required = true

[fields.logging.level]
type = "enum"
label = "Logging Level"
options = ["debug", "info", "warning", "error"]
default = "info"
required = false
```

## Validation Constraints

```toml
# provisioning/platform/.typedialog/constraints/constraints.toml

[orchestrator]
mode = ["solo", "multiuser", "enterprise"]
port = "range(1, 65535)"
database_pool_size = "range(1, 100)"
memory = "pattern(^\\d+[MG]B$)"

[control-center]
port = "range(1, 65535)"
replicas = "range(1, 10)"

[nginx]
worker_processes = "range(1, 32)"
worker_connections = "range(1, 65536)"
```

## Workflow: Setup to Deployment

```
1. User runs setup command
   ↓
2. TypeDialog displays form
   ↓
3. User fills form with validation
   ↓
4. Form data → Nickel config
   ↓
5. Nickel config → TOML (via ConfigLoader)
   ↓
6. Service reads TOML config
   ↓
7. Service starts with configured values
```

## Benefits of TypeDialog Integration

- ✅ **Type-safe forms** - Generated from Nickel schemas
- ✅ **Real-time validation** - Enforce constraints as user types
- ✅ **Progressive disclosure** - Show advanced options only when needed
- ✅ **Consistent UX** - Same forms across platforms (CLI, Web, TUI)
- ✅ **Auto-generated** - Forms stay in sync with schemas automatically
- ✅ **Fallback support** - FormInquire as alternative if TypeDialog unavailable

## Testing TypeDialog Forms

```bash
# Validate form structure
typedialog check-form provisioning/platform/.typedialog/forms/orchestrator.form.toml

# Run form with test data
typedialog run-form \
  --form provisioning/platform/.typedialog/forms/orchestrator.form.toml \
  --test-mode  # Automated validation

# Generate sample output
typedialog generate-sample \
  --form provisioning/platform/.typedialog/forms/orchestrator.form.toml \
  --output /tmp/orchestrator-sample.ncl
```

## Migration Path

### Phase A: Current (FormInquire)

```
FormInquire (Jinja2) → Nushell processing → TOML config
```

### Phase B: TypeDialog Available

```
TypeDialog (Schema-driven) → Nickel config → TOML export
```

### Phase C: Unified (Future)

```
ConfigLoader discovers config → Service reads → TypeDialog updates UI
```

## Integration with Infrastructure Schemas

TypeDialog forms work seamlessly with infrastructure schemas:

### Infrastructure Configuration Workflow

**1. Define Infrastructure Schemas** (completed)
- Location: `provisioning/schemas/infrastructure/`
- 6 schemas: docker-compose, kubernetes, nginx, prometheus, systemd, oci-registry
- All validated with `nickel typecheck`

**2. Generate Infrastructure Configs** (completed)
- Script: `provisioning/platform/scripts/generate-infrastructure-configs.nu`
- Supports: solo, multiuser, enterprise, cicd modes
- Formats: YAML, JSON, conf, service

**3. Validate Generated Configs** (completed)
- Script: `provisioning/platform/scripts/validate-infrastructure.nu`
- Tools: docker-compose config, kubectl apply --dry-run, nginx -t, promtool check
- Examples: `examples-solo-deployment.ncl`, `examples-enterprise-deployment.ncl`

**4. Interactive Setup with Forms** (ready for TypeDialog)
- Script: `provisioning/platform/scripts/setup-with-forms.sh`
- Auto-detects TypeDialog, falls back to FormInquire
- Supports batch or single-service configuration
- Auto-generates forms from schemas (when TypeDialog available)

### Current Status: Full Infrastructure Support

| Component | Status | Details |
|-----------|--------|---------|
| **Schemas** | ✅ Complete | 6 infrastructure schemas (1,577 lines) |
| **Examples** | ✅ Complete | 2 deployment examples (solo, enterprise) |
| **Generation Script** | ✅ Complete | Auto-generates configs for all modes |
| **Validation Script** | ✅ Complete | Validates Docker, K8s, Nginx, Prometheus |
| **Setup Wizard** | ✅ Complete | Interactive config + FormInquire active |
| **TypeDialog Integration** | ⏳ Pending | Structure ready, awaiting binary |

### Validated Examples

**Solo Deployment** (`examples-solo-deployment.ncl`):
- ✅ Type-checks without errors
- ✅ Exports to 198 lines of JSON
- ✅ 5 Docker Compose services
- ✅ Resource limits: 1.0-4.0 CPU, 256M-1024M RAM
- ✅ Prometheus: 4 scrape jobs
- ✅ Registry backend: Zot (filesystem)

**Enterprise Deployment** (`examples-enterprise-deployment.ncl`):
- ✅ Type-checks without errors
- ✅ Exports to 313 lines of JSON
- ✅ 6 Docker Compose services with HA
- ✅ Resource limits: 2.0-4.0 CPU, 512M-4096M RAM
- ✅ Prometheus: 7 scrape jobs with remote storage
- ✅ Registry backend: Harbor (S3 distributed)

### Test Infrastructure Generation

```bash
# Export solo infrastructure
nickel export --format json provisioning/schemas/infrastructure/examples-solo-deployment.ncl > /tmp/solo.json

# Validate JSON
jq . /tmp/solo.json

# Check Docker Compose services
jq '.docker_compose_services | keys' /tmp/solo.json

# Compare resource allocation (solo vs enterprise)
jq '.docker_compose_services.orchestrator.deploy.resources.limits' /tmp/solo.json
jq '.docker_compose_services.orchestrator.deploy.resources.limits' /tmp/enterprise.json
```

## Next Steps

1. **Infrastructure Setup** (available now):
   - Generate infrastructure configs with automation scripts
   - Validate with format-specific tools
   - Use interactive setup wizard for configuration

2. **When TypeDialog becomes available**:
   - Install TypeDialog binary
   - Run form generation script from infrastructure schemas
   - Update setup script to use TypeDialog exclusively
   - Deprecate FormInquire (keep as fallback)

3. **Production Deployment**:
   - Use validated infrastructure configs
   - Deploy with ConfigLoader + infrastructure schemas
   - Monitor via Prometheus (auto-generated from schemas)

---

**Version**: 1.1.0 (Infrastructure Integration Added)
**Status**: Ready for Infrastructure Generation; Awaiting TypeDialog Binary
**Last Updated**: 2025-01-06
**Current Alternatives**: FormInquire (active), automation scripts (complete)
**Tested**: Infrastructure examples (solo + enterprise) validated
