package main

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"ops-assistant/internal/core/ops"
)

func main() {
	if len(os.Args) < 4 {
		fmt.Println("usage: ops-runner <db_path> <base_dir> <command_text>")
		os.Exit(2)
	}
	dbPath := os.Args[1]
	baseDir := os.Args[2]
	cmd := os.Args[3]

	parts := strings.Fields(cmd)
	if len(parts) < 2 {
		fmt.Println("ERR: invalid command")
		os.Exit(2)
	}

	switch {
	case len(parts) >= 2 && parts[0] == "/cf" && parts[1] == "dnsadd":
		if len(parts) < 4 {
			fmt.Println("ERR: /cf dnsadd <name> <content> [on|off] [type]")
			os.Exit(2)
		}
		inputs := map[string]string{
			"name":    parts[2],
			"content": parts[3],
			"type":    "A",
			"proxied": "false",
		}
		if len(parts) >= 5 {
			switch strings.ToLower(parts[4]) {
			case "on":
				inputs["proxied"] = "true"
				if len(parts) >= 6 {
					inputs["type"] = parts[5]
				}
			case "off":
				inputs["proxied"] = "false"
				if len(parts) >= 6 {
					inputs["type"] = parts[5]
				}
			case "true":
				inputs["proxied"] = "true"
				if len(parts) >= 6 {
					inputs["type"] = parts[5]
				}
			case "false":
				inputs["proxied"] = "false"
				if len(parts) >= 6 {
					inputs["type"] = parts[5]
				}
			default:
				inputs["type"] = parts[4]
			}
		}
		jobID, _, err := ops.RunOnce(dbPath, filepath.Clean(baseDir), cmd, "cf_dns_add", 1, inputs)
		if err != nil {
			fmt.Printf("ERR: %v\n", err)
			os.Exit(1)
		}
		fmt.Printf("OK job=%d\n", jobID)

	case len(parts) >= 2 && parts[0] == "/cf" && parts[1] == "dnsproxy":
		if len(parts) < 4 {
			fmt.Println("ERR: /cf dnsproxy <record_id|name> on|off")
			os.Exit(2)
		}
		mode := strings.ToLower(parts[3])
		if mode != "on" && mode != "off" {
			fmt.Println("ERR: /cf dnsproxy <record_id|name> on|off")
			os.Exit(2)
		}
		proxied := "false"
		if mode == "on" {
			proxied = "true"
		}
		target := parts[2]
		inputs := map[string]string{
			"proxied":   proxied,
			"record_id": "__empty__",
			"name":      "__empty__",
		}
		if strings.Contains(target, ".") {
			inputs["name"] = target
		} else {
			inputs["record_id"] = target
		}
		jobID, _, err := ops.RunOnce(dbPath, filepath.Clean(baseDir), cmd, "cf_dns_proxy", 1, inputs)
		if err != nil {
			fmt.Printf("ERR: %v\n", err)
			os.Exit(1)
		}
		fmt.Printf("OK job=%d\n", jobID)

	case len(parts) >= 2 && parts[0] == "/cpa" && parts[1] == "status":
		jobID, _, err := ops.RunOnce(dbPath, filepath.Clean(baseDir), cmd, "cpa_status", 1, map[string]string{})
		if err != nil {
			fmt.Printf("ERR: %v\n", err)
			os.Exit(1)
		}
		fmt.Printf("OK job=%d\n", jobID)
	case len(parts) >= 3 && parts[0] == "/cpa" && parts[1] == "usage" && parts[2] == "backup":
		jobID, _, err := ops.RunOnce(dbPath, filepath.Clean(baseDir), cmd, "cpa_usage_backup", 1, map[string]string{})
		if err != nil {
			fmt.Printf("ERR: %v\n", err)
			os.Exit(1)
		}
		fmt.Printf("OK job=%d\n", jobID)
	case len(parts) >= 4 && parts[0] == "/cpa" && parts[1] == "usage" && parts[2] == "restore":
		inputs := map[string]string{
			"backup_id": parts[3],
		}
		jobID, _, err := ops.RunOnce(dbPath, filepath.Clean(baseDir), cmd, "cpa_usage_restore", 1, inputs)
		if err != nil {
			fmt.Printf("ERR: %v\n", err)
			os.Exit(1)
		}
		fmt.Printf("OK job=%d\n", jobID)
	default:
		fmt.Println("ERR: unsupported command")
		os.Exit(2)
	}
}
