# TypeDialog Integration

TypeDialog enables interactive form-based configuration from Nickel schemas.

## Status

- **TypeDialog Binary**: Not yet installed (planned: `typedialog` command)
- **TypeDialog Forms**: Created and ready (setup wizard, auth login, MFA enrollment)
- **Bash Wrappers**: Implemented to handle TTY input properly
- **ForminQuire**: DEPRECATED - Archived to `.coder/archive/forminquire/`

## Directory Structure

```toml
.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/
    └── constraints/                 # Validation constraints
        └── constraints.toml         # Shared validation rules
```

## How TypeDialog Would Work

### 1. Form Generation from Schemas

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

### 2. Interactive Configuration

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

### 3. Validation

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

## Current Status: TypeDialog Forms Ready

TypeDialog forms have been created and are ready to use:

**Form Locations**:
- Setup wizard: `provisioning/.typedialog/core/forms/setup-wizard.toml`
- Auth login: `provisioning/.typedialog/core/forms/auth-login.toml`
- MFA enrollment: `provisioning/.typedialog/core/forms/mfa-enroll.toml`

**Bash Wrappers** (TTY-safe, handle input properly):
- `provisioning/core/shlib/setup-wizard-tty.sh`
- `provisioning/core/shlib/auth-login-tty.sh`
- `provisioning/core/shlib/mfa-enroll-tty.sh`

**Usage Pattern**:
1. Bash wrapper calls TypeDialog (handles TTY input)
2. TypeDialog generates Nickel config file
3. Nushell scripts read the generated config (no input issues)

**Example**:

```toml
# Run TypeDialog setup wizard
./provisioning/core/shlib/setup-wizard-tty.sh

# Nushell reads the generated config
let config = (open provisioning/.typedialog/core/generated/setup-wizard-result.json | from json)
```

**Note**: ForminQuire (Jinja2-based forms) has been archived to `provisioning/.coder/archive/forminquire/` and is no longer in use.

## Integration Plan (When TypeDialog Available)

### Step 1: Install TypeDialog

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

### Step 2: Generate Forms from Schemas

```toml
# Batch generate all forms
for schema in provisioning/schemas/platform/*.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

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

### Step 4: Update Platform Setup Script

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

if command -v typedialog &> /dev/null; then
  # TypeDialog is installed - use bash wrapper for proper TTY handling
  ./provisioning/core/shlib/setup-wizard-tty.sh

  # Read generated JSON config
  # Nushell scripts can now read the config without input issues
else
  # Fallback to basic prompts
  echo "TypeDialog not available. Using basic interactive prompts..."
  # Nushell wizard with basic input prompts
  nu -c "use provisioning/core/nulib/lib_provisioning/setup/wizard.nu *; run-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

```toml
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
- ✅ **TTY handling** - Bash wrappers solve Nushell input stack issues
- ✅ **Graceful fallback** - Falls back to basic prompts if TypeDialog unavailable

## Testing TypeDialog Forms

```toml
# 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: Legacy (DEPRECATED)

```toml
FormInquire (Jinja2) → Nushell processing → TOML config
Status: ARCHIVED to .coder/archive/forminquire/
```

### Phase B: Current Implementation

```toml
Bash wrapper → TypeDialog (TTY input) → Nickel config → JSON export → Nushell reads JSON
Status: IMPLEMENTED with forms ready
```

### Phase C: TypeDialog Binary Available (Future)

```toml
TypeDialog binary installed → Full nickel-roundtrip workflow → Auto-sync with schemas
Status: PLANNED - awaiting TypeDialog binary release
```

### Phase D: Unified (Future)

```toml
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** (TypeDialog ready)
- Script: `provisioning/platform/scripts/setup-with-forms.sh`
- Bash wrappers: `provisioning/core/shlib/*-tty.sh` (handle TTY input)
- Forms ready: setup-wizard, auth-login, mfa-enroll
- Fallback: Basic Nushell prompts if TypeDialog unavailable

### 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 | TypeDialog forms + bash wrappers ready |
| **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

```toml
# 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 binary becomes available**:
   - Install TypeDialog binary
   - Forms already created and ready to use
   - Bash wrappers handle TTY input (no Nushell stack issues)
   - Full nickel-roundtrip workflow will be enabled

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

---

**Version**: 1.2.0 (TypeDialog Forms + Bash Wrappers)
**Status**: TypeDialog forms ready with bash wrappers; Awaiting TypeDialog Binary
**Last Updated**: 2025-01-09
**ForminQuire Status**: DEPRECATED - Archived to .coder/archive/forminquire/
**Fallback**: Basic Nushell prompts if TypeDialog unavailable
**Tested**: Infrastructure examples (solo + enterprise) validated
