{ pkgs, config, ... }:

let
  repoSccacheDir = "${config.git.root}/.devenv/sccache";
  sharedNixConfig = ''
    sandbox = relaxed
  '';
in {
  dotenv.enable = true;
  dotenv.filename = [ ".env" ".env.hcloud" ];

  packages = with pkgs; [
    git
    gnugrep
    gnused
    hcloud
    jq
    nix
    openssh
    python3
    ccache
  ];

  env = {
    CG_ROOT = config.git.root;
    CG_REF = "git+file://${config.git.root}";
    CG_SCCACHE_ROOT = repoSccacheDir;
    CG_SCCACHE_DIR = "${repoSccacheDir}/cache";
    CG_SCCACHE_CONFIG_PATH = "${repoSccacheDir}/config.toml";
    CG_SCCACHE_RUNTIME_ENV_PATH = "${config.git.root}/nix/sccache-runtime.env";
    CG_SCCACHE_SERVER_PATH = "${repoSccacheDir}/builder.sock";
    NIX_CONFIG = sharedNixConfig;
  };

  tasks."image:build" = {
    exec = ''nix run "git+file://${config.git.root}#remote" -- image'';
    cwd = config.git.root;
  };

  tasks."image:push" = {
    after = [ "image:build@completed" ];
    exec = ''nix run "git+file://${config.git.root}#remote" -- push'';
    cwd = config.git.root;
  };

  tasks."image:purge" = {
    after = [ "image:push@completed" ];
    exec = ''nix run "git+file://${config.git.root}#remote" -- purge'';
    cwd = config.git.root;
  };

  tasks."remote:test" = {
    exec = ''nix run "git+file://${config.git.root}#remote" -- test'';
    cwd = config.git.root;
  };

  tasks."remote:build" = {
    after = [ "remote:test@completed" ];
    exec = ''nix run "git+file://${config.git.root}#remote" -- build'';
    cwd = config.git.root;
  };

  tasks."remote:clean" = {
    after = [ "remote:build@completed" ];
    exec = ''nix run "git+file://${config.git.root}#remote" -- clean'';
    cwd = config.git.root;
  };

  tasks."worker:list" = {
    exec = ''nix run "git+file://${config.git.root}#remote" -- list'';
    cwd = config.git.root;
  };

  tasks."worker:status" = {
    exec = ''nix run "git+file://${config.git.root}#remote" -- status'';
    cwd = config.git.root;
  };

  tasks."worker:ssh" = {
    exec = ''nix run "git+file://${config.git.root}#remote" -- ssh'';
    cwd = config.git.root;
  };

  tasks."detect:launch" = {
    exec = ''nix run "git+file://${config.git.root}#detect" -- smoke --launch --headful'';
    cwd = config.git.root;
  };

  tasks."detect:test" = {
    exec = ''
      # Run the test derivation - this uses the already-built Chromium
      result=$(nix build "git+file://${config.git.root}#detect-test-result" --no-link --print-out-paths)
      
      # Show results
      echo ""
      echo "=== Test Results ==="
      ${pkgs.jq}/bin/jq '{
        target: .results[0].target.label,
        browser: .results[0].browserVersion.Browser,
        counts: .results[0].evaluation.counts,
        passRate: ((.results[0].evaluation.counts.pass / (.results[0].evaluation.counts.pass + .results[0].evaluation.counts.warn + .results[0].evaluation.counts.fail)) * 100 | floor)
      }' "$result/summary.json"
      
      # Save results for later inspection (copy contents, not the store path itself)
      rm -rf "${config.git.root}/.devenv/results"
      mkdir -p "${config.git.root}/.devenv/results"
      cp -Lr "$result"/* "${config.git.root}/.devenv/results/"
      chmod -R u+w "${config.git.root}/.devenv/results" 2>/dev/null || true
      echo ""
      echo "Results saved to: ${config.git.root}/.devenv/results/"
      echo "View with: cat ${config.git.root}/.devenv/results/summary.json | jq ."
    '';
    cwd = config.git.root;
  };

  tasks."canary:launch" = {
    exec = ''nix run "git+file://${config.git.root}#detect" -- canary --launch --headful'';
    cwd = config.git.root;
  };

  tasks."canary:public" = {
    exec = ''nix run "git+file://${config.git.root}#detect" -- canary --launch --headful --public'';
    cwd = config.git.root;
  };

  tasks."canary:test" = {
    exec = ''nix run "git+file://${config.git.root}#detect" -- canary --launch --headful'';
    cwd = config.git.root;
  };

  tasks."gc:workers" = {
    exec = ''nix run "git+file://${config.git.root}#remote" -- gc'';
    cwd = config.git.root;
  };

  tasks."hold:builder" = {
    exec = ''nix run "git+file://${config.git.root}#remote" -- up'';
    cwd = config.git.root;
  };

  tasks."ccache:test" = {
    exec = ''
      echo "=== Testing ccache configuration ==="
      
      tmpdir=$(mktemp -d)
      echo 'int main() { return 0; }' > "$tmpdir/test.c"
      
      export CCACHE_DIR="/var/cache/ccache"
      export CCACHE_MAXSIZE="50G"
      
      ccache="${pkgs.ccache}/bin/ccache"
      cc="${pkgs.stdenv.cc}/bin/cc"
      
      echo "Testing ccache with system cc (gcc)..."
      
      # First compile - miss
      "$ccache" "$cc" -c "$tmpdir/test.c" -o "$tmpdir/test.o" 2>&1
      echo "First compile: expected MISS"
      
      # Remove output
      rm "$tmpdir/test.o"
      
      # Second compile - hit  
      "$ccache" "$cc" -c "$tmpdir/test.c" -o "$tmpdir/test.o" 2>&1
      echo "Second compile: expected HIT"
      
      # Show stats
      echo ""
      echo "=== ccache stats ==="
      "$ccache" -s 2>&1 | grep -E "(Cacheable|Hits|Misses|Cache size)" | head -6
      
      # Cleanup
      rm -rf "$tmpdir"
      echo ""
      echo "ccache test complete"
    '';
    cwd = config.git.root;
  };

  tasks."release:builder" = {
    exec = ''nix run "git+file://${config.git.root}#remote" -- down'';
    cwd = config.git.root;
  };

  tasks."kill:worker" = {
    exec = ''nix run "git+file://${config.git.root}#remote" -- kill'';
    cwd = config.git.root;
  };
}
