#!/usr/bin/env bun
/**
 * Configuration for upstream merge automation
 */

export interface PackageMapping {
  from: string
  to: string
}

export interface MergeConfig {
  /** Package name mappings from opencode to kilo */
  packageMappings: PackageMapping[]

  /** Files to always keep Kilo's version (never take upstream changes) */
  keepOurs: string[]

  /** Files to skip entirely (don't add from upstream, remove if added) */
  skipFiles: string[]

  /** Files that should take upstream version and apply Kilo branding transforms */
  takeTheirsAndTransform: string[]

  /** Tauri/Desktop config files with predictable branding patterns */
  tauriFiles: string[]

  /** Script files with GitHub API references */
  scriptFiles: string[]

  /** Extension files (Zed, etc.) */
  extensionFiles: string[]

  /** Web/docs files */
  webFiles: string[]

  /** Lock files to accept ours and regenerate after merge */
  lockFiles: string[]

  /** Directories that are Kilo-specific and should be preserved */
  kiloDirectories: string[]

  /** File patterns to exclude from codemods */
  excludePatterns: string[]

  /** Default branch to merge into */
  baseBranch: string

  /** Branch prefix for merge branches */
  branchPrefix: string

  /** Remote name for upstream */
  upstreamRemote: string

  /** Remote name for origin */
  originRemote: string

  /** i18n file patterns that need string transformation */
  i18nPatterns: string[]
}

export const defaultConfig: MergeConfig = {
  packageMappings: [
    { from: "opencode-ai", to: "@kilocode/cli" },
    { from: "@opencode-ai/cli", to: "@kilocode/cli" },
    { from: "@opencode-ai/sdk", to: "@kilocode/sdk" },
    { from: "@opencode-ai/plugin", to: "@kilocode/plugin" },
  ],

  keepOurs: [
    "README.md",
    "CONTRIBUTING.md",
    "CODE_OF_CONDUCT.md",
    "PRIVACY.md",
    "SECURITY.md",
    "AGENTS.md",
    // GitHub workflows - MANUAL REVIEW (can break CI/CD)
    ".github/workflows/publish-stable.yml",
    ".github/workflows/publish.yml",
    ".github/workflows/close-stale-prs.yml",
    ".github/pull_request_template.md",
    // Kilo-specific command files
    ".opencode/command/commit.md",
    // Kilo-specific publish scripts
    "packages/opencode/script/publish-registries.ts",
    // GitHub Action - Kilo version is fully ported and complete
    "github/action.yml",
    "github/README.md",
    "github/.gitignore",
    "github/script/release",
    "github/script/publish",
  ],

  // Files that only exist in upstream and should NOT be added to Kilo
  // These are removed during merge if they appear
  skipFiles: [
    // Translated README files (Kilo doesn't have these)
    "README.ar.md",
    "README.br.md",
    "README.bs.md",
    "README.da.md",
    "README.de.md",
    "README.es.md",
    "README.fr.md",
    "README.it.md",
    "README.ja.md",
    "README.ko.md",
    "README.no.md",
    "README.pl.md",
    "README.ru.md",
    "README.th.md",
    "README.tr.md",
    "README.zh.md",
    "README.zht.md",
    // Stats file
    "STATS.md",
    // Workflows that don't exist in Kilo
    ".github/workflows/update-nix-hashes.yml",
    ".github/workflows/deploy.yml",
    ".github/workflows/docs-update.yml",
    ".github/workflows/docs-locale-sync.yml",
    // Vouch files (Kilo doesn't use Vouch)
    ".github/VOUCHED.md",
    ".github/workflows/vouch-check-issue.yml",
    ".github/workflows/vouch-check-pr.yml",
    ".github/workflows/vouch-manage-by-issue.yml",
    // SST infrastructure files (Kilo is CLI-only, no hosted platform)
    "sst.config.ts",
    "sst-env.d.ts",
    // Hosted platform packages (not needed for CLI)
    "infra/**",
    "packages/console/**",
    "packages/enterprise/**",
    "packages/web/**",
    "packages/slack/**",
    "packages/function/**",
    "packages/docs/**",
    "packages/identity/**",
    // GitHub Action - Kilo version is fully ported and complete
    "github/index.ts",
    "github/package.json",
    "github/tsconfig.json",
    "github/bun.lock",
    "github/sst-env.d.ts",
  ],

  // Files that should take upstream version and apply Kilo branding transforms
  // These are files with only branding differences, no logic changes
  takeTheirsAndTransform: [
    // App components with branding only
    "packages/app/src/components/**/*.tsx",
    "packages/app/src/context/**/*.tsx",
    "packages/app/src/pages/**/*.tsx",
    // UI components
    "packages/ui/src/components/**/*.tsx",
    "packages/ui/src/context/**/*.tsx",
    // Desktop TypeScript files (not Rust)
    "packages/desktop/src/**/*.ts",
    // E2E and test fixtures
    "packages/app/e2e/**/*.ts",
    "packages/app/script/**/*.ts",
  ],

  // Tauri/Desktop config files with predictable branding patterns
  tauriFiles: [
    "packages/desktop/src-tauri/tauri.conf.json",
    "packages/desktop/src-tauri/tauri.prod.conf.json",
    "packages/desktop/src-tauri/Cargo.toml",
    "packages/desktop/src-tauri/Cargo.lock",
    "packages/desktop/src-tauri/src/*.rs",
  ],

  // Script files with GitHub API references
  scriptFiles: ["script/*.ts", "packages/opencode/script/*.ts"],

  // Extension files
  extensionFiles: ["packages/extensions/**/*"],

  // Web/docs files
  webFiles: [],

  // Lock files and generated files to accept ours and regenerate after merge
  // Note: nix/hashes.json is regenerated by CI (update-nix-hashes.yml), not locally
  lockFiles: [
    "bun.lock",
    "**/bun.lock",
    "package-lock.json",
    "**/package-lock.json",
    "yarn.lock",
    "**/yarn.lock",
    "pnpm-lock.yaml",
    "**/pnpm-lock.yaml",
    "Cargo.lock",
    "**/Cargo.lock",
    "nix/hashes.json",
  ],

  kiloDirectories: [
    "packages/opencode/src/kilocode",
    "packages/opencode/test/kilocode",
    "packages/kilo-gateway",
    "packages/kilo-telemetry",
    "script/upstream",
  ],

  excludePatterns: [
    "**/node_modules/**",
    "**/dist/**",
    "**/.git/**",
    "**/bun.lock",
    "**/package-lock.json",
    "**/yarn.lock",
  ],

  baseBranch: "dev",
  branchPrefix: "upstream-merge",
  upstreamRemote: "upstream",
  originRemote: "origin",

  // i18n translation files that need Kilo branding transforms
  i18nPatterns: ["packages/*/src/i18n/*.ts", "packages/desktop/src/i18n/*.ts"],
}

export function loadConfig(overrides?: Partial<MergeConfig>): MergeConfig {
  return { ...defaultConfig, ...overrides }
}
