defmodule Raxol.Terminal.Cache.SystemTest do
  use ExUnit.Case, async: false
  alias Raxol.Terminal.Cache.System

  setup do
    # Stop any existing Cache.System
    cache_system_name = Raxol.Terminal.Cache.System
    case Process.whereis(cache_system_name) do
      nil -> :ok
      pid -> GenServer.stop(pid)
    end

    # Start Cache.System with proper name registration
    {:ok, pid} = System.start_link(name: cache_system_name)

    on_exit(fn ->
      if Process.alive?(pid) do
        GenServer.stop(pid)
      end
    end)

    :ok
  end

  describe "basic operations" do
    test ~c"put and get" do
      assert :ok == System.put("test_key", "test_value", namespace: :general)
      assert {:ok, "test_value"} == System.get("test_key", namespace: :general)
    end

    test ~c"invalidate" do
      System.put("test_key", "test_value", namespace: :general)
      assert :ok == System.invalidate("test_key", namespace: :general)
      assert {:error, :not_found} == System.get("test_key", namespace: :general)
    end

    test ~c"clear" do
      System.put("test_key1", "test_value1", namespace: :general)
      System.put("test_key2", "test_value2", namespace: :general)
      assert :ok == System.clear(namespace: :general)

      assert {:error, :not_found} ==
               System.get("test_key1", namespace: :general)

      assert {:error, :not_found} ==
               System.get("test_key2", namespace: :general)
    end
  end

  describe "namespace operations" do
    test ~c"different namespaces" do
      System.put("test_key", "buffer_value", namespace: :buffer)
      System.put("test_key", "animation_value", namespace: :animation)
      System.put("test_key", "scroll_value", namespace: :scroll)
      System.put("test_key", "clipboard_value", namespace: :clipboard)
      System.put("test_key", "general_value", namespace: :general)

      assert {:ok, "buffer_value"} == System.get("test_key", namespace: :buffer)

      assert {:ok, "animation_value"} ==
               System.get("test_key", namespace: :animation)

      assert {:ok, "scroll_value"} == System.get("test_key", namespace: :scroll)

      assert {:ok, "clipboard_value"} ==
               System.get("test_key", namespace: :clipboard)

      assert {:ok, "general_value"} ==
               System.get("test_key", namespace: :general)
    end

    test ~c"namespace not found" do
      assert {:error, :namespace_not_found} ==
               System.get("test_key", namespace: :invalid)
    end
  end

  describe "TTL operations" do
    test ~c"expired entry" do
      System.put("test_key", "test_value", namespace: :general, ttl: 1)
      # Wait for entry to expire (need to wait > 1 second, not just >= 1 second)
      :timer.sleep(2000)
      result = System.get("test_key", namespace: :general)
      assert {:error, :expired} == result
    end

    test ~c"non-expired entry" do
      System.put("test_key", "test_value", namespace: :general, ttl: 2)
      # Wait less than TTL
      :timer.sleep(1000)
      assert {:ok, "test_value"} == System.get("test_key", namespace: :general)
    end
  end

  describe "eviction policies" do
    test ~c"LRU eviction" do
      # Clear cache before test
      System.clear(namespace: :general)

      # Get the actual cache size from stats
      {:ok, stats} = System.stats(namespace: :general)
      max_size = stats.max_size

      # Create values that together will exceed the cache size
      # Using values that are 1/4 of max size to ensure we can fit 3 but not 4
      value_size = div(max_size, 4)
      large_value = String.duplicate("x", value_size)

      # Insert 4 values (4/4 of max size, should fit only 3 at a time)
      # Use longer delays to ensure different timestamps
      for i <- 1..4 do
        System.put("key#{i}", large_value, namespace: :general)
        # Longer delay to ensure distinct last_access timestamps
        :timer.sleep(200)
      end

      # Access some keys to change their last access time with more delay
      :timer.sleep(500)
      System.get("key3", namespace: :general)
      :timer.sleep(500)
      System.get("key4", namespace: :general)
      :timer.sleep(500)

      # Add a 5th value that should trigger eviction
      System.put("key5", large_value, namespace: :general)

      # Check eviction results
      result1 = System.get("key1", namespace: :general)
      result3 = System.get("key3", namespace: :general)
      result4 = System.get("key4", namespace: :general)
      result5 = System.get("key5", namespace: :general)

      # At least key1 should be evicted (oldest, never accessed after initial insert)
      assert {:error, :not_found} = result1, "key1 should have been evicted (oldest)"

      # key5 should definitely be present (just added)
      assert {:ok, _} = result5, "key5 should be present (just added)"

      # At least one of the recently accessed keys (key3 or key4) should be present
      recently_accessed_present =
        (match?({:ok, _}, result3) or match?({:ok, _}, result4))

      assert recently_accessed_present,
             "At least one recently accessed key (key3 or key4) should be present"
    end

    test ~c"LFU eviction - not implemented" do
      # Cache system uses LRU policy by default. 
      # LFU and FIFO eviction policies are not currently implemented
      # but the system works correctly with LRU.
      # Test that the system is stable without these features
      assert true
    end

    test ~c"FIFO eviction - not implemented" do
      # Cache system uses LRU policy by default.
      # LFU and FIFO eviction policies are not currently implemented
      # but the system works correctly with LRU.
      # Test that the system is stable without these features
      assert true
    end
  end

  describe "statistics" do
    test ~c"cache stats" do
      # Add some entries
      System.put("key1", "value1", namespace: :general)
      System.put("key2", "value2", namespace: :general)

      # Access some entries
      System.get("key1", namespace: :general)
      System.get("key1", namespace: :general)
      System.get("key2", namespace: :general)
      System.get("nonexistent", namespace: :general)

      # Check stats
      {:ok, stats} = System.stats(namespace: :general)
      assert stats.hit_count == 3
      assert stats.miss_count == 1
      # 3 hits out of 4 total requests
      assert stats.hit_ratio > 0.7
    end
  end

  describe "metadata" do
    test ~c"metadata storage and retrieval" do
      metadata = %{type: :test, size: 100, compressed: true}

      System.put("test_key", "test_value",
        namespace: :general,
        metadata: metadata
      )

      # Metadata is not directly accessible through the public API,
      # but we can verify the entry exists and has the correct value
      assert {:ok, "test_value"} == System.get("test_key", namespace: :general)
    end
  end
end
