"""User profile tools."""

from __future__ import annotations

from fabledassistant.services.tools._registry import tool


@tool(
    name="get_profile",
    description=(
        "Retrieve the user's stored profile: name, job title, industry, expertise level, "
        "preferred response style, tone, and interests. Use this when you need to personalise "
        "a response and the user's profile hasn't already been injected into context."
    ),
    parameters={},
    read_only=True,
)
async def get_profile_tool(*, user_id, arguments, **_ctx):
    from fabledassistant.services.user_profile import get_profile as _get_profile

    profile = await _get_profile(user_id)
    return {
        "success": True,
        "type": "profile",
        "data": {
            "display_name": profile.display_name or "",
            "job_title": profile.job_title or "",
            "industry": profile.industry or "",
            "expertise_level": profile.expertise_level or "intermediate",
            "response_style": profile.response_style or "balanced",
            "tone": profile.tone or "casual",
            "interests": profile.interests or [],
        },
    }


@tool(
    name="update_profile",
    description=(
        "Update the user's stored profile when they share personal information: their name, "
        "job, industry, expertise, preferred response style, tone, or interests. "
        "Only set fields the user has explicitly mentioned."
    ),
    parameters={
        "display_name": {"type": "string", "description": "User's preferred display name"},
        "job_title": {"type": "string", "description": "Job title (e.g. 'Software Engineer')"},
        "industry": {"type": "string", "description": "Industry (e.g. 'Healthcare', 'Finance')"},
        "expertise_level": {"type": "string", "enum": ["novice", "intermediate", "expert"], "description": "User's general expertise level"},
        "response_style": {"type": "string", "enum": ["concise", "balanced", "detailed"], "description": "Preferred response length/depth"},
        "tone": {"type": "string", "enum": ["casual", "professional", "technical"], "description": "Preferred communication tone"},
        "interests": {"type": "array", "items": {"type": "string"}, "description": "List of topics or hobbies the user is interested in"},
    },
)
async def update_profile_tool(*, user_id, arguments, **_ctx):
    from fabledassistant.services.user_profile import VALID_EXPERTISE, VALID_STYLES, VALID_TONES, update_profile as _update_profile

    data: dict = {}
    for field in ("display_name", "job_title", "industry"):
        val = arguments.get(field)
        if val is not None:
            data[field] = str(val)
    expertise = arguments.get("expertise_level")
    if expertise in VALID_EXPERTISE:
        data["expertise_level"] = expertise
    style = arguments.get("response_style")
    if style in VALID_STYLES:
        data["response_style"] = style
    tone = arguments.get("tone")
    if tone in VALID_TONES:
        data["tone"] = tone
    interests = arguments.get("interests")
    if isinstance(interests, list):
        data["interests"] = [str(i) for i in interests if str(i).strip()]
    if not data:
        return {"success": False, "error": "No valid fields provided to update."}
    await _update_profile(user_id, data)
    return {"success": True, "type": "profile_updated", "data": {"fields_updated": list(data.keys())}}
