#!/usr/bin/env nu
# Install syntaxis-api server with configuration
#
# Usage:
#   ./scripts/install-syntaxis-api.nu [--config-dir /path/to/config] [--binary /path/to/binary]
#
# This script:
# 1. Copies the template configuration file to a user-specified location
# 2. Creates a run.sh wrapper script to start the server with the config
# 3. Shows instructions for starting the server

use std log

def main [
    --config-dir: string = ""              # Where to place config file (default: ~/.config/syntaxis-api)
    --binary: string = ""                  # Path to syntaxis-api binary
] {
    log info "Installing syntaxis-api server..."

    # Determine config directory
    let config_dir = if ($config_dir == "") {
        $"($env.HOME)/.config/syntaxis-api"
    } else {
        $config_dir
    }

    # Determine binary path
    let binary_path = if ($binary == "") {
        let project_root = (pwd)
        $"($project_root)/target/release/syntaxis-api"
    } else {
        $binary
    }

    # Check if binary exists
    if not ($binary_path | path exists) {
        log error $"Binary not found at: ($binary_path)"
        log error "Please run: cargo build -p syntaxis-api --release"
        return 1
    }

    log info $"Using binary: ($binary_path)"

    # Create config directory
    mkdir ($config_dir) | ignore
    log info $"Created config directory: ($config_dir)"

    # Get config file path
    let config_file = $"($config_dir)/syntaxis-api-config.toml"

    # Check if config already exists
    if ($config_file | path exists) {
        log warn $"Config file already exists at: ($config_file)"
        let response = (input "Overwrite? (y/N): ")
        if ($response != "y") {
            log info "Installation cancelled"
            return 0
        }
    }

    # Copy template to config location
    let template = "syntaxis-api-config.template.toml"
    if not ($template | path exists) {
        log error $"Template not found: ($template)"
        return 1
    }

    cp $template $config_file
    log info $"Created config file: ($config_file)"

    # Create run-syntaxis-api.sh script
    let run_script = $"($config_dir)/run-syntaxis-api.sh"

    let run_script_content = $"#!/bin/bash
# Lifecycle API Server Runner
# Generated by install-syntaxis-api.nu
#
# This script starts the syntaxis-api with the configuration file.
# You can override settings with environment variables:
#   SYNTAXIS_API_HOST=0.0.0.0 ./run-syntaxis-api.sh
#   SYNTAXIS_API_PORT=8080 ./run-syntaxis-api.sh
#   SYNTAXIS_API_LOG_LEVEL=debug ./run-syntaxis-api.sh

exec \"($binary_path)\" --config \"($config_file)\" \"\$@\"
"

    $run_script_content | save -f $run_script
    chmod +x $run_script
    log info $"Created run script: ($run_script)"

    # Display instructions
    print ""
    print (ansi green "✓ Installation complete!")
    print ""
    print "Configuration:"
    print $"  Config file: ($config_file)"
    print $"  Binary: ($binary_path)"
    print $"  Run script: ($run_script)"
    print ""
    print "Quick Start:"
    print $"  1. Edit configuration: nano ($config_file)"
    print $"  2. Start server: ($run_script)"
    print $"  3. Check health: curl http://localhost:3000/api/health"
    print ""
    print "Environment Overrides:"
    print $"  SYNTAXIS_API_HOST=0.0.0.0 ($run_script)"
    print $"  SYNTAXIS_API_PORT=8080 ($run_script)"
    print $"  SYNTAXIS_API_LOG_LEVEL=debug ($run_script)"
    print ""
}
