# typed: false
# frozen_string_literal: true

module Hephaestus
  module Webmocks
    module YettoWebmock
      def yetto_auth_header(payload, signing_secret = Hephaestus::YETTO_SIGNING_SECRET)
        "sha256=#{OpenSSL::HMAC.hexdigest(Hephaestus::ValidatesFromYetto::SHA256_DIGEST, signing_secret, payload.to_json)}"
      end

      def assert_requested_post_access_token(plug_installation_id, times: 1)
        assert_requested(:post, "#{Hephaestus::YettoService::YETTO_API_VERSION_TLD}/plug/installations/#{plug_installation_id}/access_tokens", times: times)
      end

      def stub_post_access_token(plug_installation_id)
        response = {
          token: Faker::Alphanumeric.alphanumeric(number: 26).upcase,
        }

        stub_request(:post, "#{Hephaestus::YettoService::YETTO_API_VERSION_TLD}/plug/installations/#{plug_installation_id}/access_tokens")
          .to_return(
            body: response.to_json,
          )
      end

      def assert_requested_get_plug_installation(plug_installation_id, times: 1)
        assert_requested(:get, "#{Hephaestus::YettoService::YETTO_API_VERSION_TLD}/installations/#{plug_installation_id}", times:)
      end

      def stub_get_plug_installation(plug_installation_id, response: {}, status: 200)
        stub_request(:get, "#{Hephaestus::YettoService::YETTO_API_VERSION_TLD}/installations/#{plug_installation_id}")
          .to_return(
            status: status,
            headers: { content_type: "application/json; charset=utf-8" },
            body: response.to_json,
          )
      end

      def assert_requested_update_plug_installation(plug_installation_id)
        assert_requested(:patch, "#{Hephaestus::YettoService::YETTO_API_VERSION_TLD}/installations/#{plug_installation_id}")
      end

      def assert_not_requested_update_plug_installation(plug_installation_id)
        assert_requested(:patch, "#{Hephaestus::YettoService::YETTO_API_VERSION_TLD}/installations/#{plug_installation_id}", times: 0)
      end

      def stub_update_plug_installation(plug_installation_id, params, response: {}, status: 200)
        stub_post_access_token(plug_installation_id)

        response[:plug_installation] = { id: plug_installation_id }

        stub_request(:patch, "#{Hephaestus::YettoService::YETTO_API_VERSION_TLD}/installations/#{plug_installation_id}")
          .with(
            body: params,
          )
          .to_return(
            status: status,
            headers: { content_type: "application/json; charset=utf-8" },
            body: response.to_json,
          )
      end

      def assert_requested_create_conversation(plug_installation_id, inbox_id)
        assert_requested(:post, "#{Hephaestus::YettoService::YETTO_API_VERSION_TLD}/inboxes/#{inbox_id}/conversations")
      end

      def stub_create_conversation(plug_installation_id, inbox_id, payload, response: {})
        stub_post_access_token(plug_installation_id)

        response[:plug_installation] = { id: plug_installation_id }

        stub_request(:post, "#{Hephaestus::YettoService::YETTO_API_VERSION_TLD}/inboxes/#{inbox_id}/conversations")
          .with(
            body: payload,
          )
          .to_return(
            status: 200,
            headers: { content_type: "application/json; charset=utf-8" },
            body: response.to_json,
          )
      end

      def assert_requested_get_plug_installations(filter: "")
        assert_requested(:get, "#{Hephaestus::YettoService::YETTO_API_VERSION_TLD}/plug/installations?#{filter}")
      end

      def stub_get_plug_installations(response, status: 200, filter: "")
        stub_request(:get, "#{Hephaestus::YettoService::YETTO_API_VERSION_TLD}/plug/installations?#{filter}")
          .to_return(
            status: status,
            headers: { content_type: "application/json; charset=utf-8" },
            body: response.to_json,
          )
      end

      def assert_requested_get_messages_in_inbox(plug_installation_id, inbox_id, filter: "")
        assert_requested(:get, "#{Hephaestus::YettoService::YETTO_API_VERSION_TLD}/inboxes/#{inbox_id}/messages?#{filter}")
      end

      def stub_get_messages_in_inbox(plug_installation_id, inbox_id, response, filter: "")
        stub_post_access_token(plug_installation_id)
        stub_request(:get, "#{Hephaestus::YettoService::YETTO_API_VERSION_TLD}/inboxes/#{inbox_id}/messages?#{filter}")
          .to_return(
            status: 200,
            body: response.to_json,
          )
      end

      def assert_requested_get_messages_in_conversation(plug_installation_id, conversation_id, filter: "")
        assert_requested(:get, "#{::Hephaestus::YettoService::YETTO_API_VERSION_TLD}/conversations/#{conversation_id}/messages?#{filter}")
      end

      def stub_get_messages_in_conversation(plug_installation_id, conversation_id, response, status: 200, filter: "")
        stub_post_access_token(plug_installation_id)

        stub_request(:get, "#{::Hephaestus::YettoService::YETTO_API_VERSION_TLD}/conversations/#{conversation_id}/messages?#{filter}")
          .to_return(
            status: status,
            headers: { content_type: "application/json; charset=utf-8" },
            body: response.to_json,
          )
      end

      def stub_add_message_to_conversation(plug_installation_id, conversation_id, payload, response: {}, status: 200)
        stub_post_access_token(plug_installation_id)

        response[:plug_installation] = { id: plug_installation_id }

        stub_request(:post, "#{Hephaestus::YettoService::YETTO_API_VERSION_TLD}/conversations/#{conversation_id}/messages")
          .with(
            body: payload,
          )
          .to_return(
            status: status,
            headers: { content_type: "application/json; charset=utf-8" },
            body: response.to_json,
          )
      end

      def assert_requested_add_message_to_conversation(plug_installation_id, conversation_id)
        assert_requested(:post, "#{Hephaestus::YettoService::YETTO_API_VERSION_TLD}/conversations/#{conversation_id}/messages")
      end

      def assert_requested_create_message(plug_installation_id, message_id)
        assert_requested(:post, "#{::Hephaestus::YettoService::YETTO_API_VERSION_TLD}/messages/#{message_id}/replies")
      end

      def stub_create_message(plug_installation_id, message_id, payload)
        stub_post_access_token(plug_installation_id)

        stub_request(:post, "#{::Hephaestus::YettoService::YETTO_API_VERSION_TLD}/messages/#{message_id}/replies")
          .with(
            body: payload,
          )
          .to_return(
            status: 200,
            headers: { content_type: "application/json; charset=utf-8" },
            body: {}.to_json,
          )
      end

      def assert_requested_update_message(plug_installation_id, message_id)
        assert_requested(:patch, "#{::Hephaestus::YettoService::YETTO_API_VERSION_TLD}/messages/#{message_id}")
      end

      def stub_update_message(plug_installation_id, message_id, payload)
        stub_post_access_token(plug_installation_id)
        stub_request(:patch, "#{::Hephaestus::YettoService::YETTO_API_VERSION_TLD}/messages/#{message_id}")
          .with(
            body: payload,
          )
          .to_return(
            status: 200,
            headers: { content_type: "application/json; charset=utf-8" },
            body: {}.to_json,
          )
      end

      def assert_requested_find_message_in_conversation_by_metadata(plug_installation_id, conversation_id)
        assert_requested(:get, "#{Hephaestus::YettoService::YETTO_API_VERSION_TLD}/conversations/#{conversation_id}/messages?#{filter}")
      end

      def stub_find_message_in_conversation(plug_installation_id, conversation_id, response, status: 200, filter: "")
        stub_post_access_token(plug_installation_id)

        stub_request(:get, "#{Hephaestus::YettoService::YETTO_API_VERSION_TLD}/conversations/#{conversation_id}/messages?#{filter}")
          .to_return(
            status: status,
            headers: { content_type: "application/json; charset=utf-8" },
            body: response.to_json,
          )
      end
    end
  end
end