defmodule WhoNeedHelp.GoogleAuthFake do
  @behaviour WhoNeedHelp.GoogleAuth

  @impl true
  def enabled?, do: true

  @impl true
  def authorize_url(redirect_uri) do
    {:ok,
     %{
       url:
         "https://accounts.google.example/authorize?" <>
           URI.encode_query(%{"redirect_uri" => redirect_uri, "state" => "google-test-state"}),
       session_params: %{
         state: "google-test-state",
         nonce: "google-test-nonce",
         code_verifier: "google-test-verifier"
       }
     }}
  end

  @impl true
  def callback(_redirect_uri, params, %{state: state}) do
    cond do
      params["state"] != state ->
        {:error, :invalid_state}

      params["code"] in [nil, ""] ->
        {:error, :missing_code}

      params["email_verified"] == "false" ->
        {:error, :email_not_verified}

      true ->
        email = params["email"] || "google-user@example.com"

        {:ok,
         %{
           provider_uid: params["uid"] || "google-user-42",
           email: email,
           email_verified: true,
           display_name: params["name"] || "Google Neighbor"
         }}
    end
  end
end
