#!/usr/bin/env python3
"""
Example 2: Token Merging with Conflict Resolution

Shows how to merge tokens from multiple sources using different strategies.
"""

import asyncio
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent.parent))


async def main():
    print("=" * 60)
    print("EXAMPLE 2: Token Merging & Conflict Resolution")
    print("=" * 60)

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

    # Create tokens from different sources
    print("\n1. Creating token collections from different sources...")
    print("-" * 60)

    css_tokens = TokenCollection([
        DesignToken(name="color.primary", value="#FF0000", type=TokenType.COLOR, source="css"),
        DesignToken(name="color.secondary", value="#00FF00", type=TokenType.COLOR, source="css"),
        DesignToken(name="spacing.md", value="16px", type=TokenType.SPACING, source="css"),
    ])

    figma_tokens = TokenCollection([
        DesignToken(name="color.primary", value="#3B82F6", type=TokenType.COLOR, source="figma"),
        DesignToken(name="color.accent", value="#F59E0B", type=TokenType.COLOR, source="figma"),
    ])

    tailwind_tokens = TokenCollection([
        DesignToken(name="color.primary", value="#2563EB", type=TokenType.COLOR, source="tailwind"),
        DesignToken(name="spacing.lg", value="24px", type=TokenType.SPACING, source="tailwind"),
    ])

    print(f"CSS:      {len(css_tokens.tokens)} tokens")
    print(f"Figma:    {len(figma_tokens.tokens)} tokens")
    print(f"Tailwind: {len(tailwind_tokens.tokens)} tokens")

    # Test different merge strategies
    strategies = [
        MergeStrategy.FIRST,
        MergeStrategy.LAST,
        MergeStrategy.PREFER_FIGMA,
    ]

    for strategy in strategies:
        print(f"\n2. Merge Strategy: {strategy.value.upper()}")
        print("-" * 60)

        merger = TokenMerger(strategy=strategy)
        result = merger.merge([css_tokens, figma_tokens, tailwind_tokens])

        print(f"✅ Merged: {len(result.collection.tokens)} tokens")
        print(f"⚠️  Conflicts: {len(result.conflicts)}")

        if result.conflicts:
            print("\nConflict Resolutions:")
            for conflict in result.conflicts:
                print(f"  • {conflict.token_name}:")
                print(f"    Existing: {conflict.existing.value} (from {conflict.existing.source})")
                print(f"    Incoming: {conflict.incoming.value} (from {conflict.incoming.source})")
                print(f"    ✓ Chose: {conflict.resolved_token.value} (from {conflict.resolved_token.source})")

        # Show final token values
        print("\nFinal Tokens:")
        for token in result.collection.tokens:
            print(f"  {token.name}: {token.value} (source: {token.source})")

    print("\n" + "=" * 60)
    print("💡 Key Takeaways:")
    print("  • FIRST: Keeps first occurrence, ignores later sources")
    print("  • LAST: Override with latest source")
    print("  • PREFER_FIGMA: Prioritizes Figma as source of truth")
    print("  • PREFER_CODE: Prioritizes code sources (CSS, SCSS)")
    print("=" * 60)


if __name__ == "__main__":
    asyncio.run(main())
