"""Tests for array operations across different modes."""

from typing import Any

import pytest

from crystflux.v1 import Crystallizer, LatentValue
from crystflux.v1.adapters import enable_dream_mode


def test_first_rest_length_cases(tao_data: dict[str, Any]) -> None:
    """Test first_rest operations with various array lengths."""
    fragments = LatentValue(tao_data["anthology"]["fragments"]).to_array()
    first, rest = fragments.first_rest()
    assert first.get("source").as_str() == "道徳経"
    next_first, next_rest = rest.first_rest()
    assert next_first.get("source").as_str() == "荘子"
    tail_first, tail_rest = next_rest.first_rest()
    assert tail_first.is_void()
    assert tail_rest.is_void()

    empty = LatentValue([]).to_array()
    f, r = empty.first_rest()
    assert f.is_void() and r.is_void()

    single = LatentValue([{"x": 1}]).to_array()
    f, r = single.first_rest()
    assert f.get("x").expected_int() == 1
    assert r.first_rest()[0].is_void()


def test_rest_last_length_cases(tao_data: dict[str, Any]) -> None:
    """Test rest_last operations with various array lengths."""
    fragments = LatentValue(tao_data["anthology"]["fragments"]).to_array()
    rest_vals, last = fragments.rest_last()
    assert last.get("source_en").as_str() == "Zhuangzi"
    first_of_rest, _ = rest_vals.first_rest()
    assert first_of_rest.get("source_en").as_str() == "Tao Te Ching"

    empty = LatentValue([]).to_array()
    rest, last = empty.rest_last()
    assert rest.is_void() and last.is_void()

    single = LatentValue([{"x": 1}]).to_array()
    rest, last = single.rest_last()
    assert last.get("x").expected_int() == 1
    assert rest.first_rest()[0].is_void()

    multi_element = LatentValue([{"a": 1}, {"b": 2}, {"c": 3}]).to_array()
    rest, last = multi_element.rest_last()
    assert rest.as_json_value() == ({"a": 1}, {"b": 2})
    assert last.get("c").expected_int() == 3


def test_strict_map_filter_operations(tao_data: dict[str, Any]) -> None:
    """Test map and filter operations for strict mode."""
    fragments = tao_data["anthology"]["fragments"]

    strict_fragments = Crystallizer.strict(fragments)
    mapped_sources = strict_fragments.map(lambda v: v.get("source"))
    assert mapped_sources.as_json_value() == tuple(f["source"] for f in fragments)
    filtered = strict_fragments.filter(lambda v: v.get("source_en").as_str() == "Zhuangzi")
    assert filtered.as_json_value() == (fragments[1],)
    with pytest.raises(TypeError):
        Crystallizer.strict({"a": 1}).map(lambda v: v)


def test_latent_map_filter_operations() -> None:
    """Test map and filter operations for latent mode."""
    latent_non_array = Crystallizer.latent({"a": 1})
    assert latent_non_array.map(lambda v: v).is_void()


def test_dream_map_filter_operations(tao_data: dict[str, Any]) -> None:
    """Test map and filter operations for dream mode."""
    fragments = tao_data["anthology"]["fragments"]
    with enable_dream_mode():
        dream_fragments = Crystallizer.dream(fragments)
        dream_sources = dream_fragments.map(lambda v: v.get("source"))
        assert dream_sources.as_json_value() == tuple(f["source"] for f in fragments)

        dream_missing_chain = (
            Crystallizer.dream({"a": 1}).missing().get("missing").map(lambda v: v.get("x"))  # type:ignore
        )
        assert dream_missing_chain.is_void()
        assert dream_missing_chain.filter(lambda v: True).is_void()  # type:ignore
