defmodule Refuge.BazelConfigTest do
  use ExUnit.Case

  @stage1_dir "/Users/osayame/trace/examples/go-tutorial/stage1"
  @stage2_dir "/Users/osayame/trace/examples/go-tutorial/stage2"
  @bazel_path "/usr/local/bin/bazel"
  @target "//:print_fortune"

  @tag :integration
  test "test bazelrc configuration affects build behavior" do
    # First let's check if stage1 has a .bazelrc file
    stage1_bazelrc = Path.join(@stage1_dir, ".bazelrc")
    stage2_bazelrc = Path.join(@stage2_dir, ".bazelrc")

    # Log the existence status of .bazelrc files
    IO.puts("Stage1 .bazelrc exists: #{File.exists?(stage1_bazelrc)}")
    IO.puts("Stage2 .bazelrc exists: #{File.exists?(stage2_bazelrc)}")

    # Create temporary .bazelrc in stage1 that would enable build
    if !File.exists?(stage1_bazelrc) do
      # Create a backup of any existing .bazelrc
      if File.exists?(stage1_bazelrc) do
        File.cp!(stage1_bazelrc, "#{stage1_bazelrc}.bak")
      end

      # Try to build without .bazelrc first to confirm it fails
      {build_output_before, build_status_before} =
        System.cmd(
          @bazel_path,
          ["build", @target],
          cd: @stage1_dir,
          stderr_to_stdout: true
        )

      # Log the initial build attempt
      IO.puts("Build without proper .bazelrc:")
      IO.puts(build_output_before)

      # Now create minimal .bazelrc in stage1 to test if config is the issue
      File.write!(stage1_bazelrc, """
      # Basic .bazelrc configuration for test
      build --symlink_prefix=bazel-
      """)

      # Try to build again with basic .bazelrc
      {build_output_after, build_status_after} =
        System.cmd(
          @bazel_path,
          ["build", @target],
          cd: @stage1_dir,
          stderr_to_stdout: true
        )

      # Log the second build attempt
      IO.puts("Build with minimal .bazelrc:")
      IO.puts(build_output_after)

      # Clean up - remove the temporary .bazelrc
      File.rm(stage1_bazelrc)

      # If we had backed up an existing file, restore it
      if File.exists?("#{stage1_bazelrc}.bak") do
        File.cp!("#{stage1_bazelrc}.bak", stage1_bazelrc)
        File.rm("#{stage1_bazelrc}.bak")
      end

      # The issue isn't just .bazelrc if both attempts failed similarly
      assert build_status_before != 0
      assert build_status_after != 0
      assert build_output_before =~ "no such target"
      assert build_output_after =~ "no such target"

      # Check if the issue is related to the BUILD file
      stage1_build_file = Path.join(@stage1_dir, "BUILD")
      stage2_build_file = Path.join(@stage2_dir, "BUILD")

      IO.puts("Stage1 BUILD file exists: #{File.exists?(stage1_build_file)}")
      IO.puts("Stage2 BUILD file exists: #{File.exists?(stage2_build_file)}")

      # Read BUILD file contents if they exist
      if File.exists?(stage1_build_file) do
        IO.puts("Stage1 BUILD file contents:")
        IO.puts(File.read!(stage1_build_file))
      end

      if File.exists?(stage2_build_file) do
        IO.puts("Stage2 BUILD file contents:")
        IO.puts(File.read!(stage2_build_file))
      end
    end
  end
end
