package main

import (
	"encoding/json"
	"fmt"
	"os"
)

type Config struct {
	JupiterRateLimitMs int      `json:"jupiterRateLimitMs"`
	JupiterApiKeys     []string `json:"jupiterApiKeys"`
	Threads            int      `json:"threads"`
	EnableSolanaCheck  bool     `json:"enableSolanaCheck"`
	EnableAxiomModule  bool     `json:"enableAxiomModule"`
	EnableEVMModule    bool     `json:"enableEVMModule"`
}

func loadConfig() (*Config, error) {
	data, err := os.ReadFile("config.json")
	if err != nil {
		return &Config{
			JupiterRateLimitMs: 1100,
			Threads:            20,
			EnableSolanaCheck:  true,
			EnableAxiomModule:  false,
			EnableEVMModule:    false,
		}, nil
	}

	var cfg Config
	if err := json.Unmarshal(data, &cfg); err != nil {
		return nil, fmt.Errorf("failed to parse config: %w", err)
	}

	if cfg.JupiterRateLimitMs <= 0 {
		cfg.JupiterRateLimitMs = 1100
	}
	if cfg.Threads <= 0 {
		cfg.Threads = 20
	}

	return &cfg, nil
}

func saveConfig(cfg *Config) error {
	data, err := json.MarshalIndent(cfg, "", "  ")
	if err != nil {
		return fmt.Errorf("failed to marshal config: %w", err)
	}

	if err := os.WriteFile("config.json", data, 0644); err != nil {
		return fmt.Errorf("failed to write config: %w", err)
	}

	return nil
}
