#!/bin/bash
# Generate complete syntaxis-api configuration structure
# Creates main config + 7 feature configs in multi-tier structure
# Uses .config/syntaxis directory

set -e

CONFIG_DIR="${1:-$HOME/.config/syntaxis}"
DATA_DIR="${2:-$HOME/.local/share/syntaxis}"
FEATURES_DIR="$CONFIG_DIR/configs/features"

echo "🚀 Generating Lifecycle Server Configuration..."
echo ""

# Create directories
mkdir -p "$CONFIG_DIR"
mkdir -p "$FEATURES_DIR"
mkdir -p "$DATA_DIR"
echo "✓ Created directories:"
echo "  - $CONFIG_DIR"
echo "  - $FEATURES_DIR"
echo "  - $DATA_DIR"
echo ""

# Create MAIN config (Tier 1)
cat > "$CONFIG_DIR/syntaxis-api.toml" << 'EOF'
# Lifecycle Server Configuration (MAIN - Tier 1)
# Features are defined here and loaded from configs/features/

[server]
host = "127.0.0.1"
port = 3000
database_path = "__DATA_DIR__/syntaxis.db"
cors_enabled = true
log_level = "info"
public_files_path = "./public"

[server.tls]
enabled = false
# cert_path = "/path/to/cert.pem"
# key_path = "/path/to/key.pem"

# Feature Flags - Control which Tier 2 configs are loaded
[server.features.database]
enabled = true

[server.features.health]
enabled = true

[server.features.metrics]
enabled = false

[server.features.rate_limit]
enabled = false

[server.features.auth]
enabled = false

[server.features.cache]
enabled = false

[server.features.multi_tenant]
enabled = false
EOF

sed -i '' "s|__DATA_DIR__|$DATA_DIR|g" "$CONFIG_DIR/syntaxis-api.toml"
echo "✓ Created main config: $CONFIG_DIR/syntaxis-api.toml"
echo ""

# Create FEATURE configs (Tier 2)
echo "✓ Creating feature configs in: $FEATURES_DIR"

# Database feature
cat > "$FEATURES_DIR/database.toml" << 'EOF'
[database]
migrations_path = "./migrations"
auto_migrate = true
connection_pool_size = 10
log_queries = false
EOF
echo "  ✓ database.toml"

# Health feature
cat > "$FEATURES_DIR/health.toml" << 'EOF'
[health]
database_timeout_ms = 5000
include_version = true
include_uptime = true
include_database_status = true
EOF
echo "  ✓ health.toml"

# Metrics feature
cat > "$FEATURES_DIR/metrics.toml" << 'EOF'
[metrics]
metrics_path = "/metrics"
request_duration_buckets = [1, 5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000]
trace_requests = false
collect_performance_metrics = true
track_memory = true
EOF
echo "  ✓ metrics.toml"

# Rate Limit feature
cat > "$FEATURES_DIR/rate_limit.toml" << 'EOF'
[rate_limit]
requests_per_second = 10
burst_size = 20
rate_limit_health_metrics = false
rate_limit_message = "Too many requests"
EOF
echo "  ✓ rate_limit.toml"

# Auth feature
cat > "$FEATURES_DIR/auth.toml" << 'EOF'
[auth]
api_keys = []
auth_header = "Authorization"
auth_prefix = "Bearer"
strict_validation = true
log_failures = true
EOF
echo "  ✓ auth.toml"

# Cache feature
cat > "$FEATURES_DIR/cache.toml" << 'EOF'
[cache]
backend = "memory"
max_items = 10000
ttl_seconds = 3600

[cache.memory]
eviction_enabled = true
eviction_interval_seconds = 300

[cache.redis]
connection_url = "redis://localhost:6379/0"
connection_timeout_ms = 5000
key_prefix = "syntaxis:"
EOF
echo "  ✓ cache.toml"

# Multi-tenant feature
cat > "$FEATURES_DIR/multi_tenant.toml" << 'EOF'
[multi_tenant]
tenant_id_source = "header"
tenant_header_name = "X-Tenant-ID"
require_tenant_id = false
isolation_level = "row"
allow_schema_customization = false
max_tenants = 1000
include_tenant_in_logs = true
EOF
echo "  ✓ multi_tenant.toml"

echo ""
echo "✅ Configuration structure created!"
echo ""
echo "📁 Structure:"
echo "~/.config/syntaxis/"
echo "├── syntaxis-api.toml          (MAIN CONFIG - Tier 1)"
echo "└── configs/features/"
echo "    ├── database.toml              (Tier 2 - enabled)"
echo "    ├── health.toml                (Tier 2 - enabled)"
echo "    ├── metrics.toml               (Tier 2 - disabled)"
echo "    ├── rate_limit.toml            (Tier 2 - disabled)"
echo "    ├── auth.toml                  (Tier 2 - disabled)"
echo "    ├── cache.toml                 (Tier 2 - disabled)"
echo "    └── multi_tenant.toml          (Tier 2 - disabled)"
echo ""
echo "📍 Data directory: $DATA_DIR"
echo ""
echo "🔧 To customize:"
echo "  1. Edit main config:"
echo "     nano $CONFIG_DIR/syntaxis-api.toml"
echo ""
echo "  2. Edit feature configs:"
echo "     nano $FEATURES_DIR/database.toml"
echo "     nano $FEATURES_DIR/health.toml"
echo "     nano $FEATURES_DIR/metrics.toml   # (if enabling metrics)"
echo ""
echo "🚀 To run server:"
echo "  syntaxis-api --config $CONFIG_DIR/syntaxis-api.toml"
