defmodule WhoNeedHelp.SocialOAuthFake do
  @behaviour WhoNeedHelp.SocialOAuth

  @impl true
  def enabled?(:github), do: true

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

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

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

      true ->
        handle = params["login"] || "helpful-neighbor"

        {:ok,
         %{
           provider: :github,
           provider_uid: params["uid"] || "github-user-42",
           profile_url: "https://github.com/#{handle}",
           handle: "@#{handle}",
           verified_at: ~U[2026-07-18 18:00:00Z]
         }}
    end
  end
end
