"""Tests for the distribution formatters."""

import json
from datetime import datetime

from syke.distribution.formatters import format_profile
from syke.models import ActiveThread, UserProfile, VoicePattern


def _sample_profile() -> UserProfile:
    return UserProfile(
        user_id="test_user",
        created_at=datetime(2025, 2, 10, 12, 0),
        identity_anchor="A curious builder who loves exploring consciousness and technology.",
        active_threads=[
            ActiveThread(
                name="Syke Hackathon",
                description="Building a personal context daemon for Claude Code hackathon.",
                intensity="high",
                platforms=["github", "chatgpt"],
                recent_signals=["Multiple commits today", "ChatGPT conversations about architecture"],
            ),
        ],
        recent_detail="Working intensely on Syke, a personal context daemon. Writing Python, using Opus 4.6.",
        background_context="Has been thinking about AI personalization for years.",
        world_state="Building Syke v0.2 for Claude Code Hackathon (deadline Feb 16). Core focus: ask() tool. 4 live adapters, 3207 events ingested.",
        voice_patterns=VoicePattern(
            tone="casual, intense, exploratory",
            vocabulary_notes=["uses 'vibe' often", "says 'ship it'"],
            communication_style="Direct, fast-paced, mixes technical and philosophical.",
            examples=["Let's just ship this and iterate."],
        ),
        sources=["gmail", "chatgpt", "github"],
        events_count=150,
    )


def test_format_json():
    profile = _sample_profile()
    result = format_profile(profile, "json")
    data = json.loads(result)
    assert data["user_id"] == "test_user"
    assert data["identity_anchor"].startswith("A curious builder")
    assert data["world_state"].startswith("Building Syke v0.2")


def test_format_markdown():
    profile = _sample_profile()
    result = format_profile(profile, "markdown")
    assert "# test_user — Syke Profile" in result
    assert "Syke Hackathon" in result
    assert "Active Threads" in result
    assert "## World State" in result
    assert "ask() tool" in result


def test_format_claude_md():
    profile = _sample_profile()
    result = format_profile(profile, "claude-md")
    assert "# About test_user" in result
    assert "Generated by Syke" in result
    assert "What's Active Right Now" in result
    assert "## Current World State" in result
    assert "ask() tool" in result


def test_format_user_md():
    profile = _sample_profile()
    result = format_profile(profile, "user-md")
    assert "# User Profile: test_user" in result
    assert "Identity" in result
    assert "Current Focus" in result
    assert "## Current State" in result
    assert "ask() tool" in result


def test_format_without_world_state():
    """Formatters should not render world_state section when empty."""
    profile = _sample_profile()
    profile.world_state = ""
    result = format_profile(profile, "markdown")
    assert "## World State" not in result
    result_claude = format_profile(profile, "claude-md")
    assert "## Current World State" not in result_claude
    result_user = format_profile(profile, "user-md")
    assert "## Current State" not in result_user
