# frozen_string_literal: true

require_relative 'test_helper'
require 'open3'

class TestEntryPoint < Minitest::Test
  include TestHelpers

  SCRIPT_PATH = File.expand_path('../plugin_config.rb', __dir__)

  def setup
    setup_temp_dirs
  end

  def teardown
    teardown_temp_dirs
  end

  def test_valid_enable_operation_exits_0_with_valid_json
    create_settings({})
    create_marketplace(plugins: [])

    stdout, stderr, status = run_script(
      'enable',
      "--plugin=test-plugin",
      "--marketplace=test-marketplace",
      "--project=#{@project_dir}"
    )

    assert status.success?, "Expected exit 0, got #{status.exitstatus}. stderr: #{stderr}"
    parsed = JSON.parse(stdout)
    assert_includes %w[created_and_enabled enabled], parsed['status']
  end

  def test_unknown_operation_exits_1
    _stdout, stderr, status = run_script('bogus_operation')

    assert_equal 1, status.exitstatus
    parsed = JSON.parse(stderr)
    assert_equal 'invalid_operation', parsed['error']
  end

  def test_no_operation_exits_1
    _stdout, stderr, status = run_script

    assert_equal 1, status.exitstatus
    parsed = JSON.parse(stderr)
    assert_equal 'invalid_operation', parsed['error']
  end

  def test_list_operation_exits_0
    create_settings('a@b' => true)

    stdout, _stderr, status = run_script(
      'list',
      "--project=#{@project_dir}"
    )

    assert status.success?
    parsed = JSON.parse(stdout)
    assert_equal 'ok', parsed['status']
    assert_includes parsed['enabled_plugins'], 'a@b'
  end

  def test_output_is_always_valid_json_on_error
    # Pass a bad project path so the script hits an error path
    _stdout, stderr, status = run_script('bogus')

    refute status.success?
    # Should be valid JSON on stderr
    parsed = JSON.parse(stderr)  # will raise if not valid JSON
    assert parsed.is_a?(Hash), 'Expected stderr to contain a JSON object'
  end

  def test_disable_operation_exits_0
    create_settings('test-plugin@test-marketplace' => true)

    stdout, _stderr, status = run_script(
      'disable',
      "--plugin=test-plugin",
      "--marketplace=test-marketplace",
      "--project=#{@project_dir}"
    )

    assert status.success?
    parsed = JSON.parse(stdout)
    assert_equal 'disabled', parsed['status']
  end

  def test_schema_error_exits_2_with_malformed_settings
    # Create a malformed settings.json missing required 'enabledPlugins' key
    # This will trigger schema validation in the Enable operation
    malformed_settings = '{"badKey": "value"}'
    FileUtils.mkdir_p(File.dirname(settings_path))
    File.write(settings_path, malformed_settings)

    _stdout, stderr, status = run_script(
      'enable',
      "--plugin=test-plugin",
      "--marketplace=test-marketplace",
      "--project=#{@project_dir}"
    )

    assert_equal 2, status.exitstatus, "Expected exit code 2 for schema error, got #{status.exitstatus}"
    parsed = JSON.parse(stderr)
    assert_equal 'schema_mismatch', parsed['error']
  end

  private

  def run_script(*args)
    Open3.capture3('ruby', SCRIPT_PATH, *args)
  end
end
