defmodule Raxol.Terminal.PlatformSpecificTest do
  use ExUnit.Case
  alias Raxol.Terminal.{Renderer, ScreenBuffer}

  # These tests require a real terminal environment with specific env vars
  # Skip in CI where these are not available
  @moduletag :platform_specific

  defp render_hello do
    buffer = ScreenBuffer.new(80, 24)

    buffer =
      Enum.reduce(String.graphemes("Hello"), {buffer, 0}, fn char, {buf, x} ->
        {ScreenBuffer.write_char(buf, x, 0, char, nil), x + 1}
      end)
      |> elem(0)

    renderer = Renderer.new(buffer)
    Renderer.render(renderer)
  end

  describe "platform-specific terminal features" do
    @tag :requires_terminal
    test ~c"terminal type detection" do
      case System.get_env("TERM") do
        nil ->
          # Skip in CI environments without TERM
          assert true

        term ->
          assert is_binary(term)
      end
    end

    @tag :requires_terminal
    test ~c"color support detection" do
      case System.get_env("COLORTERM") do
        nil ->
          # COLORTERM is optional - not all terminals set it
          assert true

        colors ->
          assert is_binary(colors)
      end
    end

    @tag :requires_terminal
    test ~c"terminal size detection" do
      case :io.columns() do
        {:ok, width} ->
          assert width > 0

        {:error, _} ->
          # Not a TTY (CI environment)
          assert true
      end
    end

    @tag :requires_terminal
    test ~c"UTF-8 support" do
      case System.get_env("LANG") do
        nil ->
          # LANG not set in CI
          assert true

        lang ->
          # Allow any LANG value in CI - UTF-8 check is environment-specific
          assert is_binary(lang)
      end
    end

    @tag :requires_terminal
    test ~c"graphics support detection" do
      case System.get_env("TERM_PROGRAM") do
        nil ->
          # TERM_PROGRAM is optional - CI environments don't have it
          assert true

        term_program ->
          assert is_binary(term_program)
      end
    end
  end

  describe "platform-specific rendering" do
    test ~c"renders with platform-specific colors" do
      output = render_hello()
      assert output =~ "H"
      assert output =~ "e"
      assert output =~ "l"
      assert output =~ "o"
    end

    test ~c"handles platform-specific terminal features" do
      output = render_hello()
      assert output =~ "H"
      assert output =~ "e"
      assert output =~ "l"
      assert output =~ "o"
    end
  end

  describe "platform-specific input handling" do
    test ~c"handles platform-specific key codes" do
      output = render_hello()
      assert output =~ "H"
      assert output =~ "e"
      assert output =~ "l"
      assert output =~ "o"
    end

    test ~c"handles platform-specific mouse events" do
      output = render_hello()
      assert output =~ "H"
      assert output =~ "e"
      assert output =~ "l"
      assert output =~ "o"
    end
  end
end
