"""
Tests for token merging and conflict resolution.
"""

import pytest
from tools.ingest.merge import TokenMerger, MergeStrategy
from tools.ingest.base import TokenCollection, DesignToken, TokenType


def test_merge_no_conflicts():
    """Test merging collections with no conflicts."""
    col1 = TokenCollection([
        DesignToken(name="color.red", value="#FF0000", type=TokenType.COLOR)
    ])
    col2 = TokenCollection([
        DesignToken(name="color.blue", value="#0000FF", type=TokenType.COLOR)
    ])

    merger = TokenMerger(strategy=MergeStrategy.LAST)
    result = merger.merge([col1, col2])

    assert len(result.collection.tokens) == 2
    assert len(result.conflicts) == 0


def test_merge_strategy_first():
    """Test FIRST merge strategy."""
    col1 = TokenCollection([
        DesignToken(name="color.primary", value="#FF0000", type=TokenType.COLOR, source="css")
    ])
    col2 = TokenCollection([
        DesignToken(name="color.primary", value="#0000FF", type=TokenType.COLOR, source="figma")
    ])

    merger = TokenMerger(strategy=MergeStrategy.FIRST)
    result = merger.merge([col1, col2])

    assert len(result.collection.tokens) == 1
    assert len(result.conflicts) == 1
    # Should keep first value
    token = result.collection.tokens[0]
    assert token.value == "#FF0000"
    assert token.source == "css"


def test_merge_strategy_last():
    """Test LAST merge strategy."""
    col1 = TokenCollection([
        DesignToken(name="color.primary", value="#FF0000", type=TokenType.COLOR, source="css")
    ])
    col2 = TokenCollection([
        DesignToken(name="color.primary", value="#0000FF", type=TokenType.COLOR, source="figma")
    ])

    merger = TokenMerger(strategy=MergeStrategy.LAST)
    result = merger.merge([col1, col2])

    assert len(result.collection.tokens) == 1
    assert len(result.conflicts) == 1
    # Should keep last value
    token = result.collection.tokens[0]
    assert token.value == "#0000FF"
    assert token.source == "figma"


def test_merge_strategy_prefer_figma():
    """Test PREFER_FIGMA merge strategy."""
    col1 = TokenCollection([
        DesignToken(name="color.primary", value="#FF0000", type=TokenType.COLOR, source="css")
    ])
    col2 = TokenCollection([
        DesignToken(name="color.primary", value="#3B82F6", type=TokenType.COLOR, source="figma")
    ])
    col3 = TokenCollection([
        DesignToken(name="color.primary", value="#0000FF", type=TokenType.COLOR, source="scss")
    ])

    merger = TokenMerger(strategy=MergeStrategy.PREFER_FIGMA)
    result = merger.merge([col1, col2, col3])

    # Should prefer Figma value
    token = result.collection.tokens[0]
    assert token.value == "#3B82F6"
    assert token.source == "figma"


def test_merge_multiple_collections():
    """Test merging many collections."""
    collections = [
        TokenCollection([
            DesignToken(name=f"color.{i}", value=f"#{i:06X}", type=TokenType.COLOR)
        ])
        for i in range(10)
    ]

    merger = TokenMerger(strategy=MergeStrategy.LAST)
    result = merger.merge(collections)

    assert len(result.collection.tokens) == 10
    assert len(result.conflicts) == 0


def test_merge_empty_collections():
    """Test merging empty collections."""
    merger = TokenMerger(strategy=MergeStrategy.LAST)
    result = merger.merge([])

    assert len(result.collection.tokens) == 0
    assert len(result.conflicts) == 0
