from typing import Any

from httpx import AsyncClient, Response


def make_auth_headers(access_token: str | None) -> dict[str, Any]:
    return {} if access_token is None else {"Authorization": f"Bearer {access_token}"}


class TestApiGateway:
    def __init__(self, client: AsyncClient) -> None:
        self._client = client

    async def sign_up_email(self, email: str, password: str) -> Response:
        return await self._client.post(
            "/auth/sign_up/email",
            json={"email": email, "password": password},
        )

    async def sign_in_email(self, email: str, password: str) -> Response:
        return await self._client.post(
            "/auth/sign_in/email",
            json={"email": email, "password": password},
        )

    async def get_profile(self, access_token: str | None) -> Response:
        return await self._client.get(
            "/profile",
            headers=make_auth_headers(access_token),
        )

    async def patch_profile(
        self,
        access_token: str | None = None,
        display_name: str | None = None,
        first_name: str | None = None,
        last_name: str | None = None,
        avatar_url: str | None = None,
        phone: str | None = None,
    ) -> Response:
        return await self._client.patch(
            "/profile",
            json={
                key: value
                for key, value in {
                    "display_name": display_name,
                    "first_name": first_name,
                    "last_name": last_name,
                    "avatar_url": avatar_url,
                    "phone": phone,
                }.items()
                if value is not None
            },
            headers=make_auth_headers(access_token),
        )

    async def create_resume(
        self,
        access_token: str,
        position: str,
        location: str,
        about_me: str,
        key_skills: list[str],
        experience_type: str,
        experience: list[dict[str, Any]] | None = None,
        education: list[dict[str, Any]] | None = None,
        projects: list[dict[str, Any]] | None = None,
    ) -> Response:
        json_data: dict[str, Any] = {
            "position": position,
            "location": location,
            "about_me": about_me,
            "key_skills": key_skills,
            "experience_type": experience_type,
        }
        if experience is not None:
            json_data["experience"] = experience
        if education is not None:
            json_data["education"] = education
        if projects is not None:
            json_data["projects"] = projects
        return await self._client.post(
            "/resume",
            json=json_data,
            headers=make_auth_headers(access_token),
        )

    async def get_resume(self, access_token: str, resume_id: str) -> Response:
        return await self._client.get(
            f"/resume/{resume_id}",
            headers=make_auth_headers(access_token),
        )

    async def edit_resume(
        self,
        access_token: str,
        resume_id: str,
        position: str | None = None,
        location: str | None = None,
        about_me: str | None = None,
        key_skills: list[str] | None = None,
        experience_type: str | None = None,
        experience: list[dict[str, Any]] | None = None,
        education: list[dict[str, Any]] | None = None,
        projects: list[dict[str, Any]] | None = None,
    ) -> Response:
        json_data: dict[str, Any] = {}
        if position is not None:
            json_data["position"] = position
        if location is not None:
            json_data["location"] = location
        if about_me is not None:
            json_data["about_me"] = about_me
        if key_skills is not None:
            json_data["key_skills"] = key_skills
        if experience_type is not None:
            json_data["experience_type"] = experience_type
        if experience is not None:
            json_data["experience"] = experience
        if education is not None:
            json_data["education"] = education
        if projects is not None:
            json_data["projects"] = projects
        return await self._client.patch(
            f"/resume/{resume_id}",
            json=json_data,
            headers=make_auth_headers(access_token),
        )

    async def add_key_skill(
        self,
        key_skills: list[str],
    ) -> Response:
        return await self._client.post(
            "/key_skills",
            json={"key_skills": key_skills},
        )

    async def search_key_skills(self, query: str) -> Response:
        return await self._client.get(
            "/key_skills",
            params={"query": query},
        )
