import {
  CLAUDE_CODE_DEFAULT_ENV,
  DEFAULT_OVERVIEW_WIDGETS,
  DEFAULT_TRAY_COMPONENT_VARIANTS,
  DEFAULT_TRAY_WIDGETS,
  DEFAULT_TRAY_WINDOW_MODULES,
  type AppConfig,
  type ProxyRouteTarget
} from "@ccr/core/contracts/app";
import { defaultRequestLogBodyBytes } from "@ccr/core/observability/request-log-limits";

export const DEFAULT_PROXY_TARGETS: ProxyRouteTarget[] = [
  { host: "api.anthropic.com", paths: ["/v1/messages", "/v1/messages/count_tokens"] },
  { host: "api.openai.com", paths: ["/v1/chat/completions", "/v1/responses", "/v1/models"] },
  { host: "generativelanguage.googleapis.com", paths: ["/v1beta/models", "/v1/models"] },
  { host: "openrouter.ai", paths: ["/api/v1/chat/completions", "/api/v1/responses", "/api/v1/models"] },
  { host: "api.deepseek.com", paths: ["/chat/completions", "/v1/chat/completions", "/models", "/v1/models"] },
  { host: "api.mistral.ai", paths: ["/v1/chat/completions", "/v1/models"] }
];

export type DefaultAppConfigOptions = {
  coreHost?: string;
  generatedConfigFile: string;
};

export function createDefaultAppConfig(options: DefaultAppConfigOptions): AppConfig {
  const coreHost = options.coreHost ?? "127.0.0.1";
  return {
    APIKEY: "",
    APIKEYS: [],
    API_TIMEOUT_MS: 600000,
    CUSTOM_ROUTER_PATH: "",
    HOST: "127.0.0.1",
    PORT: 3456,
    Providers: [],
    Router: {
      builtInRules: {
        "claude-code": {
          enabled: true
        },
        codex: {
          enabled: true
        }
      },
      fallback: {
        mode: "off",
        models: [],
        retryCount: 1
      },
      rules: []
    },
    agent: {
      mcpServers: []
    },
    autoStart: false,
    botConfigs: [],
    botGateway: {
      acknowledgeEvents: false,
      args: [],
      authType: "",
      autoStartIntegration: true,
      command: "",
      createIntegration: false,
      credentials: {},
      cwd: "",
      enabled: false,
      forwardAllAgentMessages: true,
      handoff: {
        enabled: false,
        idleSeconds: 30,
        phoneBluetoothTargets: [],
        phoneWifiTargets: [],
        screenLock: true,
        userIdle: true
      },
      integrationConfig: {},
      integrationId: "",
      language: "auto",
      maxAttachmentBytes: 20 * 1024 * 1024,
      maxTurnTimeMs: 10 * 60 * 1000,
      mediaEnabled: true,
      messageChunkChars: 3500,
      platform: "none",
      pollIntervalMs: 2000,
      requestTimeoutMs: 600000,
      sessionIdleMinutes: 0,
      shellEnabled: false,
      sourceDir: "",
      startupTimeoutMs: 10000,
      stateDir: "",
      streamReplies: true,
      tenantId: "ccr"
    },
    gateway: {
      coreHost,
      corePort: 3457,
      enabled: true,
      generatedConfigFile: options.generatedConfigFile,
      host: "127.0.0.1",
      port: 3456
    },
    launchAtLogin: false,
    observability: {
      agentAnalysis: false,
      requestLogBodyCapture: "all",
      requestLogMaxBodyBytes: defaultRequestLogBodyBytes,
      requestLogSuccessSampleRate: 1,
      requestLogs: false
    },
    preferredProvider: "",
    plugins: [],
    profile: {
      claudeCode: {
        enabled: true,
        model: "",
        settingsFile: "~/.claude/settings.json",
        smallFastModel: ""
      },
      codex: {
        cliMiddleware: true,
        codexCliPath: "",
        codexHome: "",
        configFormat: "separate_profile_files",
        configFile: "~/.codex/config.toml",
        enabled: true,
        model: "",
        providerId: "claude-code-router",
        providerName: "Claude Code Router",
        showAllSessions: false
      },
      enabled: true,
      profiles: [
        {
          agent: "claude-code",
          enabled: true,
          env: { ...CLAUDE_CODE_DEFAULT_ENV },
          id: "default-claude-code",
          model: "",
          name: "Claude Code",
          scope: "global",
          settingsFile: "~/.claude/settings.json",
          smallFastModel: "",
          surface: "auto"
        },
        {
          agent: "codex",
          cliMiddleware: true,
          codexCliPath: "",
          codexHome: "",
          configFormat: "separate_profile_files",
          configFile: "~/.codex/config.toml",
          enabled: true,
          env: {},
          id: "default-codex",
          model: "",
          name: "Codex",
          providerId: "claude-code-router",
          providerName: "Claude Code Router",
          showAllSessions: false,
          scope: "global",
          surface: "auto"
        }
      ]
    },
    proxy: {
      browserMode: true,
      captureNetwork: false,
      enabled: false,
      host: "127.0.0.1",
      mode: "gateway",
      port: 7890,
      systemProxy: false,
      targets: DEFAULT_PROXY_TARGETS,
      upstream: {
        custom: {
          password: "",
          port: 7890,
          server: "",
          username: ""
        },
        mode: "system"
      }
    },
    providerPlugins: [],
    overviewWidgets: DEFAULT_OVERVIEW_WIDGETS,
    routerEndpoint: "http://127.0.0.1:3456",
    theme: "system",
    trayComponentVariants: DEFAULT_TRAY_COMPONENT_VARIANTS,
    trayIcon: "random",
    trayProgressTargetTokens: 100000,
    trayWidgets: DEFAULT_TRAY_WIDGETS,
    trayWindowModules: DEFAULT_TRAY_WINDOW_MODULES,
    toolHub: {
      browserAutomation: false,
      enabled: false,
      llm: {
        apiKey: "",
        baseUrl: "https://api.openai.com/v1",
        model: ""
      },
      mcpServers: [],
      maxTools: 10,
      requestTimeoutMs: 60000
    },
    virtualModelProfiles: []
  };
}
