"""Prompt template management with versioning and A/B testing."""

from __future__ import annotations

from typing import Any, Dict, Optional

from .._resource import AsyncResource, Resource


class Prompts(Resource):
    """Sync prompt operations."""

    def list(self) -> Dict[str, Any]:
        """List all prompt templates."""
        return self._get("/v1/prompts")

    def get(self, prompt_id: str) -> Dict[str, Any]:
        """Get a prompt with all versions."""
        return self._get(f"/v1/prompts/{prompt_id}")

    def create(self, params: Dict[str, Any]) -> Dict[str, Any]:
        """Create a new prompt template."""
        return self._post("/v1/prompts", params)

    def update(self, prompt_id: str, params: Dict[str, Any]) -> Dict[str, Any]:
        """Add a new version to an existing prompt."""
        return self._put(f"/v1/prompts/{prompt_id}", params)

    def remove(self, prompt_id: str) -> Dict[str, Any]:
        """Delete a prompt template."""
        return self._delete(f"/v1/prompts/{prompt_id}")

    def resolve(
        self,
        prompt_id: str,
        *,
        variables: Optional[Dict[str, str]] = None,
        version: Optional[int] = None,
        environment: Optional[str] = None,
    ) -> Dict[str, Any]:
        """Resolve a prompt template with variables."""
        body: Dict[str, Any] = {}
        if variables is not None:
            body["variables"] = variables
        if version is not None:
            body["version"] = version
        if environment is not None:
            body["environment"] = environment
        return self._post(f"/v1/prompts/{prompt_id}/resolve", body)

    def ab_test(
        self,
        prompt_id: str,
        *,
        version_a: int,
        version_b: int,
        split_ratio: float = 0.5,
    ) -> Dict[str, Any]:
        """Configure A/B test between two prompt versions."""
        return self._post(
            f"/v1/prompts/{prompt_id}/ab-test",
            {
                "version_a": version_a,
                "version_b": version_b,
                "split_ratio": split_ratio,
            },
        )


class AsyncPrompts(AsyncResource):
    """Async prompt operations."""

    async def list(self) -> Dict[str, Any]:
        return await self._get("/v1/prompts")

    async def get(self, prompt_id: str) -> Dict[str, Any]:
        return await self._get(f"/v1/prompts/{prompt_id}")

    async def create(self, params: Dict[str, Any]) -> Dict[str, Any]:
        return await self._post("/v1/prompts", params)

    async def update(self, prompt_id: str, params: Dict[str, Any]) -> Dict[str, Any]:
        return await self._put(f"/v1/prompts/{prompt_id}", params)

    async def remove(self, prompt_id: str) -> Dict[str, Any]:
        return await self._delete(f"/v1/prompts/{prompt_id}")

    async def resolve(
        self,
        prompt_id: str,
        *,
        variables: Optional[Dict[str, str]] = None,
        version: Optional[int] = None,
        environment: Optional[str] = None,
    ) -> Dict[str, Any]:
        body: Dict[str, Any] = {}
        if variables is not None:
            body["variables"] = variables
        if version is not None:
            body["version"] = version
        if environment is not None:
            body["environment"] = environment
        return await self._post(f"/v1/prompts/{prompt_id}/resolve", body)

    async def ab_test(
        self,
        prompt_id: str,
        *,
        version_a: int,
        version_b: int,
        split_ratio: float = 0.5,
    ) -> Dict[str, Any]:
        return await self._post(
            f"/v1/prompts/{prompt_id}/ab-test",
            {
                "version_a": version_a,
                "version_b": version_b,
                "split_ratio": split_ratio,
            },
        )
