import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { orchestratorConfig } from "./orchestrator.config";

describe("orchestratorConfig", () => {
  const originalEnv = process.env;

  beforeEach(() => {
    process.env = { ...originalEnv };
  });

  afterEach(() => {
    process.env = originalEnv;
  });

  describe("sandbox.enabled", () => {
    it("should be enabled by default when SANDBOX_ENABLED is not set", () => {
      delete process.env.SANDBOX_ENABLED;

      const config = orchestratorConfig();

      expect(config.sandbox.enabled).toBe(true);
    });

    it("should be enabled when SANDBOX_ENABLED is set to 'true'", () => {
      process.env.SANDBOX_ENABLED = "true";

      const config = orchestratorConfig();

      expect(config.sandbox.enabled).toBe(true);
    });

    it("should be disabled only when SANDBOX_ENABLED is explicitly set to 'false'", () => {
      process.env.SANDBOX_ENABLED = "false";

      const config = orchestratorConfig();

      expect(config.sandbox.enabled).toBe(false);
    });

    it("should be enabled for any other value of SANDBOX_ENABLED", () => {
      process.env.SANDBOX_ENABLED = "yes";

      const config = orchestratorConfig();

      expect(config.sandbox.enabled).toBe(true);
    });

    it("should be enabled when SANDBOX_ENABLED is empty string", () => {
      process.env.SANDBOX_ENABLED = "";

      const config = orchestratorConfig();

      expect(config.sandbox.enabled).toBe(true);
    });
  });

  describe("host binding", () => {
    it("should default to 127.0.0.1 when no env vars are set", () => {
      delete process.env.HOST;
      delete process.env.BIND_ADDRESS;

      const config = orchestratorConfig();

      expect(config.host).toBe("127.0.0.1");
    });

    it("should use HOST env var when set", () => {
      process.env.HOST = "0.0.0.0";
      delete process.env.BIND_ADDRESS;

      const config = orchestratorConfig();

      expect(config.host).toBe("0.0.0.0");
    });

    it("should use BIND_ADDRESS env var when HOST is not set", () => {
      delete process.env.HOST;
      process.env.BIND_ADDRESS = "192.168.1.100";

      const config = orchestratorConfig();

      expect(config.host).toBe("192.168.1.100");
    });

    it("should prefer HOST over BIND_ADDRESS when both are set", () => {
      process.env.HOST = "0.0.0.0";
      process.env.BIND_ADDRESS = "192.168.1.100";

      const config = orchestratorConfig();

      expect(config.host).toBe("0.0.0.0");
    });
  });

  describe("other config values", () => {
    it("should use default port when ORCHESTRATOR_PORT is not set", () => {
      delete process.env.ORCHESTRATOR_PORT;

      const config = orchestratorConfig();

      expect(config.port).toBe(3001);
    });

    it("should use provided port when ORCHESTRATOR_PORT is set", () => {
      process.env.ORCHESTRATOR_PORT = "4000";

      const config = orchestratorConfig();

      expect(config.port).toBe(4000);
    });

    it("should use default valkey config when not set", () => {
      delete process.env.VALKEY_HOST;
      delete process.env.VALKEY_PORT;
      delete process.env.VALKEY_URL;

      const config = orchestratorConfig();

      expect(config.valkey.host).toBe("localhost");
      expect(config.valkey.port).toBe(6379);
      expect(config.valkey.url).toBe("redis://localhost:6379");
    });

    it("should derive valkey host and port from VALKEY_URL when VALKEY_HOST/VALKEY_PORT are not set", () => {
      delete process.env.VALKEY_HOST;
      delete process.env.VALKEY_PORT;
      process.env.VALKEY_URL = "redis://valkey:6380";

      const config = orchestratorConfig();

      expect(config.valkey.host).toBe("valkey");
      expect(config.valkey.port).toBe(6380);
      expect(config.valkey.url).toBe("redis://valkey:6380");
    });

    it("should derive valkey password from VALKEY_URL when VALKEY_PASSWORD is not set", () => {
      delete process.env.VALKEY_PASSWORD;
      delete process.env.VALKEY_HOST;
      delete process.env.VALKEY_PORT;
      process.env.VALKEY_URL = "redis://:url-secret@valkey:6379";

      const config = orchestratorConfig();

      expect(config.valkey.password).toBe("url-secret");
    });

    it("should prefer explicit valkey env vars over VALKEY_URL values", () => {
      process.env.VALKEY_HOST = "explicit-host";
      process.env.VALKEY_PORT = "6390";
      process.env.VALKEY_PASSWORD = "explicit-password";
      process.env.VALKEY_URL = "redis://:url-secret@valkey:6380";

      const config = orchestratorConfig();

      expect(config.valkey.host).toBe("explicit-host");
      expect(config.valkey.port).toBe(6390);
      expect(config.valkey.password).toBe("explicit-password");
    });
  });

  describe("valkey timeout config (SEC-ORCH-28)", () => {
    it("should use default connectTimeout of 5000 when not set", () => {
      delete process.env.VALKEY_CONNECT_TIMEOUT_MS;

      const config = orchestratorConfig();

      expect(config.valkey.connectTimeout).toBe(5000);
    });

    it("should use provided connectTimeout when VALKEY_CONNECT_TIMEOUT_MS is set", () => {
      process.env.VALKEY_CONNECT_TIMEOUT_MS = "10000";

      const config = orchestratorConfig();

      expect(config.valkey.connectTimeout).toBe(10000);
    });

    it("should use default commandTimeout of 3000 when not set", () => {
      delete process.env.VALKEY_COMMAND_TIMEOUT_MS;

      const config = orchestratorConfig();

      expect(config.valkey.commandTimeout).toBe(3000);
    });

    it("should use provided commandTimeout when VALKEY_COMMAND_TIMEOUT_MS is set", () => {
      process.env.VALKEY_COMMAND_TIMEOUT_MS = "8000";

      const config = orchestratorConfig();

      expect(config.valkey.commandTimeout).toBe(8000);
    });
  });

  describe("spawner config", () => {
    it("should use default maxConcurrentAgents of 2 when not set", () => {
      delete process.env.MAX_CONCURRENT_AGENTS;

      const config = orchestratorConfig();

      expect(config.spawner.maxConcurrentAgents).toBe(2);
    });

    it("should use provided maxConcurrentAgents when MAX_CONCURRENT_AGENTS is set", () => {
      process.env.MAX_CONCURRENT_AGENTS = "50";

      const config = orchestratorConfig();

      expect(config.spawner.maxConcurrentAgents).toBe(50);
    });

    it("should handle MAX_CONCURRENT_AGENTS of 10", () => {
      process.env.MAX_CONCURRENT_AGENTS = "10";

      const config = orchestratorConfig();

      expect(config.spawner.maxConcurrentAgents).toBe(10);
    });
  });

  describe("AI provider config", () => {
    it("should default aiProvider to ollama when unset", () => {
      delete process.env.AI_PROVIDER;

      const config = orchestratorConfig();

      expect(config.aiProvider).toBe("ollama");
    });

    it("should normalize AI provider to lowercase", () => {
      process.env.AI_PROVIDER = "  cLaUdE  ";

      const config = orchestratorConfig();

      expect(config.aiProvider).toBe("claude");
    });

    it("should fallback unsupported AI provider to ollama", () => {
      process.env.AI_PROVIDER = "bad-provider";

      const config = orchestratorConfig();

      expect(config.aiProvider).toBe("ollama");
    });
  });
});
